diff --git a/custom_components/sun2/config.py b/custom_components/sun2/config.py index 05daea2..7f2176d 100644 --- a/custom_components/sun2/config.py +++ b/custom_components/sun2/config.py @@ -26,7 +26,7 @@ DOMAIN, SUNSET_ELEV, ) -from .helpers import LOC_PARAMS, Sun2Data +from .helpers import LOC_PARAMS, Sun2Data, translation from .sensor import val_tae_cfg, ELEVATION_AT_TIME_SCHEMA, TIME_AT_ELEVATION_SCHEMA PACKAGE_MERGE_HINT = "list" @@ -87,13 +87,6 @@ def _unique_locations_names(configs: list[dict]) -> list[dict]: ) -def _translation(hass: HomeAssistant, key: str) -> str: - """Sun2 translations.""" - return cast(Sun2Data, hass.data[DOMAIN]).translations[ - f"component.{DOMAIN}.misc.{key}" - ] - - def _val_bs_elevation(hass: HomeAssistant, config: str | ConfigType) -> ConfigType: """Validate elevation binary_sensor.""" if config[CONF_ELEVATION] == "horizon": @@ -102,15 +95,13 @@ def _val_bs_elevation(hass: HomeAssistant, config: str | ConfigType) -> ConfigTy if config.get(CONF_NAME): return config - if (elv := config[CONF_ELEVATION]) == DEFAULT_ELEVATION: - name = _translation(hass, "above_horizon") + if (elevation := config[CONF_ELEVATION]) == DEFAULT_ELEVATION: + name = translation(hass, "above_horizon") else: - above_str = _translation(hass, "above") - if elv < 0: - minus_str = _translation(hass, "minus") - name = f"{above_str} {minus_str} {-elv}" + if elevation < 0: + name = translation(hass, "above_neg_elev", {"elevation": str(-elevation)}) else: - name = f"{above_str} {elv}" + name = translation(hass, "above_pos_elev", {"elevation": str(elevation)}) config[CONF_NAME] = name return config @@ -120,9 +111,9 @@ def _val_eat_name(hass: HomeAssistant, config: str | ConfigType) -> ConfigType: if config.get(CONF_NAME): return config - config[ - CONF_NAME - ] = f"{_translation(hass, 'elevation_at')} {config[CONF_ELEVATION_AT_TIME]}" + config[CONF_NAME] = translation( + hass, "elevation_at", {"elev_time": str(config[CONF_ELEVATION_AT_TIME])} + ) return config @@ -135,11 +126,8 @@ def _val_tae_name(hass: HomeAssistant, config: str | ConfigType) -> ConfigType: direction = SunDirection(config[CONF_DIRECTION]) elevation = cast(float, config[CONF_TIME_AT_ELEVATION]) - if elevation >= 0: - elev_str = str(elevation) - else: - elev_str = f"{_translation(hass, 'minus')} {-elevation}" - config[CONF_NAME] = f"{_translation(hass, direction.name.lower())} at {elev_str} °" + trans_key = f"{direction.name.lower()}_{'neg' if elevation < 0 else 'pos'}_elev" + config[CONF_NAME] = translation(hass, trans_key, {"elevation": str(abs(elevation))}) return config diff --git a/custom_components/sun2/config_flow.py b/custom_components/sun2/config_flow.py index 14af27c..e236194 100644 --- a/custom_components/sun2/config_flow.py +++ b/custom_components/sun2/config_flow.py @@ -8,7 +8,7 @@ from homeassistant.data_entry_flow import FlowResult from .const import DOMAIN -from .helpers import Sun2Data +from .helpers import Sun2Data, translation class Sun2ConfigFlow(ConfigFlow, domain=DOMAIN): @@ -18,11 +18,11 @@ class Sun2ConfigFlow(ConfigFlow, domain=DOMAIN): async def async_step_import(self, data: dict[str, Any]) -> FlowResult: """Import config entry from configuration.""" - location_name = data.pop(CONF_LOCATION, self.hass.config.location_name) - service_name = cast(Sun2Data, self.hass.data[DOMAIN]).translations[ - f"component.{DOMAIN}.misc.service_name" - ] - title = f"{location_name} {service_name}" + title = translation( + self.hass, + "service_name", + {"location": data.pop(CONF_LOCATION, self.hass.config.location_name)}, + ) if existing_entry := await self.async_set_unique_id(data.pop(CONF_UNIQUE_ID)): self.hass.config_entries.async_update_entry( existing_entry, title=title, options=data diff --git a/custom_components/sun2/helpers.py b/custom_components/sun2/helpers.py index caa086f..ba4e17f 100644 --- a/custom_components/sun2/helpers.py +++ b/custom_components/sun2/helpers.py @@ -18,7 +18,7 @@ CONF_LONGITUDE, CONF_TIME_ZONE, ) -from homeassistant.core import CALLBACK_TYPE +from homeassistant.core import CALLBACK_TYPE, HomeAssistant from homeassistant.helpers import config_validation as cv from homeassistant.helpers.device_registry import DeviceEntryType @@ -109,6 +109,20 @@ def hours_to_hms(hours: Num | None) -> str | None: return None +def translation( + hass: HomeAssistant, key: str, placeholders: dict[str, str] | None = None +) -> str: + """Sun2 translations.""" + trans = cast(Sun2Data, hass.data[DOMAIN]).translations[ + f"component.{DOMAIN}.misc.{key}" + ] + if not placeholders: + return trans + for key, val in placeholders.items(): + trans = trans.replace(f"{{{key}}}", val) + return trans + + _Num = TypeVar("_Num", bound=Num) diff --git a/custom_components/sun2/translations/en.json b/custom_components/sun2/translations/en.json index 7f33cc4..316d998 100644 --- a/custom_components/sun2/translations/en.json +++ b/custom_components/sun2/translations/en.json @@ -1,13 +1,15 @@ { "title": "Sun2", "misc": { - "above": "Above", "above_horizon": "Above horizon", - "elevation_at": "Elevation at", - "minus": "minus", - "rising": "Rising", - "service_name": "Sun", - "setting": "Setting" + "above_neg_elev": "Above minus {elevation} °", + "above_pos_elev": "Above {elevation} °", + "elevation_at": "Elevation at {elev_time}", + "rising_neg_elev": "Rising at minus {elevation} °", + "rising_pos_elev": "Rising at {elevation} °", + "service_name": "{location} Sun", + "setting_neg_elev": "Setting at minus {elevation} °", + "setting_pos_elev": "Setting at {elevation} °" }, "entity": { "binary_sensor": { diff --git a/custom_components/sun2/translations/nl.json b/custom_components/sun2/translations/nl.json index 716aa54..3a68dd3 100644 --- a/custom_components/sun2/translations/nl.json +++ b/custom_components/sun2/translations/nl.json @@ -1,13 +1,15 @@ { "title": "Zon2", "misc": { - "above": "Boven", "above_horizon": "Boven horizon", - "elevation_at": "Hoogte bij", - "minus": "min", - "rising": "Zonsopkomst", - "service_name": "Zon", - "setting": "Zonsondergang" + "above_neg_elev": "Boven min {elevation} °", + "above_pos_elev": "Boven {elevation} °", + "elevation_at": "Hoogte bij {elev_time}", + "rising_neg_elev": "Stijgend bij min {elevation} °", + "rising_pos_elev": "Stijgend bij {elevation} °", + "service_name": "{location} Zon", + "setting_neg_elev": "Instelling bij min {elevation} °", + "setting_pos_elev": "Instelling bij {elevation} °" }, "entity": { "binary_sensor": {