Skip to content

Commit

Permalink
Move processing of HA location config to __init__
Browse files Browse the repository at this point in the history
  • Loading branch information
pnbruckner committed Nov 14, 2023
1 parent 643bf3e commit 927c165
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
27 changes: 24 additions & 3 deletions custom_components/sun2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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.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(
Expand Down
33 changes: 2 additions & 31 deletions custom_components/sun2/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 927c165

Please sign in to comment.