Zeitplanung und nautische Einzeltermine erweitern
This commit is contained in:
@@ -18,6 +18,8 @@ from .const import (
|
||||
SERVICE_POWER_OFF,
|
||||
SERVICE_POWER_ON,
|
||||
SERVICE_SET_NAUTICAL_SCHEDULE,
|
||||
SERVICE_SET_NAUTICAL_OPEN_SCHEDULE,
|
||||
SERVICE_SET_NAUTICAL_CLOSE_SCHEDULE,
|
||||
SERVICE_STOP_ROOF,
|
||||
)
|
||||
from .coordinator import SternwarteDataUpdateCoordinator
|
||||
@@ -77,6 +79,14 @@ async def _async_handle_set_nautical_schedule(hass: HomeAssistant, call: Service
|
||||
await _async_call_on_all(hass, "async_set_next_nautical_schedule")
|
||||
|
||||
|
||||
async def _async_handle_set_nautical_open_schedule(hass: HomeAssistant, call: ServiceCall) -> None:
|
||||
await _async_call_on_all(hass, "async_set_next_nautical_open_schedule")
|
||||
|
||||
|
||||
async def _async_handle_set_nautical_close_schedule(hass: HomeAssistant, call: ServiceCall) -> None:
|
||||
await _async_call_on_all(hass, "async_set_next_nautical_close_schedule")
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up Sternwarte from a config entry."""
|
||||
coordinator = SternwarteDataUpdateCoordinator(hass, entry)
|
||||
@@ -106,6 +116,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
SERVICE_SET_NAUTICAL_SCHEDULE,
|
||||
partial(_async_handle_set_nautical_schedule, hass),
|
||||
)
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_SET_NAUTICAL_OPEN_SCHEDULE,
|
||||
partial(_async_handle_set_nautical_open_schedule, hass),
|
||||
)
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_SET_NAUTICAL_CLOSE_SCHEDULE,
|
||||
partial(_async_handle_set_nautical_close_schedule, hass),
|
||||
)
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
return True
|
||||
@@ -127,6 +147,8 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
SERVICE_CLEAR_OPEN_SCHEDULE,
|
||||
SERVICE_CLEAR_CLOSE_SCHEDULE,
|
||||
SERVICE_SET_NAUTICAL_SCHEDULE,
|
||||
SERVICE_SET_NAUTICAL_OPEN_SCHEDULE,
|
||||
SERVICE_SET_NAUTICAL_CLOSE_SCHEDULE,
|
||||
):
|
||||
if hass.services.has_service(DOMAIN, service):
|
||||
hass.services.async_remove(DOMAIN, service)
|
||||
|
||||
@@ -49,3 +49,5 @@ ENTITY_ROOF_POSITION: Final = "roof_position"
|
||||
ENTITY_SCHEDULE_OPEN: Final = "schedule_open"
|
||||
ENTITY_SCHEDULE_CLOSE: Final = "schedule_close"
|
||||
ENTITY_SET_NAUTICAL_SCHEDULE: Final = "set_nautical_schedule"
|
||||
SERVICE_SET_NAUTICAL_OPEN_SCHEDULE: Final = "set_nautical_open_schedule"
|
||||
SERVICE_SET_NAUTICAL_CLOSE_SCHEDULE: Final = "set_nautical_close_schedule"
|
||||
|
||||
@@ -249,6 +249,22 @@ class SternwarteDataUpdateCoordinator(DataUpdateCoordinator[SternwarteState]):
|
||||
await self._async_save_runtime_state()
|
||||
await self.async_request_refresh()
|
||||
|
||||
async def async_set_next_nautical_open_schedule(self) -> None:
|
||||
"""Set only the next nautical dusk as the opening schedule."""
|
||||
now = dt_util.now()
|
||||
when = self._get_next_nautical_event(now, "dusk")
|
||||
if when is None:
|
||||
raise HomeAssistantError("Keine kommende nautische Abenddämmerung gefunden")
|
||||
await self.async_set_open_schedule(when)
|
||||
|
||||
async def async_set_next_nautical_close_schedule(self) -> None:
|
||||
"""Set only the next nautical dawn as the closing schedule."""
|
||||
now = dt_util.now()
|
||||
when = self._get_next_nautical_event(now, "dawn")
|
||||
if when is None:
|
||||
raise HomeAssistantError("Keine kommende nautische Morgendämmerung gefunden")
|
||||
await self.async_set_close_schedule(when)
|
||||
|
||||
async def async_turn_power_on(self) -> None:
|
||||
"""Turn roof power on."""
|
||||
async with self._command_lock:
|
||||
@@ -831,6 +847,17 @@ class SternwarteDataUpdateCoordinator(DataUpdateCoordinator[SternwarteState]):
|
||||
)
|
||||
return dt_util.as_local(event_utc).replace(second=0, microsecond=0)
|
||||
|
||||
def _get_next_nautical_event(self, now: datetime, event_name: str) -> datetime | None:
|
||||
for day_offset in range(3):
|
||||
base_date = now + timedelta(days=day_offset)
|
||||
twilight = self._calc_twilight(base_date)
|
||||
if twilight["naut"] is None:
|
||||
continue
|
||||
event = self._event_to_local_datetime(base_date, twilight["naut"][event_name])
|
||||
if event and event > now:
|
||||
return event
|
||||
return None
|
||||
|
||||
def _get_next_nautical_schedule(self, now: datetime) -> dict[str, datetime | None] | None:
|
||||
for day_offset in range(3):
|
||||
base_date = now + timedelta(days=day_offset)
|
||||
|
||||
@@ -33,3 +33,11 @@ clear_close_schedule:
|
||||
set_nautical_schedule:
|
||||
name: Nautische Dämmerung setzen
|
||||
description: Setzt Öffnen und Schließen auf die nächste nautische Abend- und Morgendämmerung.
|
||||
|
||||
set_nautical_open_schedule:
|
||||
name: Nautische Öffnungszeit setzen
|
||||
description: Setzt nur Öffnen auf die nächste nautische Abenddämmerung.
|
||||
|
||||
set_nautical_close_schedule:
|
||||
name: Nautische Schließzeit setzen
|
||||
description: Setzt nur Schließen auf die nächste nautische Morgendämmerung.
|
||||
|
||||
Reference in New Issue
Block a user