Fix sensor.time intermittent startup exception (#38525)
parent
c2f5831181
commit
df8e179207
|
@ -42,15 +42,9 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||
_LOGGER.error("Timezone is not set in Home Assistant configuration")
|
||||
return False
|
||||
|
||||
devices = []
|
||||
for variable in config[CONF_DISPLAY_OPTIONS]:
|
||||
device = TimeDateSensor(hass, variable)
|
||||
async_track_point_in_utc_time(
|
||||
hass, device.point_in_time_listener, device.get_next_interval()
|
||||
)
|
||||
devices.append(device)
|
||||
|
||||
async_add_entities(devices, True)
|
||||
async_add_entities(
|
||||
[TimeDateSensor(hass, variable) for variable in config[CONF_DISPLAY_OPTIONS]]
|
||||
)
|
||||
|
||||
|
||||
class TimeDateSensor(Entity):
|
||||
|
@ -62,6 +56,7 @@ class TimeDateSensor(Entity):
|
|||
self.type = option_type
|
||||
self._state = None
|
||||
self.hass = hass
|
||||
self.unsub = None
|
||||
|
||||
self._update_internal_state(dt_util.utcnow())
|
||||
|
||||
|
@ -84,6 +79,18 @@ class TimeDateSensor(Entity):
|
|||
return "mdi:calendar"
|
||||
return "mdi:clock"
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Set up next update."""
|
||||
self.unsub = async_track_point_in_utc_time(
|
||||
self.hass, self.point_in_time_listener, self.get_next_interval()
|
||||
)
|
||||
|
||||
async def async_will_remove_from_hass(self) -> None:
|
||||
"""Cancel next update."""
|
||||
if self.unsub:
|
||||
self.unsub()
|
||||
self.unsub = None
|
||||
|
||||
def get_next_interval(self, now=None):
|
||||
"""Compute next time an update should occur."""
|
||||
if now is None:
|
||||
|
@ -137,6 +144,6 @@ class TimeDateSensor(Entity):
|
|||
"""Get the latest data and update state."""
|
||||
self._update_internal_state(time_date)
|
||||
self.async_write_ha_state()
|
||||
async_track_point_in_utc_time(
|
||||
self.unsub = async_track_point_in_utc_time(
|
||||
self.hass, self.point_in_time_listener, self.get_next_interval()
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue