mirror of
https://github.com/Gurkengewuerz/casait-has.git
synced 2025-01-18 15:36:28 +01:00
feat: refactor services to be more organized
This commit is contained in:
parent
647efb0e61
commit
1d8fe7a6ab
@ -4,17 +4,17 @@ from __future__ import annotations
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import Platform, CONF_HOST, CONF_PORT, CONF_NAME, ATTR_ENTITY_ID
|
from homeassistant.const import Platform, CONF_HOST, CONF_PORT, CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
DATA_COORDINATOR,
|
DATA_COORDINATOR,
|
||||||
DATA_CONFIG,
|
DATA_CONFIG,
|
||||||
DEFAULT_PORT, SERVICE_SET_ANIMATION_SPEED, SERVICE_SCHEMA_ANIMATION_SPEED, ATTR_SPEED,
|
DEFAULT_PORT
|
||||||
)
|
)
|
||||||
from .coordinator import SmartHomeDataUpdateCoordinator
|
from .coordinator import SmartHomeDataUpdateCoordinator
|
||||||
|
from .services import async_setup_services, async_unload_services
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -27,6 +27,7 @@ PLATFORMS: list[Platform] = [
|
|||||||
Platform.BINARY_SENSOR,
|
Platform.BINARY_SENSOR,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up Smart Home from a config entry."""
|
"""Set up Smart Home from a config entry."""
|
||||||
hass.data.setdefault(DOMAIN, {})
|
hass.data.setdefault(DOMAIN, {})
|
||||||
@ -47,34 +48,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
# Register services
|
# Register services
|
||||||
async def service_handler(call: ServiceCall) -> None:
|
await async_setup_services(hass)
|
||||||
"""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,
|
|
||||||
)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
@ -83,8 +61,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
coordinator: SmartHomeDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
|
coordinator: SmartHomeDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
|
||||||
await coordinator.async_shutdown()
|
await coordinator.async_shutdown()
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
|
await async_unload_services(hass)
|
||||||
# Unregister services
|
|
||||||
hass.services.async_remove(DOMAIN, SERVICE_SET_ANIMATION_SPEED)
|
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
"""Constants for the Smart Home integration."""
|
"""Constants for the Smart Home integration."""
|
||||||
import voluptuous as vol
|
|
||||||
|
|
||||||
from typing import Final
|
from typing import Final
|
||||||
|
|
||||||
from homeassistant.const import ATTR_ENTITY_ID
|
|
||||||
from homeassistant.helpers import config_validation as cv
|
|
||||||
|
|
||||||
DOMAIN: Final = "smart_home"
|
DOMAIN: Final = "smart_home"
|
||||||
|
|
||||||
CONF_HOST: Final = "host"
|
CONF_HOST: Final = "host"
|
||||||
@ -20,12 +15,3 @@ STEP_USER: Final = "user"
|
|||||||
DATA_COORDINATOR: Final = "coordinator"
|
DATA_COORDINATOR: Final = "coordinator"
|
||||||
DATA_CONFIG: Final = "config"
|
DATA_CONFIG: Final = "config"
|
||||||
|
|
||||||
SERVICE_SET_ANIMATION_SPEED = "set_animation_speed"
|
|
||||||
ATTR_SPEED = "speed"
|
|
||||||
|
|
||||||
SERVICE_SCHEMA_ANIMATION_SPEED = vol.Schema({
|
|
||||||
vol.Required(ATTR_ENTITY_ID): cv.comp_entity_ids,
|
|
||||||
vol.Required(ATTR_SPEED): vol.All(
|
|
||||||
vol.Coerce(int), vol.Range(min=1, max=255)
|
|
||||||
),
|
|
||||||
})
|
|
||||||
|
@ -168,10 +168,12 @@ class SmartHomeLight(SmartHomeEntity, LightEntity):
|
|||||||
if ATTR_EFFECT in kwargs:
|
if ATTR_EFFECT in kwargs:
|
||||||
# Convert HA effect name back to API animation mode
|
# Convert HA effect name back to API animation mode
|
||||||
effect_name = kwargs[ATTR_EFFECT]
|
effect_name = kwargs[ATTR_EFFECT]
|
||||||
if effect_name in self._effect_map:
|
effect_key = list(self._effect_map.keys())[list(self._effect_map.values()).index(effect_name)]
|
||||||
data["animation"] = self._effect_map[effect_name]
|
if effect_key is not None:
|
||||||
|
# get effect key from value
|
||||||
|
data["animation"] = effect_key
|
||||||
else:
|
else:
|
||||||
_LOGGER.warning("Unknown effect name: %s. Valid effects are: %s", effect_name, list(self._effect_map.keys()))
|
_LOGGER.warning("Unknown effect name: %s (%s). Valid effects are: %s", effect_name, effect_key, list(self._effect_map.keys()))
|
||||||
|
|
||||||
_LOGGER.debug("Sending data to API: %s", data)
|
_LOGGER.debug("Sending data to API: %s", data)
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
|
58
custom_components/smart_home/services.py
Normal file
58
custom_components/smart_home/services.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
"""Service for adjusting animation speed of RGB lights."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import voluptuous as vol
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
|
from homeassistant.const import ATTR_ENTITY_ID
|
||||||
|
from homeassistant.core import HomeAssistant, ServiceCall
|
||||||
|
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
SERVICE_SET_ANIMATION_SPEED = "set_animation_speed"
|
||||||
|
ATTR_SPEED = "speed"
|
||||||
|
|
||||||
|
SERVICE_SCHEMA_ANIMATION_SPEED = vol.Schema({
|
||||||
|
vol.Required(ATTR_ENTITY_ID): cv.comp_entity_ids,
|
||||||
|
vol.Required(ATTR_SPEED): vol.All(
|
||||||
|
vol.Coerce(int), vol.Range(min=1, max=255)
|
||||||
|
),
|
||||||
|
})
|
||||||
|
|
||||||
|
async def async_setup_services(hass: HomeAssistant) -> None:
|
||||||
|
"""Set up the Light Animation services."""
|
||||||
|
|
||||||
|
# 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,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def async_unload_services(hass: HomeAssistant) -> None:
|
||||||
|
# Unregister services
|
||||||
|
hass.services.async_remove(DOMAIN, SERVICE_SET_ANIMATION_SPEED)
|
Loading…
Reference in New Issue
Block a user