@@ -96,7 +96,9 @@ async def async_setup_platform(
96
96
97
97
await async_setup_reload_service (hass , DOMAIN , PLATFORMS )
98
98
99
- async_add_entities ([PeriodicMinMaxSensor (hass , entity_id , name , sensor_type , unique_id )])
99
+ async_add_entities (
100
+ [PeriodicMinMaxSensor (hass , entity_id , name , sensor_type , unique_id )]
101
+ )
100
102
101
103
102
104
class PeriodicMinMaxSensor (SensorEntity , RestoreEntity ):
@@ -136,13 +138,14 @@ def __init__(
136
138
self .max_value : float | None = None
137
139
self ._state : Any = None
138
140
139
-
140
141
async def async_added_to_hass (self ) -> None :
141
142
"""Handle added to Hass."""
142
143
143
144
self .async_on_remove (
144
145
async_track_state_change_event (
145
- self .hass , self ._source_entity_id , self ._async_min_max_sensor_state_listener
146
+ self .hass ,
147
+ self ._source_entity_id ,
148
+ self ._async_min_max_sensor_state_listener ,
146
149
)
147
150
)
148
151
@@ -158,18 +161,34 @@ async def async_added_to_hass(self) -> None:
158
161
159
162
if entry :
160
163
self ._unit_of_measurement = entry .unit_of_measurement
161
- self ._attr_device_class = SensorDeviceClass (entry .device_class ) if entry .device_class else None
162
- self ._attr_icon = entry .icon if entry .icon else entry .original_icon if entry .original_icon else ICON
164
+ self ._attr_device_class = (
165
+ SensorDeviceClass (entry .device_class ) if entry .device_class else None
166
+ )
167
+ self ._attr_icon = (
168
+ entry .icon
169
+ if entry .icon
170
+ else entry .original_icon
171
+ if entry .original_icon
172
+ else ICON
173
+ )
163
174
164
175
state = await self .async_get_last_state ()
165
- if state is not None and state .state not in [STATE_UNKNOWN , STATE_UNAVAILABLE ]:
176
+ if state is not None and state .state not in [
177
+ STATE_UNKNOWN ,
178
+ STATE_UNAVAILABLE ,
179
+ ]:
166
180
self ._state = float (state .state )
167
181
self ._calc_values ()
168
182
169
183
# Replay current state of source entitiy
170
184
state = self .hass .states .get (self ._source_entity_id )
171
185
state_event : Event [EventStateChangedData ] = Event (
172
- "" , {"entity_id" : self ._source_entity_id , "new_state" : state , "old_state" : None }
186
+ "" ,
187
+ {
188
+ "entity_id" : self ._source_entity_id ,
189
+ "new_state" : state ,
190
+ "old_state" : None ,
191
+ },
173
192
)
174
193
self ._async_min_max_sensor_state_listener (state_event , update_state = False )
175
194
@@ -259,4 +278,4 @@ async def handle_reset(self) -> None:
259
278
self .min_value = self ._state
260
279
self .max_value = self ._state
261
280
262
- self .async_write_ha_state ()
281
+ self .async_write_ha_state ()
0 commit comments