Start
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
"""Sternwarte Home Assistant integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from functools import partial
|
||||
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
SERVICE_CLEAR_SCHEDULE,
|
||||
SERVICE_CLOSE_ROOF,
|
||||
SERVICE_OPEN_ROOF,
|
||||
SERVICE_POWER_OFF,
|
||||
SERVICE_POWER_ON,
|
||||
SERVICE_SET_NAUTICAL_SCHEDULE,
|
||||
SERVICE_STOP_ROOF,
|
||||
)
|
||||
from .coordinator import SternwarteDataUpdateCoordinator
|
||||
|
||||
PLATFORMS: list[Platform] = [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.BUTTON,
|
||||
Platform.COVER,
|
||||
Platform.DATETIME,
|
||||
Platform.SENSOR,
|
||||
Platform.SWITCH,
|
||||
]
|
||||
|
||||
|
||||
async def _async_call_on_all(
|
||||
hass: HomeAssistant,
|
||||
entry_action: str,
|
||||
) -> None:
|
||||
"""Run an action on all Sternwarte coordinators."""
|
||||
for coordinator in hass.data.get(DOMAIN, {}).values():
|
||||
await getattr(coordinator, entry_action)()
|
||||
|
||||
|
||||
async def _async_handle_open_roof(hass: HomeAssistant, call: ServiceCall) -> None:
|
||||
await _async_call_on_all(hass, "async_open_roof")
|
||||
|
||||
|
||||
async def _async_handle_close_roof(hass: HomeAssistant, call: ServiceCall) -> None:
|
||||
await _async_call_on_all(hass, "async_close_roof")
|
||||
|
||||
|
||||
async def _async_handle_stop_roof(hass: HomeAssistant, call: ServiceCall) -> None:
|
||||
await _async_call_on_all(hass, "async_stop_roof")
|
||||
|
||||
|
||||
async def _async_handle_power_on(hass: HomeAssistant, call: ServiceCall) -> None:
|
||||
await _async_call_on_all(hass, "async_turn_power_on")
|
||||
|
||||
|
||||
async def _async_handle_power_off(hass: HomeAssistant, call: ServiceCall) -> None:
|
||||
await _async_call_on_all(hass, "async_turn_power_off")
|
||||
|
||||
|
||||
async def _async_handle_clear_schedule(hass: HomeAssistant, call: ServiceCall) -> None:
|
||||
await _async_call_on_all(hass, "async_clear_schedule")
|
||||
|
||||
|
||||
async def _async_handle_set_nautical_schedule(hass: HomeAssistant, call: ServiceCall) -> None:
|
||||
await _async_call_on_all(hass, "async_set_next_nautical_schedule")
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up Sternwarte from a config entry."""
|
||||
coordinator = SternwarteDataUpdateCoordinator(hass, entry)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
||||
|
||||
if not hass.services.has_service(DOMAIN, SERVICE_OPEN_ROOF):
|
||||
hass.services.async_register(DOMAIN, SERVICE_OPEN_ROOF, partial(_async_handle_open_roof, hass))
|
||||
hass.services.async_register(DOMAIN, SERVICE_CLOSE_ROOF, partial(_async_handle_close_roof, hass))
|
||||
hass.services.async_register(DOMAIN, SERVICE_STOP_ROOF, partial(_async_handle_stop_roof, hass))
|
||||
hass.services.async_register(DOMAIN, SERVICE_POWER_ON, partial(_async_handle_power_on, hass))
|
||||
hass.services.async_register(DOMAIN, SERVICE_POWER_OFF, partial(_async_handle_power_off, hass))
|
||||
hass.services.async_register(DOMAIN, SERVICE_CLEAR_SCHEDULE, partial(_async_handle_clear_schedule, hass))
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_SET_NAUTICAL_SCHEDULE,
|
||||
partial(_async_handle_set_nautical_schedule, hass),
|
||||
)
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id, None)
|
||||
if not hass.data[DOMAIN]:
|
||||
for service in (
|
||||
SERVICE_OPEN_ROOF,
|
||||
SERVICE_CLOSE_ROOF,
|
||||
SERVICE_STOP_ROOF,
|
||||
SERVICE_POWER_ON,
|
||||
SERVICE_POWER_OFF,
|
||||
SERVICE_CLEAR_SCHEDULE,
|
||||
SERVICE_SET_NAUTICAL_SCHEDULE,
|
||||
):
|
||||
if hass.services.has_service(DOMAIN, service):
|
||||
hass.services.async_remove(DOMAIN, service)
|
||||
return unload_ok
|
||||
Reference in New Issue
Block a user