2025-01-03 22:01:49 +01:00
|
|
|
"""The Smart Home integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2025-01-16 22:57:55 +01:00
|
|
|
from homeassistant.const import Platform, CONF_HOST, CONF_PORT, CONF_NAME, ATTR_ENTITY_ID
|
|
|
|
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
|
|
|
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
2025-01-03 22:01:49 +01:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
DOMAIN,
|
|
|
|
DATA_COORDINATOR,
|
|
|
|
DATA_CONFIG,
|
2025-01-16 22:57:55 +01:00
|
|
|
DEFAULT_PORT, SERVICE_SET_ANIMATION_SPEED, SERVICE_SCHEMA_ANIMATION_SPEED, ATTR_SPEED,
|
2025-01-03 22:01:49 +01:00
|
|
|
)
|
|
|
|
from .coordinator import SmartHomeDataUpdateCoordinator
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
PLATFORMS: list[Platform] = [
|
|
|
|
Platform.SWITCH,
|
2025-01-16 22:09:00 +01:00
|
|
|
Platform.LIGHT,
|
2025-01-03 22:01:49 +01:00
|
|
|
Platform.BUTTON,
|
|
|
|
Platform.COVER,
|
|
|
|
Platform.SENSOR,
|
|
|
|
Platform.BINARY_SENSOR,
|
|
|
|
]
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up Smart Home from a config entry."""
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
|
|
|
|
coordinator = SmartHomeDataUpdateCoordinator(
|
|
|
|
hass,
|
|
|
|
config=entry.data,
|
|
|
|
entry_id=entry.entry_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
|
|
|
DATA_COORDINATOR: coordinator,
|
|
|
|
DATA_CONFIG: entry.data,
|
|
|
|
}
|
|
|
|
|
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2025-01-16 22:57:55 +01:00
|
|
|
|
|
|
|
# Register services
|
|
|
|
async def service_handler(call: ServiceCall) -> None:
|
|
|
|
"""Handle the services."""
|
|
|
|
# coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
|
|
|
|
if call.service == SERVICE_SET_ANIMATION_SPEED:
|
|
|
|
# Get all light entities from the call
|
|
|
|
target_entities = call.data.get(ATTR_ENTITY_ID)
|
|
|
|
speed = call.data.get(ATTR_SPEED)
|
|
|
|
|
|
|
|
for entity_id in target_entities:
|
|
|
|
# Find the light entity
|
|
|
|
entity = hass.data[LIGHT_DOMAIN].get_entity(entity_id)
|
|
|
|
if entity and hasattr(entity, "set_animation_speed"):
|
|
|
|
await entity.set_animation_speed(speed)
|
|
|
|
else:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Entity %s doesn't support animation speed adjustment",
|
|
|
|
entity_id
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_ANIMATION_SPEED,
|
|
|
|
service_handler,
|
|
|
|
schema=SERVICE_SCHEMA_ANIMATION_SPEED,
|
|
|
|
)
|
|
|
|
|
2025-01-03 22:01:49 +01:00
|
|
|
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:
|
|
|
|
coordinator: SmartHomeDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
|
|
|
|
await coordinator.async_shutdown()
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2025-01-16 22:57:55 +01:00
|
|
|
|
|
|
|
# Unregister services
|
|
|
|
hass.services.async_remove(DOMAIN, SERVICE_SET_ANIMATION_SPEED)
|
|
|
|
|
2025-01-16 22:09:00 +01:00
|
|
|
return unload_ok
|