| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- from __future__ import annotations
- from homeassistant.core import HomeAssistant, callback
- from homeassistant.const import Platform
- from homeassistant.config_entries import ConfigEntry
- from homeassistant.helpers.device import async_entity_id_to_device
- from homeassistant.helpers.device_registry import DATA_REGISTRY
- from homeassistant.helpers.entity_component import DATA_INSTANCES
- from .reservation import Reservation
- from .const import DOMAIN, PLATFORMS, RESERVATION_FIELDS
- type ZiBConfigEntry = ConfigEntry[Reservation]
- async def async_setup_entry(hass: HomeAssistant, entry: ZiBConfigEntry) -> bool:
- entry.runtime_data = Reservation(hass, entry.data["Name"])
- await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
- return True
- async def async_unload_entry(hass: HomeAssistant, entry: ZiBConfigEntry) -> bool:
- unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
- return unload_ok
- async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
- @callback
- async def get_reservations(devices, entities) -> list:
- reservations = []
- if devices:
- for device in devices:
- dev = hass.data[DATA_REGISTRY].async_get(device)
- if not dev:
- continue
- conf = hass.config_entries.async_get_entry(dev.primary_config_entry)
- if not conf:
- continue
- reservations.append(conf.runtime_data)
- if entities:
- for entity in hass.data[DATA_INSTANCES][Platform.SENSOR].entities:
- if entity.entity_id in entities:
- reservations.append(entity.platform.config_entry.runtime_data)
- for res in reservations:
- if not isinstance(res, Reservation):
- reservations.remove(res);
- return reservations
- @callback
- async def set_reservation(call: ServiceCall) -> None:
- reservations = await get_reservations(call.data.get('device_id'), call.data.get('entity_id'))
- for res in reservations:
- for field in RESERVATION_FIELDS:
- value = call.data.get(field)
- if value is not None:
- res.set(field, value)
- await res.publish_updates()
- hass.services.async_register(DOMAIN, 'set_reservation', set_reservation)
- return True
|