From 06fd6442b6a7df1f0e076852c68b6077126f6287 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Sat, 8 Mar 2025 20:03:25 +0100 Subject: [PATCH] Use the set language for condition sensors in Accuweather integration (#140107) * Use the set language for condition sensors * Update strings * Update test snapshots * Add missing string --- homeassistant/components/accuweather/const.py | 14 +++- .../components/accuweather/coordinator.py | 4 +- .../components/accuweather/sensor.py | 28 +++++--- .../components/accuweather/strings.json | 72 +++++++++---------- .../accuweather/snapshots/test_sensor.ambr | 40 +++-------- 5 files changed, 80 insertions(+), 78 deletions(-) diff --git a/homeassistant/components/accuweather/const.py b/homeassistant/components/accuweather/const.py index 1bbf5a36187..f09b9771ab6 100644 --- a/homeassistant/components/accuweather/const.py +++ b/homeassistant/components/accuweather/const.py @@ -24,7 +24,7 @@ from homeassistant.components.weather import ( API_METRIC: Final = "Metric" ATTRIBUTION: Final = "Data provided by AccuWeather" -ATTR_CATEGORY: Final = "Category" +ATTR_CATEGORY_VALUE = "CategoryValue" ATTR_DIRECTION: Final = "Direction" ATTR_ENGLISH: Final = "English" ATTR_LEVEL: Final = "level" @@ -55,5 +55,17 @@ CONDITION_MAP = { for cond_ha, cond_codes in CONDITION_CLASSES.items() for cond_code in cond_codes } +AIR_QUALITY_CATEGORY_MAP = { + 1: "good", + 2: "moderate", + 3: "unhealthy", + 4: "hazardous", +} +POLLEN_CATEGORY_MAP = { + 1: "low", + 2: "moderate", + 3: "high", + 4: "very high", +} UPDATE_INTERVAL_OBSERVATION = timedelta(minutes=40) UPDATE_INTERVAL_DAILY_FORECAST = timedelta(hours=6) diff --git a/homeassistant/components/accuweather/coordinator.py b/homeassistant/components/accuweather/coordinator.py index 40ff3ad2c87..67e3e2ad76e 100644 --- a/homeassistant/components/accuweather/coordinator.py +++ b/homeassistant/components/accuweather/coordinator.py @@ -117,7 +117,9 @@ class AccuWeatherDailyForecastDataUpdateCoordinator( """Update data via library.""" try: async with timeout(10): - result = await self.accuweather.async_get_daily_forecast() + result = await self.accuweather.async_get_daily_forecast( + language=self.hass.config.language + ) except EXCEPTIONS as error: raise UpdateFailed(error) from error diff --git a/homeassistant/components/accuweather/sensor.py b/homeassistant/components/accuweather/sensor.py index f14584cf08c..415df402d55 100644 --- a/homeassistant/components/accuweather/sensor.py +++ b/homeassistant/components/accuweather/sensor.py @@ -29,8 +29,9 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import ( + AIR_QUALITY_CATEGORY_MAP, API_METRIC, - ATTR_CATEGORY, + ATTR_CATEGORY_VALUE, ATTR_DIRECTION, ATTR_ENGLISH, ATTR_LEVEL, @@ -38,6 +39,7 @@ from .const import ( ATTR_VALUE, ATTRIBUTION, MAX_FORECAST_DAYS, + POLLEN_CATEGORY_MAP, ) from .coordinator import ( AccuWeatherConfigEntry, @@ -59,9 +61,9 @@ class AccuWeatherSensorDescription(SensorEntityDescription): FORECAST_SENSOR_TYPES: tuple[AccuWeatherSensorDescription, ...] = ( AccuWeatherSensorDescription( key="AirQuality", - value_fn=lambda data: cast(str, data[ATTR_CATEGORY]), + value_fn=lambda data: AIR_QUALITY_CATEGORY_MAP[data[ATTR_CATEGORY_VALUE]], device_class=SensorDeviceClass.ENUM, - options=["good", "hazardous", "high", "low", "moderate", "unhealthy"], + options=list(AIR_QUALITY_CATEGORY_MAP.values()), translation_key="air_quality", ), AccuWeatherSensorDescription( @@ -83,7 +85,9 @@ FORECAST_SENSOR_TYPES: tuple[AccuWeatherSensorDescription, ...] = ( entity_registry_enabled_default=False, native_unit_of_measurement=CONCENTRATION_PARTS_PER_CUBIC_METER, value_fn=lambda data: cast(int, data[ATTR_VALUE]), - attr_fn=lambda data: {ATTR_LEVEL: data[ATTR_CATEGORY]}, + attr_fn=lambda data: { + ATTR_LEVEL: POLLEN_CATEGORY_MAP[data[ATTR_CATEGORY_VALUE]] + }, translation_key="grass_pollen", ), AccuWeatherSensorDescription( @@ -107,7 +111,9 @@ FORECAST_SENSOR_TYPES: tuple[AccuWeatherSensorDescription, ...] = ( entity_registry_enabled_default=False, native_unit_of_measurement=CONCENTRATION_PARTS_PER_CUBIC_METER, value_fn=lambda data: cast(int, data[ATTR_VALUE]), - attr_fn=lambda data: {ATTR_LEVEL: data[ATTR_CATEGORY]}, + attr_fn=lambda data: { + ATTR_LEVEL: POLLEN_CATEGORY_MAP[data[ATTR_CATEGORY_VALUE]] + }, translation_key="mold_pollen", ), AccuWeatherSensorDescription( @@ -115,7 +121,9 @@ FORECAST_SENSOR_TYPES: tuple[AccuWeatherSensorDescription, ...] = ( native_unit_of_measurement=CONCENTRATION_PARTS_PER_CUBIC_METER, entity_registry_enabled_default=False, value_fn=lambda data: cast(int, data[ATTR_VALUE]), - attr_fn=lambda data: {ATTR_LEVEL: data[ATTR_CATEGORY]}, + attr_fn=lambda data: { + ATTR_LEVEL: POLLEN_CATEGORY_MAP[data[ATTR_CATEGORY_VALUE]] + }, translation_key="ragweed_pollen", ), AccuWeatherSensorDescription( @@ -181,14 +189,18 @@ FORECAST_SENSOR_TYPES: tuple[AccuWeatherSensorDescription, ...] = ( native_unit_of_measurement=CONCENTRATION_PARTS_PER_CUBIC_METER, entity_registry_enabled_default=False, value_fn=lambda data: cast(int, data[ATTR_VALUE]), - attr_fn=lambda data: {ATTR_LEVEL: data[ATTR_CATEGORY]}, + attr_fn=lambda data: { + ATTR_LEVEL: POLLEN_CATEGORY_MAP[data[ATTR_CATEGORY_VALUE]] + }, translation_key="tree_pollen", ), AccuWeatherSensorDescription( key="UVIndex", native_unit_of_measurement=UV_INDEX, value_fn=lambda data: cast(int, data[ATTR_VALUE]), - attr_fn=lambda data: {ATTR_LEVEL: data[ATTR_CATEGORY]}, + attr_fn=lambda data: { + ATTR_LEVEL: POLLEN_CATEGORY_MAP[data[ATTR_CATEGORY_VALUE]] + }, translation_key="uv_index_forecast", ), AccuWeatherSensorDescription( diff --git a/homeassistant/components/accuweather/strings.json b/homeassistant/components/accuweather/strings.json index d0250a382e9..e5190b7a8da 100644 --- a/homeassistant/components/accuweather/strings.json +++ b/homeassistant/components/accuweather/strings.json @@ -26,10 +26,18 @@ "state": { "good": "Good", "hazardous": "Hazardous", - "high": "High", - "low": "Low", "moderate": "Moderate", "unhealthy": "Unhealthy" + }, + "state_attributes": { + "options": { + "state": { + "good": "[%key:component::accuweather::entity::sensor::air_quality::state::good%]", + "hazardous": "[%key:component::accuweather::entity::sensor::air_quality::state::hazardous%]", + "moderate": "[%key:component::accuweather::entity::sensor::air_quality::state::moderate%]", + "unhealthy": "[%key:component::accuweather::entity::sensor::air_quality::state::unhealthy%]" + } + } } }, "apparent_temperature": { @@ -62,12 +70,10 @@ "level": { "name": "Level", "state": { - "good": "[%key:component::accuweather::entity::sensor::air_quality::state::good%]", - "hazardous": "[%key:component::accuweather::entity::sensor::air_quality::state::hazardous%]", - "high": "[%key:component::accuweather::entity::sensor::air_quality::state::high%]", - "low": "[%key:component::accuweather::entity::sensor::air_quality::state::low%]", - "moderate": "[%key:component::accuweather::entity::sensor::air_quality::state::moderate%]", - "unhealthy": "[%key:component::accuweather::entity::sensor::air_quality::state::unhealthy%]" + "high": "High", + "low": "Low", + "moderate": "Moderate", + "very_high": "Very high" } } } @@ -81,12 +87,10 @@ "level": { "name": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::name%]", "state": { - "good": "[%key:component::accuweather::entity::sensor::air_quality::state::good%]", - "hazardous": "[%key:component::accuweather::entity::sensor::air_quality::state::hazardous%]", - "high": "[%key:component::accuweather::entity::sensor::air_quality::state::high%]", - "low": "[%key:component::accuweather::entity::sensor::air_quality::state::low%]", - "moderate": "[%key:component::accuweather::entity::sensor::air_quality::state::moderate%]", - "unhealthy": "[%key:component::accuweather::entity::sensor::air_quality::state::unhealthy%]" + "high": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::high%]", + "low": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::low%]", + "moderate": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::moderate%]", + "very_high": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::very_high%]" } } } @@ -108,12 +112,10 @@ "level": { "name": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::name%]", "state": { - "good": "[%key:component::accuweather::entity::sensor::air_quality::state::good%]", - "hazardous": "[%key:component::accuweather::entity::sensor::air_quality::state::hazardous%]", - "high": "[%key:component::accuweather::entity::sensor::air_quality::state::high%]", - "low": "[%key:component::accuweather::entity::sensor::air_quality::state::low%]", - "moderate": "[%key:component::accuweather::entity::sensor::air_quality::state::moderate%]", - "unhealthy": "[%key:component::accuweather::entity::sensor::air_quality::state::unhealthy%]" + "high": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::high%]", + "low": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::low%]", + "moderate": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::moderate%]", + "very_high": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::very_high%]" } } } @@ -154,12 +156,10 @@ "level": { "name": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::name%]", "state": { - "good": "[%key:component::accuweather::entity::sensor::air_quality::state::good%]", - "hazardous": "[%key:component::accuweather::entity::sensor::air_quality::state::hazardous%]", - "high": "[%key:component::accuweather::entity::sensor::air_quality::state::high%]", - "low": "[%key:component::accuweather::entity::sensor::air_quality::state::low%]", - "moderate": "[%key:component::accuweather::entity::sensor::air_quality::state::moderate%]", - "unhealthy": "[%key:component::accuweather::entity::sensor::air_quality::state::unhealthy%]" + "high": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::high%]", + "low": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::low%]", + "moderate": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::moderate%]", + "very_high": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::very_high%]" } } } @@ -170,12 +170,10 @@ "level": { "name": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::name%]", "state": { - "good": "[%key:component::accuweather::entity::sensor::air_quality::state::good%]", - "hazardous": "[%key:component::accuweather::entity::sensor::air_quality::state::hazardous%]", - "high": "[%key:component::accuweather::entity::sensor::air_quality::state::high%]", - "low": "[%key:component::accuweather::entity::sensor::air_quality::state::low%]", - "moderate": "[%key:component::accuweather::entity::sensor::air_quality::state::moderate%]", - "unhealthy": "[%key:component::accuweather::entity::sensor::air_quality::state::unhealthy%]" + "high": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::high%]", + "low": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::low%]", + "moderate": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::moderate%]", + "very_high": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::very_high%]" } } } @@ -186,12 +184,10 @@ "level": { "name": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::name%]", "state": { - "good": "[%key:component::accuweather::entity::sensor::air_quality::state::good%]", - "hazardous": "[%key:component::accuweather::entity::sensor::air_quality::state::hazardous%]", - "high": "[%key:component::accuweather::entity::sensor::air_quality::state::high%]", - "low": "[%key:component::accuweather::entity::sensor::air_quality::state::low%]", - "moderate": "[%key:component::accuweather::entity::sensor::air_quality::state::moderate%]", - "unhealthy": "[%key:component::accuweather::entity::sensor::air_quality::state::unhealthy%]" + "high": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::high%]", + "low": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::low%]", + "moderate": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::moderate%]", + "very_high": "[%key:component::accuweather::entity::sensor::grass_pollen::state_attributes::level::state::very_high%]" } } } diff --git a/tests/components/accuweather/snapshots/test_sensor.ambr b/tests/components/accuweather/snapshots/test_sensor.ambr index 257d29ae844..3176f0a88bd 100644 --- a/tests/components/accuweather/snapshots/test_sensor.ambr +++ b/tests/components/accuweather/snapshots/test_sensor.ambr @@ -7,11 +7,9 @@ 'capabilities': dict({ 'options': list([ 'good', - 'hazardous', - 'high', - 'low', 'moderate', 'unhealthy', + 'hazardous', ]), }), 'config_entry_id': , @@ -50,11 +48,9 @@ 'friendly_name': 'Home Air quality day 0', 'options': list([ 'good', - 'hazardous', - 'high', - 'low', 'moderate', 'unhealthy', + 'hazardous', ]), }), 'context': , @@ -73,11 +69,9 @@ 'capabilities': dict({ 'options': list([ 'good', - 'hazardous', - 'high', - 'low', 'moderate', 'unhealthy', + 'hazardous', ]), }), 'config_entry_id': , @@ -116,11 +110,9 @@ 'friendly_name': 'Home Air quality day 1', 'options': list([ 'good', - 'hazardous', - 'high', - 'low', 'moderate', 'unhealthy', + 'hazardous', ]), }), 'context': , @@ -139,11 +131,9 @@ 'capabilities': dict({ 'options': list([ 'good', - 'hazardous', - 'high', - 'low', 'moderate', 'unhealthy', + 'hazardous', ]), }), 'config_entry_id': , @@ -182,11 +172,9 @@ 'friendly_name': 'Home Air quality day 2', 'options': list([ 'good', - 'hazardous', - 'high', - 'low', 'moderate', 'unhealthy', + 'hazardous', ]), }), 'context': , @@ -205,11 +193,9 @@ 'capabilities': dict({ 'options': list([ 'good', - 'hazardous', - 'high', - 'low', 'moderate', 'unhealthy', + 'hazardous', ]), }), 'config_entry_id': , @@ -248,11 +234,9 @@ 'friendly_name': 'Home Air quality day 3', 'options': list([ 'good', - 'hazardous', - 'high', - 'low', 'moderate', 'unhealthy', + 'hazardous', ]), }), 'context': , @@ -271,11 +255,9 @@ 'capabilities': dict({ 'options': list([ 'good', - 'hazardous', - 'high', - 'low', 'moderate', 'unhealthy', + 'hazardous', ]), }), 'config_entry_id': , @@ -314,11 +296,9 @@ 'friendly_name': 'Home Air quality day 4', 'options': list([ 'good', - 'hazardous', - 'high', - 'low', 'moderate', 'unhealthy', + 'hazardous', ]), }), 'context': ,