diff --git a/homeassistant/components/zodiac/sensor.py b/homeassistant/components/zodiac/sensor.py index b337dda1db0..f63c844701f 100644 --- a/homeassistant/components/zodiac/sensor.py +++ b/homeassistant/components/zodiac/sensor.py @@ -1,7 +1,7 @@ """Support for tracking the zodiac sign.""" from __future__ import annotations -from homeassistant.components.sensor import SensorEntity +from homeassistant.components.sensor import SensorDeviceClass, SensorEntity from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType @@ -175,52 +175,34 @@ async def async_setup_platform( class ZodiacSensor(SensorEntity): """Representation of a Zodiac sensor.""" - def __init__(self) -> None: - """Initialize the zodiac sensor.""" - self._attrs: dict[str, str] = {} - self._state: str = "" - - @property - def unique_id(self) -> str: - """Return a unique ID.""" - return DOMAIN - - @property - def name(self) -> str: - """Return the name of the entity.""" - return "Zodiac" - - @property - def device_class(self) -> str: - """Return the device class of the entity.""" - return "zodiac__sign" - - @property - def native_value(self) -> str: - """Return the state of the device.""" - return self._state - - @property - def icon(self) -> str | None: - """Icon to use in the frontend.""" - return ZODIAC_ICONS.get(self._state) - - @property - def extra_state_attributes(self) -> dict[str, str]: - """Return the state attributes.""" - return self._attrs + _attr_name = "Zodiac" + _attr_device_class = SensorDeviceClass.ENUM + _attr_options = [ + SIGN_AQUARIUS, + SIGN_ARIES, + SIGN_CANCER, + SIGN_CAPRICORN, + SIGN_GEMINI, + SIGN_LEO, + SIGN_LIBRA, + SIGN_PISCES, + SIGN_SAGITTARIUS, + SIGN_SCORPIO, + SIGN_TAURUS, + SIGN_VIRGO, + ] + _attr_translation_key = "sign" + _attr_unique_id = DOMAIN async def async_update(self) -> None: """Get the time and updates the state.""" today = as_local(utcnow()).date() - month = int(today.month) - day = int(today.day) - for sign in ZODIAC_BY_DATE: - if (month == sign[0][1] and day >= sign[0][0]) or ( - month == sign[1][1] and day <= sign[1][0] + if (today.month == sign[0][1] and today.day >= sign[0][0]) or ( + today.month == sign[1][1] and today.day <= sign[1][0] ): - self._state = sign[2] - self._attrs = sign[3] + self._attr_native_value = sign[2] + self._attr_icon = ZODIAC_ICONS.get(sign[2]) + self._attr_extra_state_attributes = sign[3] break diff --git a/homeassistant/components/zodiac/strings.json b/homeassistant/components/zodiac/strings.json new file mode 100644 index 00000000000..cbae6ead433 --- /dev/null +++ b/homeassistant/components/zodiac/strings.json @@ -0,0 +1,22 @@ +{ + "entity": { + "sensor": { + "sign": { + "state": { + "aquarius": "Aquarius", + "aries": "Aries", + "cancer": "Cancer", + "capricorn": "Capricorn", + "gemini": "Gemini", + "leo": "Leo", + "libra": "Libra", + "pisces": "Pisces", + "sagittarius": "Sagittarius", + "scorpio": "Scorpio", + "taurus": "Taurus", + "virgo": "Virgo" + } + } + } + } +} diff --git a/homeassistant/components/zodiac/strings.sensor.json b/homeassistant/components/zodiac/strings.sensor.json deleted file mode 100644 index fec38fe2147..00000000000 --- a/homeassistant/components/zodiac/strings.sensor.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "state": { - "zodiac__sign": { - "aries": "Aries", - "taurus": "Taurus", - "gemini": "Gemini", - "cancer": "Cancer", - "leo": "Leo", - "virgo": "Virgo", - "libra": "Libra", - "scorpio": "Scorpio", - "sagittarius": "Sagittarius", - "capricorn": "Capricorn", - "aquarius": "Aquarius", - "pisces": "Pisces" - } - } -} diff --git a/homeassistant/components/zodiac/translations/en.json b/homeassistant/components/zodiac/translations/en.json new file mode 100644 index 00000000000..c25206cc0aa --- /dev/null +++ b/homeassistant/components/zodiac/translations/en.json @@ -0,0 +1,22 @@ +{ + "entity": { + "sensor": { + "sign": { + "state": { + "aquarius": "Aquarius", + "aries": "Aries", + "cancer": "Cancer", + "capricorn": "Capricorn", + "gemini": "Gemini", + "leo": "Leo", + "libra": "Libra", + "pisces": "Pisces", + "sagittarius": "Sagittarius", + "scorpio": "Scorpio", + "taurus": "Taurus", + "virgo": "Virgo" + } + } + } + } +} \ No newline at end of file diff --git a/tests/components/zodiac/test_sensor.py b/tests/components/zodiac/test_sensor.py index 90b19e73b04..b0894dfde7d 100644 --- a/tests/components/zodiac/test_sensor.py +++ b/tests/components/zodiac/test_sensor.py @@ -4,6 +4,7 @@ from unittest.mock import patch import pytest +from homeassistant.components.sensor import ATTR_OPTIONS, SensorDeviceClass from homeassistant.components.zodiac.const import ( ATTR_ELEMENT, ATTR_MODALITY, @@ -17,6 +18,8 @@ from homeassistant.components.zodiac.const import ( SIGN_SCORPIO, SIGN_TAURUS, ) +from homeassistant.const import ATTR_DEVICE_CLASS +from homeassistant.helpers import entity_registry as er from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util @@ -48,3 +51,24 @@ async def test_zodiac_day(hass, now, sign, element, modality): assert state.attributes assert state.attributes[ATTR_ELEMENT] == element assert state.attributes[ATTR_MODALITY] == modality + assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.ENUM + assert state.attributes[ATTR_OPTIONS] == [ + "aquarius", + "aries", + "cancer", + "capricorn", + "gemini", + "leo", + "libra", + "pisces", + "sagittarius", + "scorpio", + "taurus", + "virgo", + ] + + entity_registry = er.async_get(hass) + entry = entity_registry.async_get("sensor.zodiac") + assert entry + assert entry.unique_id == "zodiac" + assert entry.translation_key == "sign"