mirror of
https://github.com/home-assistant/core.git
synced 2026-07-07 06:46:17 +01:00
150 lines
4.9 KiB
Python
150 lines
4.9 KiB
Python
"""Services for the Vallox integration."""
|
|
|
|
from enum import StrEnum, auto
|
|
import logging
|
|
|
|
from vallox_websocket_api import Profile, ValloxApiException
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
|
|
from .const import DOMAIN, I18N_KEY_TO_VALLOX_PROFILE, PROFILE_DURATION_INDEFINITE
|
|
from .coordinator import ValloxConfigEntry, ValloxDataUpdateCoordinator
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
ATTR_PROFILE_FAN_SPEED = "fan_speed"
|
|
ATTR_PROFILE = "profile"
|
|
ATTR_DURATION = "duration"
|
|
|
|
|
|
class ValloxService(StrEnum):
|
|
"""Vallox service names."""
|
|
|
|
SET_PROFILE_FAN_SPEED_HOME = auto()
|
|
SET_PROFILE_FAN_SPEED_AWAY = auto()
|
|
SET_PROFILE_FAN_SPEED_BOOST = auto()
|
|
SET_PROFILE = auto()
|
|
|
|
|
|
SERVICE_SCHEMA_SET_PROFILE_FAN_SPEED = vol.Schema(
|
|
{
|
|
vol.Required(ATTR_PROFILE_FAN_SPEED): vol.All(
|
|
vol.Coerce(int), vol.Clamp(min=0, max=100)
|
|
)
|
|
}
|
|
)
|
|
|
|
SERVICE_SCHEMA_SET_PROFILE = vol.Schema(
|
|
{
|
|
vol.Required(ATTR_PROFILE): vol.In(I18N_KEY_TO_VALLOX_PROFILE),
|
|
vol.Optional(ATTR_DURATION): vol.All(
|
|
vol.Coerce(int), vol.Clamp(min=1, max=PROFILE_DURATION_INDEFINITE)
|
|
),
|
|
}
|
|
)
|
|
|
|
|
|
def _get_coordinator(
|
|
hass: HomeAssistant,
|
|
) -> ValloxDataUpdateCoordinator:
|
|
"""Return the coordinator for the Vallox config entry."""
|
|
entries: list[ValloxConfigEntry] = hass.config_entries.async_loaded_entries(DOMAIN)
|
|
if len(entries) != 1:
|
|
raise ValueError("Expected exactly one loaded Vallox config entry")
|
|
|
|
return entries[0].runtime_data
|
|
|
|
|
|
async def _async_set_profile_fan_speed(call: ServiceCall, profile: Profile) -> None:
|
|
"""Set the fan speed in percent for the profile matching the called service."""
|
|
fan_speed: int = call.data[ATTR_PROFILE_FAN_SPEED]
|
|
_LOGGER.debug("Setting %s fan speed to: %d%%", profile.name, fan_speed)
|
|
|
|
coordinator = _get_coordinator(call.hass)
|
|
try:
|
|
await coordinator.client.set_fan_speed(profile, fan_speed)
|
|
except ValloxApiException as err:
|
|
raise HomeAssistantError(
|
|
translation_domain=DOMAIN,
|
|
translation_key="failed_to_set_fan_speed_for_profile",
|
|
translation_placeholders={
|
|
"profile": profile.name.lower(),
|
|
"fan_speed": str(fan_speed),
|
|
},
|
|
) from err
|
|
else:
|
|
await coordinator.async_request_refresh()
|
|
|
|
|
|
async def _async_set_profile_fan_speed_away(call: ServiceCall) -> None:
|
|
"""Set the fan speed in percent for the Away profile."""
|
|
await _async_set_profile_fan_speed(call, Profile.AWAY)
|
|
|
|
|
|
async def _async_set_profile_fan_speed_boost(call: ServiceCall) -> None:
|
|
"""Set the fan speed in percent for the Boost profile."""
|
|
await _async_set_profile_fan_speed(call, Profile.BOOST)
|
|
|
|
|
|
async def _async_set_profile_fan_speed_home(call: ServiceCall) -> None:
|
|
"""Set the fan speed in percent for the Home profile."""
|
|
await _async_set_profile_fan_speed(call, Profile.HOME)
|
|
|
|
|
|
async def _async_set_profile(call: ServiceCall) -> None:
|
|
"""Activate the given profile for the given duration."""
|
|
profile_key: str = call.data[ATTR_PROFILE]
|
|
duration: int | None = call.data.get(ATTR_DURATION)
|
|
_LOGGER.debug("Activating profile %s for %s min", profile_key, duration)
|
|
|
|
coordinator = _get_coordinator(call.hass)
|
|
try:
|
|
await coordinator.client.set_profile(
|
|
I18N_KEY_TO_VALLOX_PROFILE[profile_key], duration
|
|
)
|
|
except ValloxApiException as err:
|
|
placeholders = {"profile": profile_key}
|
|
if duration is not None and duration != PROFILE_DURATION_INDEFINITE:
|
|
placeholders["duration"] = str(duration)
|
|
translation_key = "failed_to_set_profile_for_duration"
|
|
else:
|
|
translation_key = "failed_to_set_profile"
|
|
raise HomeAssistantError(
|
|
translation_domain=DOMAIN,
|
|
translation_key=translation_key,
|
|
translation_placeholders=placeholders,
|
|
) from err
|
|
else:
|
|
await coordinator.async_request_refresh()
|
|
|
|
|
|
@callback
|
|
def async_setup_services(hass: HomeAssistant) -> None:
|
|
"""Register the Vallox services."""
|
|
hass.services.async_register(
|
|
DOMAIN,
|
|
ValloxService.SET_PROFILE_FAN_SPEED_AWAY,
|
|
_async_set_profile_fan_speed_away,
|
|
schema=SERVICE_SCHEMA_SET_PROFILE_FAN_SPEED,
|
|
)
|
|
hass.services.async_register(
|
|
DOMAIN,
|
|
ValloxService.SET_PROFILE_FAN_SPEED_BOOST,
|
|
_async_set_profile_fan_speed_boost,
|
|
schema=SERVICE_SCHEMA_SET_PROFILE_FAN_SPEED,
|
|
)
|
|
hass.services.async_register(
|
|
DOMAIN,
|
|
ValloxService.SET_PROFILE_FAN_SPEED_HOME,
|
|
_async_set_profile_fan_speed_home,
|
|
schema=SERVICE_SCHEMA_SET_PROFILE_FAN_SPEED,
|
|
)
|
|
hass.services.async_register(
|
|
DOMAIN,
|
|
ValloxService.SET_PROFILE,
|
|
_async_set_profile,
|
|
schema=SERVICE_SCHEMA_SET_PROFILE,
|
|
)
|