diff --git a/custom_components/sun2/__init__.py b/custom_components/sun2/__init__.py index ab28b9d..028dc05 100644 --- a/custom_components/sun2/__init__.py +++ b/custom_components/sun2/__init__.py @@ -9,15 +9,17 @@ CONF_LOCATION, CONF_SENSORS, CONF_UNIQUE_ID, + EVENT_CORE_CONFIG_UPDATE, Platform, ) -from homeassistant.core import HomeAssistant +from homeassistant.core import Event, HomeAssistant import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.dispatcher import dispatcher_send from homeassistant.helpers.typing import ConfigType from .binary_sensor import SUN2_BINARY_SENSOR_SCHEMA -from .const import DOMAIN -from .helpers import LOC_PARAMS +from .const import DOMAIN, SIG_HA_LOC_UPDATED +from .helpers import LOC_PARAMS, LocData, LocParams from .sensor import ELEVATION_AT_TIME_SCHEMA, TIME_AT_ELEVATION_SCHEMA @@ -58,6 +60,25 @@ def _unique_locations_names(configs: list[dict]) -> list[dict]: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Setup composite integration.""" + hass.data[DOMAIN] = {} + + def update_local_loc_data(event: Event | None = None) -> None: + """Update local location data from HA's config.""" + hass.data[DOMAIN][None] = loc_data = LocData( + LocParams( + hass.config.elevation, + hass.config.latitude, + hass.config.longitude, + str(hass.config.time_zone), + ) + ) + if event: + # Signal all instances that location data has changed. + dispatcher_send(hass, SIG_HA_LOC_UPDATED, loc_data) + + update_local_loc_data(hass) + hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, update_local_loc_data) + for conf in config[DOMAIN]: hass.async_create_task( hass.config_entries.flow.async_init( diff --git a/custom_components/sun2/helpers.py b/custom_components/sun2/helpers.py index 6d8c76c..e14a96a 100644 --- a/custom_components/sun2/helpers.py +++ b/custom_components/sun2/helpers.py @@ -5,7 +5,6 @@ from collections.abc import Mapping from dataclasses import dataclass from datetime import date, datetime, time, timedelta, tzinfo -from functools import partial from typing import Any, TypeVar, Union, cast from astral import LocationInfo @@ -18,9 +17,8 @@ CONF_LATITUDE, CONF_LONGITUDE, CONF_TIME_ZONE, - EVENT_CORE_CONFIG_UPDATE, ) -from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant +from homeassistant.core import CALLBACK_TYPE from homeassistant.helpers import config_validation as cv from homeassistant.helpers.device_registry import DeviceEntryType @@ -29,10 +27,7 @@ from homeassistant.helpers.device_registry import DeviceInfo except ImportError: from homeassistant.helpers.entity import DeviceInfo -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - dispatcher_send, -) +from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import Entity from homeassistant.helpers.typing import ConfigType from homeassistant.util import dt as dt_util @@ -166,30 +161,6 @@ def _get_loc_data(self) -> LocData: loc_params = None -> Use location parameters from HA's config. """ - if DOMAIN not in self.hass.data: - self.hass.data[DOMAIN] = {} - - def update_local_loc_data( - hass: HomeAssistant, event: Event | None = None - ) -> None: - """Update local location data from HA's config.""" - hass.data[DOMAIN][None] = loc_data = LocData( - LocParams( - hass.config.elevation, - hass.config.latitude, - hass.config.longitude, - str(hass.config.time_zone), - ) - ) - if event: - # Signal all instances that location data has changed. - dispatcher_send(hass, SIG_HA_LOC_UPDATED, loc_data) - - update_local_loc_data(self.hass) - self.hass.bus.async_listen( - EVENT_CORE_CONFIG_UPDATE, partial(update_local_loc_data, self.hass) - ) - try: loc_data = cast(LocData, self.hass.data[DOMAIN][self._loc_params]) except KeyError: