From 06c8b838b5ebb36bcceb2785930f3bea871da7f6 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 5 Dec 2022 13:48:32 +0100 Subject: [PATCH] Use enum sensor device class in Season (#83301) --- homeassistant/components/season/sensor.py | 5 ++++- homeassistant/components/season/strings.json | 12 ++++++++++++ homeassistant/components/season/strings.sensor.json | 10 ---------- homeassistant/components/season/translations/en.json | 12 ++++++++++++ tests/components/season/test_sensor.py | 9 ++++++++- 5 files changed, 36 insertions(+), 12 deletions(-) delete mode 100644 homeassistant/components/season/strings.sensor.json diff --git a/homeassistant/components/season/sensor.py b/homeassistant/components/season/sensor.py index 8bb8773860d8..a568e51ed9d8 100644 --- a/homeassistant/components/season/sensor.py +++ b/homeassistant/components/season/sensor.py @@ -8,6 +8,7 @@ import voluptuous as vol from homeassistant.components.sensor import ( PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA, + SensorDeviceClass, SensorEntity, ) from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry @@ -132,8 +133,10 @@ def get_season( class SeasonSensorEntity(SensorEntity): """Representation of the current season.""" - _attr_device_class = "season__season" + _attr_device_class = SensorDeviceClass.ENUM _attr_has_entity_name = True + _attr_options = ["spring", "summer", "autumn", "winter"] + _attr_translation_key = "season" def __init__(self, entry: ConfigEntry, hemisphere: str) -> None: """Initialize the season.""" diff --git a/homeassistant/components/season/strings.json b/homeassistant/components/season/strings.json index 25d121d16e6e..bff02df5c6c2 100644 --- a/homeassistant/components/season/strings.json +++ b/homeassistant/components/season/strings.json @@ -16,5 +16,17 @@ "title": "The Season YAML configuration has been removed", "description": "Configuring Season using YAML has been removed.\n\nYour existing YAML configuration is not used by Home Assistant.\n\nRemove the YAML configuration from your configuration.yaml file and restart Home Assistant to fix this issue." } + }, + "entity": { + "sensor": { + "season": { + "state": { + "spring": "Spring", + "summer": "Summer", + "autumn": "Autumn", + "winter": "Winter" + } + } + } } } diff --git a/homeassistant/components/season/strings.sensor.json b/homeassistant/components/season/strings.sensor.json deleted file mode 100644 index 0407d99dee4a..000000000000 --- a/homeassistant/components/season/strings.sensor.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "state": { - "season__season": { - "spring": "Spring", - "summer": "Summer", - "autumn": "Autumn", - "winter": "Winter" - } - } -} diff --git a/homeassistant/components/season/translations/en.json b/homeassistant/components/season/translations/en.json index 5508c305fea2..79d23fabf404 100644 --- a/homeassistant/components/season/translations/en.json +++ b/homeassistant/components/season/translations/en.json @@ -11,6 +11,18 @@ } } }, + "entity": { + "sensor": { + "season": { + "state": { + "autumn": "Autumn", + "spring": "Spring", + "summer": "Summer", + "winter": "Winter" + } + } + } + }, "issues": { "removed_yaml": { "description": "Configuring Season using YAML has been removed.\n\nYour existing YAML configuration is not used by Home Assistant.\n\nRemove the YAML configuration from your configuration.yaml file and restart Home Assistant to fix this issue.", diff --git a/tests/components/season/test_sensor.py b/tests/components/season/test_sensor.py index 0c2470edb7bf..e2f4e6d4dccd 100644 --- a/tests/components/season/test_sensor.py +++ b/tests/components/season/test_sensor.py @@ -15,7 +15,8 @@ from homeassistant.components.season.sensor import ( STATE_SUMMER, STATE_WINTER, ) -from homeassistant.const import CONF_TYPE, STATE_UNKNOWN +from homeassistant.components.sensor import ATTR_OPTIONS, SensorDeviceClass +from homeassistant.const import ATTR_DEVICE_CLASS, CONF_TYPE, STATE_UNKNOWN from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er @@ -92,11 +93,14 @@ async def test_season_northern_hemisphere( state = hass.states.get("sensor.season") assert state assert state.state == expected + assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.ENUM + assert state.attributes[ATTR_OPTIONS] == ["spring", "summer", "autumn", "winter"] entity_registry = er.async_get(hass) entry = entity_registry.async_get("sensor.season") assert entry assert entry.unique_id == mock_config_entry.entry_id + assert entry.translation_key == "season" @pytest.mark.parametrize("type,day,expected", SOUTHERN_PARAMETERS, ids=idfn) @@ -121,11 +125,14 @@ async def test_season_southern_hemisphere( state = hass.states.get("sensor.season") assert state assert state.state == expected + assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.ENUM + assert state.attributes[ATTR_OPTIONS] == ["spring", "summer", "autumn", "winter"] entity_registry = er.async_get(hass) entry = entity_registry.async_get("sensor.season") assert entry assert entry.unique_id == mock_config_entry.entry_id + assert entry.translation_key == "season" device_registry = dr.async_get(hass) assert entry.device_id