mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Fix sensor.time intermittent startup exception (#38525)
This commit is contained in:
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")
|
_LOGGER.error("Timezone is not set in Home Assistant configuration")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
devices = []
|
async_add_entities(
|
||||||
for variable in config[CONF_DISPLAY_OPTIONS]:
|
[TimeDateSensor(hass, variable) 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)
|
|
||||||
|
|
||||||
|
|
||||||
class TimeDateSensor(Entity):
|
class TimeDateSensor(Entity):
|
||||||
@ -62,6 +56,7 @@ class TimeDateSensor(Entity):
|
|||||||
self.type = option_type
|
self.type = option_type
|
||||||
self._state = None
|
self._state = None
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
|
self.unsub = None
|
||||||
|
|
||||||
self._update_internal_state(dt_util.utcnow())
|
self._update_internal_state(dt_util.utcnow())
|
||||||
|
|
||||||
@ -84,6 +79,18 @@ class TimeDateSensor(Entity):
|
|||||||
return "mdi:calendar"
|
return "mdi:calendar"
|
||||||
return "mdi:clock"
|
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):
|
def get_next_interval(self, now=None):
|
||||||
"""Compute next time an update should occur."""
|
"""Compute next time an update should occur."""
|
||||||
if now is None:
|
if now is None:
|
||||||
@ -137,6 +144,6 @@ class TimeDateSensor(Entity):
|
|||||||
"""Get the latest data and update state."""
|
"""Get the latest data and update state."""
|
||||||
self._update_internal_state(time_date)
|
self._update_internal_state(time_date)
|
||||||
self.async_write_ha_state()
|
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()
|
self.hass, self.point_in_time_listener, self.get_next_interval()
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user