mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Move Jewish Calendar to runtime data (#129609)
This commit is contained in:
parent
96de4b3828
commit
57d1001603
@ -7,12 +7,11 @@ from functools import partial
|
|||||||
from hdate import Location
|
from hdate import Location
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
from homeassistant.config_entries import SOURCE_IMPORT
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_ELEVATION,
|
CONF_ELEVATION,
|
||||||
CONF_LANGUAGE,
|
CONF_LANGUAGE,
|
||||||
CONF_LATITUDE,
|
CONF_LATITUDE,
|
||||||
CONF_LOCATION,
|
|
||||||
CONF_LONGITUDE,
|
CONF_LONGITUDE,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
CONF_TIME_ZONE,
|
CONF_TIME_ZONE,
|
||||||
@ -36,6 +35,7 @@ from .const import (
|
|||||||
DEFAULT_NAME,
|
DEFAULT_NAME,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
|
from .entity import JewishCalendarConfigEntry, JewishCalendarData
|
||||||
from .sensor import INFO_SENSORS, TIME_SENSORS
|
from .sensor import INFO_SENSORS, TIME_SENSORS
|
||||||
|
|
||||||
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||||
@ -120,7 +120,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
return True
|
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."""
|
"""Set up a configuration entry for Jewish calendar."""
|
||||||
language = config_entry.data.get(CONF_LANGUAGE, DEFAULT_LANGUAGE)
|
language = config_entry.data.get(CONF_LANGUAGE, DEFAULT_LANGUAGE)
|
||||||
diaspora = config_entry.data.get(CONF_DIASPORA, DEFAULT_DIASPORA)
|
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] = {
|
config_entry.runtime_data = JewishCalendarData(
|
||||||
CONF_LANGUAGE: language,
|
language,
|
||||||
CONF_DIASPORA: diaspora,
|
diaspora,
|
||||||
CONF_LOCATION: location,
|
location,
|
||||||
CONF_CANDLE_LIGHT_MINUTES: candle_lighting_offset,
|
candle_lighting_offset,
|
||||||
CONF_HAVDALAH_OFFSET_MINUTES: havdalah_offset,
|
havdalah_offset,
|
||||||
}
|
)
|
||||||
|
|
||||||
# Update unique ID to be unrelated to user defined options
|
# Update unique ID to be unrelated to user defined options
|
||||||
old_prefix = get_unique_prefix(
|
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)
|
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
|
# Trigger update of states for all platforms
|
||||||
await hass.config_entries.async_reload(config_entry.entry_id)
|
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
|
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 a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(
|
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
|
||||||
config_entry, PLATFORMS
|
|
||||||
)
|
|
||||||
|
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(config_entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
@ -14,15 +14,13 @@ from homeassistant.components.binary_sensor import (
|
|||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
BinarySensorEntityDescription,
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
||||||
from homeassistant.helpers import event
|
from homeassistant.helpers import event
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .entity import JewishCalendarConfigEntry, JewishCalendarEntity
|
||||||
from .entity import JewishCalendarEntity
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
@ -63,14 +61,12 @@ BINARY_SENSORS: tuple[JewishCalendarBinarySensorEntityDescription, ...] = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: JewishCalendarConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Jewish Calendar binary sensors."""
|
"""Set up the Jewish Calendar binary sensors."""
|
||||||
entry = hass.data[DOMAIN][config_entry.entry_id]
|
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
JewishCalendarBinarySensor(config_entry, entry, description)
|
JewishCalendarBinarySensor(config_entry, description)
|
||||||
for description in BINARY_SENSORS
|
for description in BINARY_SENSORS
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,18 +1,27 @@
|
|||||||
"""Entity representing a Jewish Calendar sensor."""
|
"""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.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_LANGUAGE, CONF_LOCATION
|
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||||
from homeassistant.helpers.entity import Entity, EntityDescription
|
from homeassistant.helpers.entity import Entity, EntityDescription
|
||||||
|
|
||||||
from .const import (
|
from .const import DOMAIN
|
||||||
CONF_CANDLE_LIGHT_MINUTES,
|
|
||||||
CONF_DIASPORA,
|
type JewishCalendarConfigEntry = ConfigEntry[JewishCalendarData]
|
||||||
CONF_HAVDALAH_OFFSET_MINUTES,
|
|
||||||
DOMAIN,
|
|
||||||
)
|
@dataclass
|
||||||
|
class JewishCalendarData:
|
||||||
|
"""Jewish Calendar runtime dataclass."""
|
||||||
|
|
||||||
|
language: str
|
||||||
|
diaspora: bool
|
||||||
|
location: Location
|
||||||
|
candle_lighting_offset: int
|
||||||
|
havdalah_offset: int
|
||||||
|
|
||||||
|
|
||||||
class JewishCalendarEntity(Entity):
|
class JewishCalendarEntity(Entity):
|
||||||
@ -22,8 +31,7 @@ class JewishCalendarEntity(Entity):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
config_entry: ConfigEntry,
|
config_entry: JewishCalendarConfigEntry,
|
||||||
data: dict[str, Any],
|
|
||||||
description: EntityDescription,
|
description: EntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a Jewish Calendar entity."""
|
"""Initialize a Jewish Calendar entity."""
|
||||||
@ -32,10 +40,10 @@ class JewishCalendarEntity(Entity):
|
|||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
entry_type=DeviceEntryType.SERVICE,
|
entry_type=DeviceEntryType.SERVICE,
|
||||||
identifiers={(DOMAIN, config_entry.entry_id)},
|
identifiers={(DOMAIN, config_entry.entry_id)},
|
||||||
name=config_entry.title,
|
|
||||||
)
|
)
|
||||||
self._location = data[CONF_LOCATION]
|
data = config_entry.runtime_data
|
||||||
self._hebrew = data[CONF_LANGUAGE] == "hebrew"
|
self._location = data.location
|
||||||
self._candle_lighting_offset = data[CONF_CANDLE_LIGHT_MINUTES]
|
self._hebrew = data.language == "hebrew"
|
||||||
self._havdalah_offset = data[CONF_HAVDALAH_OFFSET_MINUTES]
|
self._candle_lighting_offset = data.candle_lighting_offset
|
||||||
self._diaspora = data[CONF_DIASPORA]
|
self._havdalah_offset = data.havdalah_offset
|
||||||
|
self._diaspora = data.diaspora
|
||||||
|
@ -14,15 +14,13 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntity,
|
SensorEntity,
|
||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import SUN_EVENT_SUNSET, EntityCategory
|
from homeassistant.const import SUN_EVENT_SUNSET, EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.sun import get_astral_event_date
|
from homeassistant.helpers.sun import get_astral_event_date
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .entity import JewishCalendarConfigEntry, JewishCalendarEntity
|
||||||
from .entity import JewishCalendarEntity
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -169,17 +167,15 @@ TIME_SENSORS: tuple[SensorEntityDescription, ...] = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: JewishCalendarConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Jewish calendar sensors ."""
|
"""Set up the Jewish calendar sensors ."""
|
||||||
entry = hass.data[DOMAIN][config_entry.entry_id]
|
|
||||||
sensors = [
|
sensors = [
|
||||||
JewishCalendarSensor(config_entry, entry, description)
|
JewishCalendarSensor(config_entry, description) for description in INFO_SENSORS
|
||||||
for description in INFO_SENSORS
|
|
||||||
]
|
]
|
||||||
sensors.extend(
|
sensors.extend(
|
||||||
JewishCalendarTimeSensor(config_entry, entry, description)
|
JewishCalendarTimeSensor(config_entry, description)
|
||||||
for description in TIME_SENSORS
|
for description in TIME_SENSORS
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -193,12 +189,11 @@ class JewishCalendarSensor(JewishCalendarEntity, SensorEntity):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
config_entry: ConfigEntry,
|
config_entry: JewishCalendarConfigEntry,
|
||||||
data: dict[str, Any],
|
|
||||||
description: SensorEntityDescription,
|
description: SensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the Jewish calendar sensor."""
|
"""Initialize the Jewish calendar sensor."""
|
||||||
super().__init__(config_entry, data, description)
|
super().__init__(config_entry, description)
|
||||||
self._attrs: dict[str, str] = {}
|
self._attrs: dict[str, str] = {}
|
||||||
|
|
||||||
async def async_update(self) -> None:
|
async def async_update(self) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user