4040from .coordinator import SatDataUpdateCoordinator , DeviceState
4141from .entity import SatEntity
4242from .errors import Errors , Error
43- from .helpers import convert_time_str_to_seconds
43+ from .helpers import convert_time_str_to_seconds , is_state_stale , state_age_seconds
4444from .manufacturers .geminox import Geminox
4545from .pwm import PWMState
4646from .relative_modulation import RelativeModulation , RelativeModulationState
@@ -90,20 +90,6 @@ def __init__(self, coordinator: SatDataUpdateCoordinator, config_entry: ConfigEn
9090 self .inside_sensor_entity_id = config_entry .data .get (CONF_INSIDE_SENSOR_ENTITY_ID )
9191 self .humidity_sensor_entity_id = config_entry .data .get (CONF_HUMIDITY_SENSOR_ENTITY_ID )
9292
93- # Get some sensor entity states
94- inside_sensor_entity = coordinator .hass .states .get (self .inside_sensor_entity_id )
95- humidity_sensor_entity = coordinator .hass .states .get (self .humidity_sensor_entity_id ) if self .humidity_sensor_entity_id is not None else None
96-
97- # Get current temperature
98- self ._current_temperature = None
99- if inside_sensor_entity is not None and inside_sensor_entity .state not in [STATE_UNKNOWN , STATE_UNAVAILABLE ]:
100- self ._current_temperature = float (inside_sensor_entity .state )
101-
102- # Get current temperature
103- self ._current_humidity = None
104- if humidity_sensor_entity is not None and humidity_sensor_entity .state not in [STATE_UNKNOWN , STATE_UNAVAILABLE ]:
105- self ._current_humidity = float (humidity_sensor_entity .state )
106-
10793 # Get outside sensor entity IDs
10894 self .outside_sensor_entities = config_entry .data .get (CONF_OUTSIDE_SENSOR_ENTITY_ID )
10995
@@ -405,10 +391,10 @@ def extra_state_attributes(self):
405391
406392 "rooms" : self ._rooms ,
407393 "setpoint" : self ._setpoint ,
408- "current_humidity" : self ._current_humidity ,
394+ "current_humidity" : self .current_humidity ,
409395
410- "summer_simmer_index" : SummerSimmer .index (self ._current_temperature , self ._current_humidity ),
411- "summer_simmer_perception" : SummerSimmer .perception (self ._current_temperature , self ._current_humidity ),
396+ "summer_simmer_index" : SummerSimmer .index (self .current_temperature , self .current_humidity ),
397+ "summer_simmer_perception" : SummerSimmer .perception (self .current_temperature , self .current_humidity ),
412398
413399 "valves_open" : self .valves_open ,
414400 "heating_curve" : self .heating_curve .value ,
@@ -428,23 +414,29 @@ def extra_state_attributes(self):
428414 }
429415
430416 @property
431- def current_temperature (self ):
417+ def current_temperature (self ) -> Optional [ float ] :
432418 """Return the sensor temperature."""
433- if self ._thermal_comfort and self ._current_humidity is not None :
434- return SummerSimmer .index (self ._current_temperature , self ._current_humidity )
419+ if (current_temperature := self ._get_entity_state_float (self .inside_sensor_entity_id )) is None :
420+ return None
421+
422+ if self ._thermal_comfort :
423+ return SummerSimmer .index (current_temperature , self .current_humidity )
424+
425+ return current_temperature
435426
436- return self ._current_temperature
427+ @property
428+ def current_humidity (self ) -> Optional [float ]:
429+ """Return the sensor humidity."""
430+ if self .humidity_sensor_entity_id is None :
431+ return None
432+
433+ return self ._get_entity_state_float (self .humidity_sensor_entity_id )
437434
438435 @property
439436 def target_temperature (self ):
440437 """Return the temperature we try to reach."""
441438 return self ._target_temperature
442439
443- @property
444- def current_humidity (self ):
445- """Return the sensor humidity."""
446- return self ._current_humidity
447-
448440 @property
449441 def error (self ):
450442 """Return the error value."""
@@ -632,7 +624,6 @@ async def _async_inside_sensor_changed(self, event: Event[EventStateChangedData]
632624 return
633625
634626 _LOGGER .debug ("Inside Sensor Changed." )
635- self ._current_temperature = float (new_state .state )
636627 self .async_write_ha_state ()
637628
638629 self ._async_control_pid ()
@@ -656,7 +647,6 @@ async def _async_humidity_sensor_changed(self, event: Event[EventStateChangedDat
656647 return
657648
658649 _LOGGER .debug ("Humidity Sensor Changed." )
659- self ._current_humidity = float (new_state .state )
660650 self .async_write_ha_state ()
661651
662652 self ._async_control_pid ()
@@ -1135,3 +1125,22 @@ async def async_send_notification(self, title: str, message: str, service: str =
11351125 """Send a notification to the user."""
11361126 data = {"title" : title , "message" : message }
11371127 await self .hass .services .async_call (notify .DOMAIN , service , data )
1128+
1129+ def _get_entity_state_float (self , entity_id : str ) -> Optional [float ]:
1130+ """Return state if available and valid."""
1131+ if entity_id is None :
1132+ return None
1133+
1134+ if (entity := self .hass .states .get (entity_id )) is None :
1135+ return None
1136+
1137+ sensor_max_value_age = self ._sensor_max_value_age
1138+
1139+ if is_state_stale (entity , sensor_max_value_age ):
1140+ _LOGGER .debug ("Sensor %s stale for %s (age=%.1fs > %.1fs)" , entity_id , self .entity_id , state_age_seconds (entity ), sensor_max_value_age )
1141+ return None
1142+
1143+ if entity .state in [STATE_UNKNOWN , STATE_UNAVAILABLE ]:
1144+ return None
1145+
1146+ return float (entity .state )
0 commit comments