Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CODEOWNERS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 7 additions & 17 deletions homeassistant/components/frontend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,39 +75,29 @@

_LOGGER = logging.getLogger(__name__)

EXTENDED_THEME_SCHEMA = vol.Schema(
THEME_SCHEMA = vol.Schema(
{
# Theme variables that apply to all modes
cv.string: cv.string,
# Mode specific theme variables
vol.Optional(CONF_THEMES_MODES): vol.Schema(
vol.Optional(CONF_THEMES_MODES): vol.All(
{
vol.Optional(CONF_THEMES_LIGHT): vol.Schema({cv.string: cv.string}),
vol.Optional(CONF_THEMES_DARK): vol.Schema({cv.string: cv.string}),
}
},
cv.has_at_least_one_key(CONF_THEMES_LIGHT, CONF_THEMES_DARK),
),
}
)

THEME_SCHEMA = vol.Schema(
{
cv.string: (
vol.Any(
# Legacy theme scheme
{cv.string: cv.string},
# New extended schema with mode support
EXTENDED_THEME_SCHEMA,
)
)
}
)
THEMES_SCHEMA = vol.Schema({cv.string: THEME_SCHEMA})

CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Optional(CONF_FRONTEND_REPO): cv.isdir,
vol.Optional(CONF_THEMES): THEME_SCHEMA,
vol.Optional(CONF_THEMES): THEMES_SCHEMA,
vol.Optional(CONF_EXTRA_MODULE_URL): vol.All(
cv.ensure_list, [cv.string]
),
Expand Down Expand Up @@ -546,7 +536,7 @@ async def reload_themes(_: ServiceCall) -> None:
new_themes = config.get(DOMAIN, {}).get(CONF_THEMES, {})

try:
THEME_SCHEMA(new_themes)
THEMES_SCHEMA(new_themes)
except vol.Invalid as err:
raise HomeAssistantError(f"Failed to reload themes: {err}") from err

Expand Down
18 changes: 4 additions & 14 deletions homeassistant/components/jewish_calendar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
DEFAULT_LANGUAGE,
DOMAIN,
)
from .coordinator import JewishCalendarData, JewishCalendarUpdateCoordinator
from .entity import JewishCalendarConfigEntry
from .entity import JewishCalendarConfigEntry, JewishCalendarData
from .services import async_setup_services

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -70,33 +69,24 @@ async def async_setup_entry(
)
)

data = JewishCalendarData(
config_entry.runtime_data = JewishCalendarData(
language,
diaspora,
location,
candle_lighting_offset,
havdalah_offset,
)

coordinator = JewishCalendarUpdateCoordinator(hass, config_entry, data)
await coordinator.async_config_entry_first_refresh()

config_entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)

return True


async def async_unload_entry(
hass: HomeAssistant, config_entry: JewishCalendarConfigEntry
) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(
config_entry, PLATFORMS
):
coordinator = config_entry.runtime_data
if coordinator.event_unsub:
coordinator.event_unsub()
return unload_ok
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)


async def async_migrate_entry(
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/jewish_calendar/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class JewishCalendarBinarySensor(JewishCalendarEntity, BinarySensorEntity):
@property
def is_on(self) -> bool:
"""Return true if sensor is on."""
return self.entity_description.is_on(self.coordinator.zmanim)(dt_util.now())
zmanim = self.make_zmanim(dt.date.today())
return self.entity_description.is_on(zmanim)(dt_util.now())

def _update_times(self, zmanim: Zmanim) -> list[dt.datetime | None]:
"""Return a list of times to update the sensor."""
Expand Down
116 changes: 0 additions & 116 deletions homeassistant/components/jewish_calendar/coordinator.py

This file was deleted.

2 changes: 1 addition & 1 deletion homeassistant/components/jewish_calendar/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ async def async_get_config_entry_diagnostics(

return {
"entry_data": async_redact_data(entry.data, TO_REDACT),
"data": async_redact_data(asdict(entry.runtime_data.data), TO_REDACT),
"data": async_redact_data(asdict(entry.runtime_data), TO_REDACT),
}
64 changes: 57 additions & 7 deletions homeassistant/components/jewish_calendar/entity.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
"""Entity representing a Jewish Calendar sensor."""

from abc import abstractmethod
from dataclasses import dataclass
import datetime as dt
import logging

from hdate import Zmanim
from hdate import HDateInfo, Location, Zmanim
from hdate.translator import Language, set_language

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import CALLBACK_TYPE, callback
from homeassistant.helpers import event
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.util import dt as dt_util

from .const import DOMAIN
from .coordinator import JewishCalendarConfigEntry, JewishCalendarUpdateCoordinator

_LOGGER = logging.getLogger(__name__)

class JewishCalendarEntity(CoordinatorEntity[JewishCalendarUpdateCoordinator]):
type JewishCalendarConfigEntry = ConfigEntry[JewishCalendarData]


@dataclass
class JewishCalendarDataResults:
"""Jewish Calendar results dataclass."""

dateinfo: HDateInfo
zmanim: Zmanim


@dataclass
class JewishCalendarData:
"""Jewish Calendar runtime dataclass."""

language: Language
diaspora: bool
location: Location
candle_lighting_offset: int
havdalah_offset: int
results: JewishCalendarDataResults | None = None


class JewishCalendarEntity(Entity):
"""An HA implementation for Jewish Calendar entity."""

_attr_has_entity_name = True
Expand All @@ -29,13 +55,23 @@ def __init__(
description: EntityDescription,
) -> None:
"""Initialize a Jewish Calendar entity."""
super().__init__(config_entry.runtime_data)
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)},
)
self.data = config_entry.runtime_data
set_language(self.data.language)

def make_zmanim(self, date: dt.date) -> Zmanim:
"""Create a Zmanim object."""
return Zmanim(
date=date,
location=self.data.location,
candle_lighting_offset=self.data.candle_lighting_offset,
havdalah_offset=self.data.havdalah_offset,
)

async def async_added_to_hass(self) -> None:
"""Call when entity is added to hass."""
Expand All @@ -56,9 +92,10 @@ def _update_times(self, zmanim: Zmanim) -> list[dt.datetime | None]:
def _schedule_update(self) -> None:
"""Schedule the next update of the sensor."""
now = dt_util.now()
zmanim = self.make_zmanim(now.date())
update = dt_util.start_of_local_day() + dt.timedelta(days=1)

for update_time in self._update_times(self.coordinator.zmanim):
for update_time in self._update_times(zmanim):
if update_time is not None and now < update_time < update:
update = update_time

Expand All @@ -73,4 +110,17 @@ def _update(self, now: dt.datetime | None = None) -> None:
"""Update the sensor data."""
self._update_unsub = None
self._schedule_update()
self.create_results(now)
self.async_write_ha_state()

def create_results(self, now: dt.datetime | None = None) -> None:
"""Create the results for the sensor."""
if now is None:
now = dt_util.now()

_LOGGER.debug("Now: %s Location: %r", now, self.data.location)

today = now.date()
zmanim = self.make_zmanim(today)
dateinfo = HDateInfo(today, diaspora=self.data.diaspora)
self.data.results = JewishCalendarDataResults(dateinfo, zmanim)
Loading
Loading