Skip to content

Commit

Permalink
Use Switcher _async_call_api in climate (#133230)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecode authored Dec 18, 2024
1 parent 0ff2a0d commit 3a8b0b3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 25 deletions.
25 changes: 3 additions & 22 deletions homeassistant/components/switcher_kis/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from typing import Any, cast

from aioswitcher.api import SwitcherApi, SwitcherBaseResponse
from aioswitcher.api.remotes import SwitcherBreezeRemote
from aioswitcher.device import (
DeviceCategory,
Expand Down Expand Up @@ -38,6 +37,8 @@
from .entity import SwitcherEntity
from .utils import get_breeze_remote_manager

API_CONTROL_BREEZE_DEVICE = "control_breeze_device"

DEVICE_MODE_TO_HA = {
ThermostatMode.COOL: HVACMode.COOL,
ThermostatMode.HEAT: HVACMode.HEAT,
Expand Down Expand Up @@ -155,27 +156,7 @@ def _update_data(self, force_update: bool = False) -> None:

async def _async_control_breeze_device(self, **kwargs: Any) -> None:
"""Call Switcher Control Breeze API."""
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 swapi.control_breeze_device(self._remote, **kwargs)
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 Breeze control for {self.name} failed, "
f"response/error: {response or error}"
)
await self._async_call_api(API_CONTROL_BREEZE_DEVICE, self._remote, **kwargs)

async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature."""
Expand Down
7 changes: 4 additions & 3 deletions homeassistant/components/switcher_kis/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import logging
from typing import Any

from aioswitcher.api import SwitcherApi, SwitcherBaseResponse
from aioswitcher.api import SwitcherApi
from aioswitcher.api.messages import SwitcherBaseResponse

from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr
Expand All @@ -27,7 +28,7 @@ def __init__(self, coordinator: SwitcherDataUpdateCoordinator) -> None:
connections={(dr.CONNECTION_NETWORK_MAC, coordinator.mac_address)}
)

async def _async_call_api(self, api: str, *args: Any) -> None:
async def _async_call_api(self, api: str, *args: Any, **kwargs: Any) -> None:
"""Call Switcher API."""
_LOGGER.debug("Calling api for %s, api: '%s', args: %s", self.name, api, args)
response: SwitcherBaseResponse | None = None
Expand All @@ -41,7 +42,7 @@ async def _async_call_api(self, api: str, *args: Any) -> None:
self.coordinator.data.device_key,
self.coordinator.token,
) as swapi:
response = await getattr(swapi, api)(*args)
response = await getattr(swapi, api)(*args, **kwargs)
except (TimeoutError, OSError, RuntimeError) as err:
error = repr(err)

Expand Down

0 comments on commit 3a8b0b3

Please sign in to comment.