From 29da88d8f6ee7d11b548094eb41837a2b16686de Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Sun, 23 Jun 2024 20:55:27 +0300 Subject: [PATCH] Create a Jewish Calendar entity (#120253) * Set has_entity_name to true * Move common code to jewish calendar service entity * Remove already existing assignment * Move data to common entity * Remove description name * Use config entry title instead of name for the device * Address comments --- .../jewish_calendar/binary_sensor.py | 33 ++++----------- .../components/jewish_calendar/entity.py | 41 +++++++++++++++++++ .../components/jewish_calendar/sensor.py | 33 ++++----------- .../jewish_calendar/test_binary_sensor.py | 3 ++ .../components/jewish_calendar/test_sensor.py | 13 ++++-- 5 files changed, 68 insertions(+), 55 deletions(-) create mode 100644 homeassistant/components/jewish_calendar/entity.py diff --git a/homeassistant/components/jewish_calendar/binary_sensor.py b/homeassistant/components/jewish_calendar/binary_sensor.py index 54080fcefd8..060650ee25c 100644 --- a/homeassistant/components/jewish_calendar/binary_sensor.py +++ b/homeassistant/components/jewish_calendar/binary_sensor.py @@ -6,7 +6,6 @@ from collections.abc import Callable from dataclasses import dataclass import datetime as dt from datetime import datetime -from typing import Any import hdate from hdate.zmanim import Zmanim @@ -16,18 +15,14 @@ from homeassistant.components.binary_sensor import ( BinarySensorEntityDescription, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_LANGUAGE, CONF_LOCATION, EntityCategory +from homeassistant.const import EntityCategory from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback from homeassistant.helpers import event from homeassistant.helpers.entity_platform import AddEntitiesCallback import homeassistant.util.dt as dt_util -from .const import ( - CONF_CANDLE_LIGHT_MINUTES, - CONF_HAVDALAH_OFFSET_MINUTES, - DEFAULT_NAME, - DOMAIN, -) +from .const import DOMAIN +from .entity import JewishCalendarEntity @dataclass(frozen=True) @@ -75,33 +70,19 @@ async def async_setup_entry( entry = hass.data[DOMAIN][config_entry.entry_id] async_add_entities( - JewishCalendarBinarySensor(config_entry.entry_id, entry, description) + JewishCalendarBinarySensor(config_entry, entry, description) for description in BINARY_SENSORS ) -class JewishCalendarBinarySensor(BinarySensorEntity): +class JewishCalendarBinarySensor(JewishCalendarEntity, BinarySensorEntity): """Representation of an Jewish Calendar binary sensor.""" _attr_should_poll = False _attr_entity_category = EntityCategory.DIAGNOSTIC - entity_description: JewishCalendarBinarySensorEntityDescription + _update_unsub: CALLBACK_TYPE | None = None - def __init__( - self, - entry_id: str, - data: dict[str, Any], - description: JewishCalendarBinarySensorEntityDescription, - ) -> None: - """Initialize the binary sensor.""" - self.entity_description = description - self._attr_name = f"{DEFAULT_NAME} {description.name}" - self._attr_unique_id = f"{entry_id}-{description.key}" - self._location = data[CONF_LOCATION] - self._hebrew = data[CONF_LANGUAGE] == "hebrew" - self._candle_lighting_offset = data[CONF_CANDLE_LIGHT_MINUTES] - self._havdalah_offset = data[CONF_HAVDALAH_OFFSET_MINUTES] - self._update_unsub: CALLBACK_TYPE | None = None + entity_description: JewishCalendarBinarySensorEntityDescription @property def is_on(self) -> bool: diff --git a/homeassistant/components/jewish_calendar/entity.py b/homeassistant/components/jewish_calendar/entity.py new file mode 100644 index 00000000000..aba76599f63 --- /dev/null +++ b/homeassistant/components/jewish_calendar/entity.py @@ -0,0 +1,41 @@ +"""Entity representing a Jewish Calendar sensor.""" + +from typing import Any + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_LANGUAGE, CONF_LOCATION +from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo +from homeassistant.helpers.entity import Entity, EntityDescription + +from .const import ( + CONF_CANDLE_LIGHT_MINUTES, + CONF_DIASPORA, + CONF_HAVDALAH_OFFSET_MINUTES, + DOMAIN, +) + + +class JewishCalendarEntity(Entity): + """An HA implementation for Jewish Calendar entity.""" + + _attr_has_entity_name = True + + def __init__( + self, + config_entry: ConfigEntry, + data: dict[str, Any], + description: EntityDescription, + ) -> None: + """Initialize a Jewish Calendar entity.""" + self.entity_description = description + self._attr_unique_id = f"{config_entry.entry_id}_{description.key}" + self._attr_device_info = DeviceInfo( + entry_type=DeviceEntryType.SERVICE, + identifiers={(DOMAIN, config_entry.entry_id)}, + name=config_entry.title, + ) + self._location = data[CONF_LOCATION] + self._hebrew = data[CONF_LANGUAGE] == "hebrew" + self._candle_lighting_offset = data[CONF_CANDLE_LIGHT_MINUTES] + self._havdalah_offset = data[CONF_HAVDALAH_OFFSET_MINUTES] + self._diaspora = data[CONF_DIASPORA] diff --git a/homeassistant/components/jewish_calendar/sensor.py b/homeassistant/components/jewish_calendar/sensor.py index 02a5da27119..87b4375b8b2 100644 --- a/homeassistant/components/jewish_calendar/sensor.py +++ b/homeassistant/components/jewish_calendar/sensor.py @@ -15,24 +15,14 @@ from homeassistant.components.sensor import ( SensorEntityDescription, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ( - CONF_LANGUAGE, - CONF_LOCATION, - SUN_EVENT_SUNSET, - EntityCategory, -) +from homeassistant.const import SUN_EVENT_SUNSET, EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.sun import get_astral_event_date import homeassistant.util.dt as dt_util -from .const import ( - CONF_CANDLE_LIGHT_MINUTES, - CONF_DIASPORA, - CONF_HAVDALAH_OFFSET_MINUTES, - DEFAULT_NAME, - DOMAIN, -) +from .const import DOMAIN +from .entity import JewishCalendarEntity _LOGGER = logging.getLogger(__name__) @@ -185,37 +175,30 @@ async def async_setup_entry( """Set up the Jewish calendar sensors .""" entry = hass.data[DOMAIN][config_entry.entry_id] sensors = [ - JewishCalendarSensor(config_entry.entry_id, entry, description) + JewishCalendarSensor(config_entry, entry, description) for description in INFO_SENSORS ] sensors.extend( - JewishCalendarTimeSensor(config_entry.entry_id, entry, description) + JewishCalendarTimeSensor(config_entry, entry, description) for description in TIME_SENSORS ) async_add_entities(sensors) -class JewishCalendarSensor(SensorEntity): +class JewishCalendarSensor(JewishCalendarEntity, SensorEntity): """Representation of an Jewish calendar sensor.""" _attr_entity_category = EntityCategory.DIAGNOSTIC def __init__( self, - entry_id: str, + config_entry: ConfigEntry, data: dict[str, Any], description: SensorEntityDescription, ) -> None: """Initialize the Jewish calendar sensor.""" - self.entity_description = description - self._attr_name = f"{DEFAULT_NAME} {description.name}" - self._attr_unique_id = f"{entry_id}-{description.key}" - self._location = data[CONF_LOCATION] - self._hebrew = data[CONF_LANGUAGE] == "hebrew" - self._candle_lighting_offset = data[CONF_CANDLE_LIGHT_MINUTES] - self._havdalah_offset = data[CONF_HAVDALAH_OFFSET_MINUTES] - self._diaspora = data[CONF_DIASPORA] + super().__init__(config_entry, data, description) self._attrs: dict[str, str] = {} async def async_update(self) -> None: diff --git a/tests/components/jewish_calendar/test_binary_sensor.py b/tests/components/jewish_calendar/test_binary_sensor.py index b60e7698266..8abaaecb77d 100644 --- a/tests/components/jewish_calendar/test_binary_sensor.py +++ b/tests/components/jewish_calendar/test_binary_sensor.py @@ -10,6 +10,7 @@ from homeassistant.components.jewish_calendar.const import ( CONF_CANDLE_LIGHT_MINUTES, CONF_DIASPORA, CONF_HAVDALAH_OFFSET_MINUTES, + DEFAULT_NAME, DOMAIN, ) from homeassistant.const import CONF_LANGUAGE, CONF_PLATFORM, STATE_OFF, STATE_ON @@ -192,6 +193,7 @@ async def test_issur_melacha_sensor( with alter_time(test_time): entry = MockConfigEntry( + title=DEFAULT_NAME, domain=DOMAIN, data={ CONF_LANGUAGE: "english", @@ -264,6 +266,7 @@ async def test_issur_melacha_sensor_update( with alter_time(test_time): entry = MockConfigEntry( + title=DEFAULT_NAME, domain=DOMAIN, data={ CONF_LANGUAGE: "english", diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index e2f7cf25244..cb054751f67 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -10,6 +10,7 @@ from homeassistant.components.jewish_calendar.const import ( CONF_CANDLE_LIGHT_MINUTES, CONF_DIASPORA, CONF_HAVDALAH_OFFSET_MINUTES, + DEFAULT_NAME, DOMAIN, ) from homeassistant.const import CONF_LANGUAGE, CONF_PLATFORM @@ -24,7 +25,7 @@ from tests.common import MockConfigEntry, async_fire_time_changed async def test_jewish_calendar_min_config(hass: HomeAssistant) -> None: """Test minimum jewish calendar configuration.""" - entry = MockConfigEntry(domain=DOMAIN, data={}) + entry = MockConfigEntry(title=DEFAULT_NAME, domain=DOMAIN, data={}) entry.add_to_hass(hass) await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() @@ -33,7 +34,9 @@ async def test_jewish_calendar_min_config(hass: HomeAssistant) -> None: async def test_jewish_calendar_hebrew(hass: HomeAssistant) -> None: """Test jewish calendar sensor with language set to hebrew.""" - entry = MockConfigEntry(domain=DOMAIN, data={"language": "hebrew"}) + entry = MockConfigEntry( + title=DEFAULT_NAME, domain=DOMAIN, data={"language": "hebrew"} + ) entry.add_to_hass(hass) await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() @@ -224,6 +227,7 @@ async def test_jewish_calendar_sensor( with alter_time(test_time): entry = MockConfigEntry( + title=DEFAULT_NAME, domain=DOMAIN, data={ CONF_LANGUAGE: language, @@ -565,6 +569,7 @@ async def test_shabbat_times_sensor( with alter_time(test_time): entry = MockConfigEntry( + title=DEFAULT_NAME, domain=DOMAIN, data={ CONF_LANGUAGE: language, @@ -625,7 +630,7 @@ async def test_omer_sensor(hass: HomeAssistant, test_time, result) -> None: test_time = test_time.replace(tzinfo=dt_util.get_time_zone(hass.config.time_zone)) with alter_time(test_time): - entry = MockConfigEntry(domain=DOMAIN) + entry = MockConfigEntry(title=DEFAULT_NAME, domain=DOMAIN) entry.add_to_hass(hass) await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() @@ -660,7 +665,7 @@ async def test_dafyomi_sensor(hass: HomeAssistant, test_time, result) -> None: test_time = test_time.replace(tzinfo=dt_util.get_time_zone(hass.config.time_zone)) with alter_time(test_time): - entry = MockConfigEntry(domain=DOMAIN) + entry = MockConfigEntry(title=DEFAULT_NAME, domain=DOMAIN) entry.add_to_hass(hass) await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done()