Move Jewish Calendar to runtime data (#129609)
This commit is contained in:
parent
96de4b3828
commit
57d1001603
4 changed files with 52 additions and 54 deletions
|
@ -7,12 +7,11 @@ from functools import partial
|
|||
from hdate import Location
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
from homeassistant.config_entries import SOURCE_IMPORT
|
||||
from homeassistant.const import (
|
||||
CONF_ELEVATION,
|
||||
CONF_LANGUAGE,
|
||||
CONF_LATITUDE,
|
||||
CONF_LOCATION,
|
||||
CONF_LONGITUDE,
|
||||
CONF_NAME,
|
||||
CONF_TIME_ZONE,
|
||||
|
@ -36,6 +35,7 @@ from .const import (
|
|||
DEFAULT_NAME,
|
||||
DOMAIN,
|
||||
)
|
||||
from .entity import JewishCalendarConfigEntry, JewishCalendarData
|
||||
from .sensor import INFO_SENSORS, TIME_SENSORS
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||
|
@ -120,7 +120,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, config_entry: JewishCalendarConfigEntry
|
||||
) -> bool:
|
||||
"""Set up a configuration entry for Jewish calendar."""
|
||||
language = config_entry.data.get(CONF_LANGUAGE, DEFAULT_LANGUAGE)
|
||||
diaspora = config_entry.data.get(CONF_DIASPORA, DEFAULT_DIASPORA)
|
||||
|
@ -143,13 +145,13 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||
)
|
||||
)
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = {
|
||||
CONF_LANGUAGE: language,
|
||||
CONF_DIASPORA: diaspora,
|
||||
CONF_LOCATION: location,
|
||||
CONF_CANDLE_LIGHT_MINUTES: candle_lighting_offset,
|
||||
CONF_HAVDALAH_OFFSET_MINUTES: havdalah_offset,
|
||||
}
|
||||
config_entry.runtime_data = JewishCalendarData(
|
||||
language,
|
||||
diaspora,
|
||||
location,
|
||||
candle_lighting_offset,
|
||||
havdalah_offset,
|
||||
)
|
||||
|
||||
# Update unique ID to be unrelated to user defined options
|
||||
old_prefix = get_unique_prefix(
|
||||
|
@ -163,7 +165,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||
|
||||
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
||||
|
||||
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
async def update_listener(
|
||||
hass: HomeAssistant, config_entry: JewishCalendarConfigEntry
|
||||
) -> None:
|
||||
# Trigger update of states for all platforms
|
||||
await hass.config_entries.async_reload(config_entry.entry_id)
|
||||
|
||||
|
@ -171,16 +175,11 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(
|
||||
hass: HomeAssistant, config_entry: JewishCalendarConfigEntry
|
||||
) -> bool:
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(
|
||||
config_entry, PLATFORMS
|
||||
)
|
||||
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(config_entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
|
||||
|
||||
|
||||
@callback
|
||||
|
|
|
@ -14,15 +14,13 @@ from homeassistant.components.binary_sensor import (
|
|||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
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 DOMAIN
|
||||
from .entity import JewishCalendarEntity
|
||||
from .entity import JewishCalendarConfigEntry, JewishCalendarEntity
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
@ -63,14 +61,12 @@ BINARY_SENSORS: tuple[JewishCalendarBinarySensorEntityDescription, ...] = (
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: JewishCalendarConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Jewish Calendar binary sensors."""
|
||||
entry = hass.data[DOMAIN][config_entry.entry_id]
|
||||
|
||||
async_add_entities(
|
||||
JewishCalendarBinarySensor(config_entry, entry, description)
|
||||
JewishCalendarBinarySensor(config_entry, description)
|
||||
for description in BINARY_SENSORS
|
||||
)
|
||||
|
||||
|
|
|
@ -1,18 +1,27 @@
|
|||
"""Entity representing a Jewish Calendar sensor."""
|
||||
|
||||
from typing import Any
|
||||
from dataclasses import dataclass
|
||||
|
||||
from hdate import Location
|
||||
|
||||
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,
|
||||
)
|
||||
from .const import DOMAIN
|
||||
|
||||
type JewishCalendarConfigEntry = ConfigEntry[JewishCalendarData]
|
||||
|
||||
|
||||
@dataclass
|
||||
class JewishCalendarData:
|
||||
"""Jewish Calendar runtime dataclass."""
|
||||
|
||||
language: str
|
||||
diaspora: bool
|
||||
location: Location
|
||||
candle_lighting_offset: int
|
||||
havdalah_offset: int
|
||||
|
||||
|
||||
class JewishCalendarEntity(Entity):
|
||||
|
@ -22,8 +31,7 @@ class JewishCalendarEntity(Entity):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
config_entry: ConfigEntry,
|
||||
data: dict[str, Any],
|
||||
config_entry: JewishCalendarConfigEntry,
|
||||
description: EntityDescription,
|
||||
) -> None:
|
||||
"""Initialize a Jewish Calendar entity."""
|
||||
|
@ -32,10 +40,10 @@ class JewishCalendarEntity(Entity):
|
|||
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]
|
||||
data = config_entry.runtime_data
|
||||
self._location = data.location
|
||||
self._hebrew = data.language == "hebrew"
|
||||
self._candle_lighting_offset = data.candle_lighting_offset
|
||||
self._havdalah_offset = data.havdalah_offset
|
||||
self._diaspora = data.diaspora
|
||||
|
|
|
@ -14,15 +14,13 @@ from homeassistant.components.sensor import (
|
|||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
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 DOMAIN
|
||||
from .entity import JewishCalendarEntity
|
||||
from .entity import JewishCalendarConfigEntry, JewishCalendarEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -169,17 +167,15 @@ TIME_SENSORS: tuple[SensorEntityDescription, ...] = (
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: JewishCalendarConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Jewish calendar sensors ."""
|
||||
entry = hass.data[DOMAIN][config_entry.entry_id]
|
||||
sensors = [
|
||||
JewishCalendarSensor(config_entry, entry, description)
|
||||
for description in INFO_SENSORS
|
||||
JewishCalendarSensor(config_entry, description) for description in INFO_SENSORS
|
||||
]
|
||||
sensors.extend(
|
||||
JewishCalendarTimeSensor(config_entry, entry, description)
|
||||
JewishCalendarTimeSensor(config_entry, description)
|
||||
for description in TIME_SENSORS
|
||||
)
|
||||
|
||||
|
@ -193,12 +189,11 @@ class JewishCalendarSensor(JewishCalendarEntity, SensorEntity):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
config_entry: ConfigEntry,
|
||||
data: dict[str, Any],
|
||||
config_entry: JewishCalendarConfigEntry,
|
||||
description: SensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the Jewish calendar sensor."""
|
||||
super().__init__(config_entry, data, description)
|
||||
super().__init__(config_entry, description)
|
||||
self._attrs: dict[str, str] = {}
|
||||
|
||||
async def async_update(self) -> None:
|
||||
|
|
Loading…
Add table
Reference in a new issue