Update Switcher actions exceptions (#155296)

This commit is contained in:
Shay Levy
2025-10-29 14:32:10 +02:00
committed by GitHub
parent 48d371eddb
commit 1387308f48
4 changed files with 22 additions and 60 deletions

View File

@@ -2,24 +2,21 @@
from __future__ import annotations
from collections.abc import Callable, Coroutine
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any, cast
from aioswitcher.api import SwitcherApi
from aioswitcher.api.messages import SwitcherBaseResponse
from aioswitcher.api.remotes import SwitcherBreezeRemote
from aioswitcher.device import DeviceCategory, DeviceState, ThermostatSwing
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import SwitcherConfigEntry
from .const import SIGNAL_DEVICE_ADD
from .const import API_CONTROL_BREEZE_DEVICE, SIGNAL_DEVICE_ADD
from .coordinator import SwitcherDataUpdateCoordinator
from .entity import SwitcherEntity
from .utils import get_breeze_remote_manager
@@ -31,10 +28,7 @@ PARALLEL_UPDATES = 1
class SwitcherThermostatButtonEntityDescription(ButtonEntityDescription):
"""Class to describe a Switcher Thermostat Button entity."""
press_fn: Callable[
[SwitcherApi, SwitcherBreezeRemote],
Coroutine[Any, Any, SwitcherBaseResponse],
]
press_args: dict[str, Any]
supported: Callable[[SwitcherBreezeRemote], bool]
@@ -43,34 +37,26 @@ THERMOSTAT_BUTTONS = [
key="assume_on",
translation_key="assume_on",
entity_category=EntityCategory.CONFIG,
press_fn=lambda api, remote: api.control_breeze_device(
remote, state=DeviceState.ON, update_state=True
),
press_args={"state": DeviceState.ON, "update_state": True},
supported=lambda _: True,
),
SwitcherThermostatButtonEntityDescription(
key="assume_off",
translation_key="assume_off",
entity_category=EntityCategory.CONFIG,
press_fn=lambda api, remote: api.control_breeze_device(
remote, state=DeviceState.OFF, update_state=True
),
press_args={"state": DeviceState.OFF, "update_state": True},
supported=lambda _: True,
),
SwitcherThermostatButtonEntityDescription(
key="vertical_swing_on",
translation_key="vertical_swing_on",
press_fn=lambda api, remote: api.control_breeze_device(
remote, swing=ThermostatSwing.ON
),
press_args={"swing": ThermostatSwing.ON},
supported=lambda remote: bool(remote.separated_swing_command),
),
SwitcherThermostatButtonEntityDescription(
key="vertical_swing_off",
translation_key="vertical_swing_off",
press_fn=lambda api, remote: api.control_breeze_device(
remote, swing=ThermostatSwing.OFF
),
press_args={"swing": ThermostatSwing.OFF},
supported=lambda remote: bool(remote.separated_swing_command),
),
]
@@ -121,23 +107,8 @@ class SwitcherThermostatButtonEntity(SwitcherEntity, ButtonEntity):
async def async_press(self) -> None:
"""Press the button."""
response: SwitcherBaseResponse | None = None
error = None
try:
async with SwitcherApi(
self.coordinator.data.device_type,
self.coordinator.data.ip_address,
self.coordinator.data.device_id,
self.coordinator.data.device_key,
) as swapi:
response = await self.entity_description.press_fn(swapi, self._remote)
except (TimeoutError, OSError, RuntimeError) as err:
error = repr(err)
if error or not response or not response.successful:
self.coordinator.last_update_success = False
self.async_write_ha_state()
raise HomeAssistantError(
f"Call api for {self.name} failed, response/error: {response or error}"
)
await self._async_call_api(
API_CONTROL_BREEZE_DEVICE,
self._remote,
**self.entity_description.press_args,
)

View File

@@ -27,20 +27,18 @@ from homeassistant.components.climate import (
)
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import SwitcherConfigEntry
from .const import SIGNAL_DEVICE_ADD
from .const import API_CONTROL_BREEZE_DEVICE, SIGNAL_DEVICE_ADD
from .coordinator import SwitcherDataUpdateCoordinator
from .entity import SwitcherEntity
from .utils import get_breeze_remote_manager
PARALLEL_UPDATES = 1
API_CONTROL_BREEZE_DEVICE = "control_breeze_device"
DEVICE_MODE_TO_HA = {
ThermostatMode.COOL: HVACMode.COOL,
ThermostatMode.HEAT: HVACMode.HEAT,
@@ -159,21 +157,16 @@ class SwitcherClimateEntity(SwitcherEntity, ClimateEntity):
"""Set new target temperature."""
data = cast(SwitcherThermostat, self.coordinator.data)
if not self._remote.modes_features[data.mode]["temperature_control"]:
raise HomeAssistantError(
"Current mode doesn't support setting Target Temperature"
raise ServiceValidationError(
"Current mode does not support setting 'Target temperature'."
)
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
raise ValueError("No target temperature provided")
await self._async_control_breeze_device(target_temp=int(temperature))
await self._async_control_breeze_device(
target_temp=int(kwargs[ATTR_TEMPERATURE])
)
async def async_set_fan_mode(self, fan_mode: str) -> None:
"""Set new target fan mode."""
data = cast(SwitcherThermostat, self.coordinator.data)
if not self._remote.modes_features[data.mode]["fan_levels"]:
raise HomeAssistantError("Current mode doesn't support setting Fan Mode")
await self._async_control_breeze_device(fan_level=HA_TO_DEVICE_FAN[fan_mode])
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
@@ -187,10 +180,6 @@ class SwitcherClimateEntity(SwitcherEntity, ClimateEntity):
async def async_set_swing_mode(self, swing_mode: str) -> None:
"""Set new target swing operation."""
data = cast(SwitcherThermostat, self.coordinator.data)
if not self._remote.modes_features[data.mode]["swing"]:
raise HomeAssistantError("Current mode doesn't support setting Swing Mode")
if swing_mode == SWING_VERTICAL:
await self._async_control_breeze_device(swing=ThermostatSwing.ON)
else:

View File

@@ -2,6 +2,8 @@
DOMAIN = "switcher_kis"
API_CONTROL_BREEZE_DEVICE = "control_breeze_device"
DISCOVERY_TIME_SEC = 12
SIGNAL_DEVICE_ADD = "switcher_device_add"

View File

@@ -28,7 +28,7 @@ rules:
comment: The integration only supports a single config entry.
# Silver
action-exceptions: todo
action-exceptions: done
config-entry-unloading: done
docs-configuration-parameters: done
docs-installation-parameters: done