From 2e422c5f0e5543272ba1d8b9528358f17d6e1f70 Mon Sep 17 00:00:00 2001 From: Phil Bruckner Date: Wed, 29 Nov 2023 13:05:38 -0600 Subject: [PATCH] Update names of additional entities when language is changed --- custom_components/sun2/__init__.py | 61 ++++++++++++++++++++++++--- custom_components/sun2/config_flow.py | 3 +- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/custom_components/sun2/__init__.py b/custom_components/sun2/__init__.py index 878d6a5..6288aff 100644 --- a/custom_components/sun2/__init__.py +++ b/custom_components/sun2/__init__.py @@ -2,11 +2,15 @@ from __future__ import annotations import asyncio -from typing import cast +from typing import Callable, cast from homeassistant.config_entries import ConfigEntry, SOURCE_IMPORT from homeassistant.const import ( + CONF_BINARY_SENSORS, + CONF_ELEVATION, CONF_LATITUDE, + CONF_NAME, + CONF_SENSORS, CONF_UNIQUE_ID, EVENT_CORE_CONFIG_UPDATE, Platform, @@ -18,7 +22,13 @@ from homeassistant.helpers.service import async_register_admin_service from homeassistant.helpers.typing import ConfigType -from .const import DOMAIN, SIG_HA_LOC_UPDATED +from .config import val_bs_elevation, val_elevation_at_time, val_time_at_elevation +from .const import ( + CONF_ELEVATION_AT_TIME, + CONF_TIME_AT_ELEVATION, + DOMAIN, + SIG_HA_LOC_UPDATED, +) from .helpers import LocData, LocParams, Sun2Data @@ -72,6 +82,26 @@ async def reload_config(call: ServiceCall | None = None) -> None: """Reload configuration.""" await process_config(await async_integration_yaml_config(hass, DOMAIN)) + def update_sensor_names( + options: ConfigType, + sensor_type: str, + validators: dict[str, Callable[[dict], dict]], + ) -> None: + """Update sensor names that were not specified by user.""" + if sensor_type not in options: + return + + sensors = [] + for sensor in options[sensor_type]: + if not sensor["user_named"]: + del sensor[CONF_NAME] + for key, validate in validators.items(): + if key in sensor: + sensor = validate(sensor) + break + sensors.append(sensor) + options[sensor_type] = sensors + async def handle_core_config_update(event: Event) -> None: """Handle core config update.""" if not event.data: @@ -88,10 +118,31 @@ async def handle_core_config_update(event: Event) -> None: for entry in hass.config_entries.async_entries(DOMAIN): if entry.source == SOURCE_IMPORT: continue + + kwargs = {} + if CONF_LATITUDE not in entry.options: - reload = not hass.config_entries.async_update_entry( - entry, title=hass.config.location_name - ) + kwargs["title"] = hass.config.location_name + + new_options = dict(entry.options) + update_sensor_names( + new_options, + CONF_BINARY_SENSORS, + {CONF_ELEVATION: val_bs_elevation(hass)}, + ) + update_sensor_names( + new_options, + CONF_SENSORS, + { + CONF_ELEVATION_AT_TIME: val_elevation_at_time(hass), + CONF_TIME_AT_ELEVATION: val_time_at_elevation(hass), + }, + ) + if new_options != entry.options: + kwargs["options"] = new_options + + if kwargs: + reload = not hass.config_entries.async_update_entry(entry, **kwargs) else: reload = True if reload: diff --git a/custom_components/sun2/config_flow.py b/custom_components/sun2/config_flow.py index fb61a90..5323a93 100644 --- a/custom_components/sun2/config_flow.py +++ b/custom_components/sun2/config_flow.py @@ -251,7 +251,8 @@ async def async_finish_sensor( sensor_type: str, ) -> FlowResult: """Finish elevation binary sensor.""" - sensor_option = validator(self.hass)(config) + sensor_option = {"user_named": CONF_NAME in config} + sensor_option.update(validator(self.hass)(config)) sensor_option[CONF_UNIQUE_ID] = random_uuid_hex() self.options.setdefault(sensor_type, []).append(sensor_option) return await self.async_step_entities_menu()