Add Switcher Breeze support (#78596)

* Add switcher Breeze support

* Review comments and updates for aioswitcher
This commit is contained in:
Shay Levy 2022-10-01 19:14:55 +03:00 committed by GitHub
parent 9c9c8b324a
commit f1f01429f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 589 additions and 1 deletions

View File

@ -29,7 +29,7 @@ from .const import (
)
from .utils import async_start_bridge, async_stop_bridge
PLATFORMS = [Platform.SWITCH, Platform.SENSOR]
PLATFORMS = [Platform.CLIMATE, Platform.SENSOR, Platform.SWITCH]
_LOGGER = logging.getLogger(__name__)

View File

@ -0,0 +1,218 @@
"""Switcher integration Climate platform."""
from __future__ import annotations
import asyncio
from typing import Any, cast
from aioswitcher.api import SwitcherBaseResponse, SwitcherType2Api
from aioswitcher.api.remotes import SwitcherBreezeRemote, SwitcherBreezeRemoteManager
from aioswitcher.device import (
DeviceCategory,
DeviceState,
ThermostatFanLevel,
ThermostatMode,
ThermostatSwing,
)
from homeassistant.components.climate import (
FAN_AUTO,
FAN_HIGH,
FAN_LOW,
FAN_MEDIUM,
SWING_OFF,
SWING_VERTICAL,
ClimateEntity,
ClimateEntityFeature,
HVACMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import SwitcherDataUpdateCoordinator
from .const import SIGNAL_DEVICE_ADD
DEVICE_MODE_TO_HA = {
ThermostatMode.COOL: HVACMode.COOL,
ThermostatMode.HEAT: HVACMode.HEAT,
ThermostatMode.FAN: HVACMode.FAN_ONLY,
ThermostatMode.DRY: HVACMode.DRY,
ThermostatMode.AUTO: HVACMode.HEAT_COOL,
}
HA_TO_DEVICE_MODE = {value: key for key, value in DEVICE_MODE_TO_HA.items()}
DEVICE_FAN_TO_HA = {
ThermostatFanLevel.LOW: FAN_LOW,
ThermostatFanLevel.MEDIUM: FAN_MEDIUM,
ThermostatFanLevel.HIGH: FAN_HIGH,
ThermostatFanLevel.AUTO: FAN_AUTO,
}
HA_TO_DEVICE_FAN = {value: key for key, value in DEVICE_FAN_TO_HA.items()}
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Switcher climate from config entry."""
remote_manager = SwitcherBreezeRemoteManager()
async def async_add_climate(coordinator: SwitcherDataUpdateCoordinator) -> None:
"""Get remote and add climate from Switcher device."""
if coordinator.data.device_type.category == DeviceCategory.THERMOSTAT:
remote: SwitcherBreezeRemote = await hass.async_add_executor_job(
remote_manager.get_remote, coordinator.data.remote_id
)
async_add_entities([SwitcherClimateEntity(coordinator, remote)])
config_entry.async_on_unload(
async_dispatcher_connect(hass, SIGNAL_DEVICE_ADD, async_add_climate)
)
class SwitcherClimateEntity(
CoordinatorEntity[SwitcherDataUpdateCoordinator], ClimateEntity
):
"""Representation of a Switcher climate entity."""
def __init__(
self, coordinator: SwitcherDataUpdateCoordinator, remote: SwitcherBreezeRemote
) -> None:
"""Initialize the entity."""
super().__init__(coordinator)
self._remote = remote
self._attr_name = coordinator.name
self._attr_unique_id = f"{coordinator.device_id}-{coordinator.mac_address}"
self._attr_device_info = DeviceInfo(
connections={
(device_registry.CONNECTION_NETWORK_MAC, coordinator.mac_address)
}
)
self._attr_min_temp = remote.min_temperature
self._attr_max_temp = remote.max_temperature
self._attr_target_temperature_step = 1
self._attr_temperature_unit = TEMP_CELSIUS
self._attr_supported_features = 0
self._attr_hvac_modes = [HVACMode.OFF]
for mode in remote.modes_features:
self._attr_hvac_modes.append(DEVICE_MODE_TO_HA[mode])
features = remote.modes_features[mode]
if features["temperature_control"]:
self._attr_supported_features |= ClimateEntityFeature.TARGET_TEMPERATURE
if features["fan_levels"]:
self._attr_supported_features |= ClimateEntityFeature.FAN_MODE
if features["swing"] and not remote.separated_swing_command:
self._attr_supported_features |= ClimateEntityFeature.SWING_MODE
self._update_data(True)
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._update_data()
self.async_write_ha_state()
def _update_data(self, force_update: bool = False) -> None:
"""Update data from device."""
data = self.coordinator.data
features = self._remote.modes_features[data.mode]
if data.target_temperature == 0 and not force_update:
return
self._attr_current_temperature = cast(float, data.temperature)
self._attr_target_temperature = float(data.target_temperature)
self._attr_hvac_mode = HVACMode.OFF
if data.device_state == DeviceState.ON:
self._attr_hvac_mode = DEVICE_MODE_TO_HA[data.mode]
self._attr_fan_mode = None
self._attr_fan_modes = []
if features["fan_levels"]:
self._attr_fan_modes = [DEVICE_FAN_TO_HA[x] for x in features["fan_levels"]]
self._attr_fan_mode = DEVICE_FAN_TO_HA[data.fan_level]
self._attr_swing_mode = None
self._attr_swing_modes = []
if features["swing"]:
self._attr_swing_mode = SWING_OFF
self._attr_swing_modes = [SWING_VERTICAL, SWING_OFF]
if data.swing == ThermostatSwing.ON:
self._attr_swing_mode = SWING_VERTICAL
async def _async_control_breeze_device(self, **kwargs: Any) -> None:
"""Call Switcher Control Breeze API."""
response: SwitcherBaseResponse = None
error = None
try:
async with SwitcherType2Api(
self.coordinator.data.ip_address, self.coordinator.data.device_id
) as swapi:
response = await swapi.control_breeze_device(self._remote, **kwargs)
except (asyncio.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}"
)
async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature."""
if not self._remote.modes_features[self.coordinator.data.mode][
"temperature_control"
]:
raise HomeAssistantError(
"Current mode doesn't 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))
async def async_set_fan_mode(self, fan_mode: str) -> None:
"""Set new target fan mode."""
if not self._remote.modes_features[self.coordinator.data.mode]["fan_levels"]:
raise HomeAssistantError("Current mode doesn't support setting Fan Mode")
await self._async_control_breeze_device(fan_mode=HA_TO_DEVICE_FAN[fan_mode])
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set new target operation mode."""
if hvac_mode == hvac_mode.OFF:
await self._async_control_breeze_device(state=DeviceState.OFF)
else:
await self._async_control_breeze_device(
state=DeviceState.ON, mode=HA_TO_DEVICE_MODE[hvac_mode]
)
async def async_set_swing_mode(self, swing_mode: str) -> None:
"""Set new target swing operation."""
if not self._remote.modes_features[self.coordinator.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_mode=ThermostatSwing.ON)
else:
await self._async_control_breeze_device(swing_mode=ThermostatSwing.OFF)

View File

@ -50,6 +50,14 @@ def mock_api():
"homeassistant.components.switcher_kis.switch.SwitcherType1Api.disconnect",
new=api_mock,
),
patch(
"homeassistant.components.switcher_kis.climate.SwitcherType2Api.connect",
new=api_mock,
),
patch(
"homeassistant.components.switcher_kis.climate.SwitcherType2Api.disconnect",
new=api_mock,
),
]
for patcher in patchers:

View File

@ -4,7 +4,11 @@ from aioswitcher.device import (
DeviceState,
DeviceType,
SwitcherPowerPlug,
SwitcherThermostat,
SwitcherWaterHeater,
ThermostatFanLevel,
ThermostatMode,
ThermostatSwing,
)
from homeassistant.components.switcher_kis import (
@ -18,20 +22,30 @@ DUMMY_AUTO_OFF_SET = "01:30:00"
DUMMY_AUTO_SHUT_DOWN = "02:00:00"
DUMMY_DEVICE_ID1 = "a123bc"
DUMMY_DEVICE_ID2 = "cafe12"
DUMMY_DEVICE_ID3 = "bada77"
DUMMY_DEVICE_NAME1 = "Plug 23BC"
DUMMY_DEVICE_NAME2 = "Heater FE12"
DUMMY_DEVICE_NAME3 = "Breeze AB39"
DUMMY_DEVICE_PASSWORD = "12345678"
DUMMY_ELECTRIC_CURRENT1 = 0.5
DUMMY_ELECTRIC_CURRENT2 = 12.8
DUMMY_IP_ADDRESS1 = "192.168.100.157"
DUMMY_IP_ADDRESS2 = "192.168.100.158"
DUMMY_IP_ADDRESS3 = "192.168.100.159"
DUMMY_MAC_ADDRESS1 = "A1:B2:C3:45:67:D8"
DUMMY_MAC_ADDRESS2 = "A1:B2:C3:45:67:D9"
DUMMY_MAC_ADDRESS3 = "A1:B2:C3:45:67:DA"
DUMMY_PHONE_ID = "1234"
DUMMY_POWER_CONSUMPTION1 = 100
DUMMY_POWER_CONSUMPTION2 = 2780
DUMMY_REMAINING_TIME = "01:29:32"
DUMMY_TIMER_MINUTES_SET = "90"
DUMMY_THERMOSTAT_MODE = ThermostatMode.COOL
DUMMY_TEMPERATURE = 24.1
DUMMY_TARGET_TEMPERATURE = 23
DUMMY_FAN_LEVEL = ThermostatFanLevel.LOW
DUMMY_SWING = ThermostatSwing.OFF
DUMMY_REMOTE_ID = "ELEC7001"
YAML_CONFIG = {
DOMAIN: {
@ -65,4 +79,19 @@ DUMMY_WATER_HEATER_DEVICE = SwitcherWaterHeater(
DUMMY_AUTO_SHUT_DOWN,
)
DUMMY_THERMOSTAT_DEVICE = SwitcherThermostat(
DeviceType.BREEZE,
DeviceState.ON,
DUMMY_DEVICE_ID3,
DUMMY_IP_ADDRESS3,
DUMMY_MAC_ADDRESS3,
DUMMY_DEVICE_NAME3,
DUMMY_THERMOSTAT_MODE,
DUMMY_TEMPERATURE,
DUMMY_TARGET_TEMPERATURE,
DUMMY_FAN_LEVEL,
DUMMY_SWING,
DUMMY_REMOTE_ID,
)
DUMMY_SWITCHER_DEVICES = [DUMMY_PLUG_DEVICE, DUMMY_WATER_HEATER_DEVICE]

View File

@ -0,0 +1,333 @@
"""Test the Switcher climate platform."""
from unittest.mock import patch
from aioswitcher.api import SwitcherBaseResponse
from aioswitcher.device import (
DeviceState,
ThermostatFanLevel,
ThermostatMode,
ThermostatSwing,
)
import pytest
from homeassistant.components.climate import (
ATTR_FAN_MODE,
ATTR_HVAC_MODE,
ATTR_SWING_MODE,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
DOMAIN as CLIMATE_DOMAIN,
SERVICE_SET_FAN_MODE,
SERVICE_SET_HVAC_MODE,
SERVICE_SET_SWING_MODE,
SERVICE_SET_TEMPERATURE,
HVACMode,
)
from homeassistant.const import ATTR_ENTITY_ID, ATTR_TEMPERATURE, STATE_UNAVAILABLE
from homeassistant.exceptions import HomeAssistantError
from homeassistant.util import slugify
from . import init_integration
from .consts import DUMMY_THERMOSTAT_DEVICE as DEVICE
ENTITY_ID = f"{CLIMATE_DOMAIN}.{slugify(DEVICE.name)}"
@pytest.mark.parametrize("mock_bridge", [[DEVICE]], indirect=True)
async def test_climate_hvac_mode(hass, mock_bridge, mock_api, monkeypatch):
"""Test climate hvac mode service."""
await init_integration(hass)
assert mock_bridge
# Test initial hvac mode - cool
state = hass.states.get(ENTITY_ID)
assert state.state == HVACMode.COOL
# Test set hvac mode heat
with patch(
"homeassistant.components.switcher_kis.climate.SwitcherType2Api.control_breeze_device",
) as mock_control_device:
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_HVAC_MODE,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_HVAC_MODE: HVACMode.HEAT},
blocking=True,
)
monkeypatch.setattr(DEVICE, "mode", ThermostatMode.HEAT)
mock_bridge.mock_callbacks([DEVICE])
await hass.async_block_till_done()
assert mock_api.call_count == 2
mock_control_device.assert_called_once()
state = hass.states.get(ENTITY_ID)
assert state.state == HVACMode.HEAT
# Test set hvac mode off
with patch(
"homeassistant.components.switcher_kis.climate.SwitcherType2Api.control_breeze_device",
) as mock_control_device:
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_HVAC_MODE,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_HVAC_MODE: HVACMode.OFF},
blocking=True,
)
monkeypatch.setattr(DEVICE, "device_state", DeviceState.OFF)
mock_bridge.mock_callbacks([DEVICE])
await hass.async_block_till_done()
assert mock_api.call_count == 4
mock_control_device.assert_called_once()
state = hass.states.get(ENTITY_ID)
assert state.state == HVACMode.OFF
@pytest.mark.parametrize("mock_bridge", [[DEVICE]], indirect=True)
async def test_climate_temperature(hass, mock_bridge, mock_api, monkeypatch):
"""Test climate temperature service."""
await init_integration(hass)
assert mock_bridge
# Test initial target temperature
state = hass.states.get(ENTITY_ID)
assert state.attributes["temperature"] == 23
# Test set target temperature
with patch(
"homeassistant.components.switcher_kis.climate.SwitcherType2Api.control_breeze_device",
) as mock_control_device:
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_TEMPERATURE: 22},
blocking=True,
)
monkeypatch.setattr(DEVICE, "target_temperature", 22)
mock_bridge.mock_callbacks([DEVICE])
await hass.async_block_till_done()
assert mock_api.call_count == 2
mock_control_device.assert_called_once()
state = hass.states.get(ENTITY_ID)
assert state.attributes["temperature"] == 22
# Test set target temperature - incorrect params
with patch(
"homeassistant.components.switcher_kis.climate.SwitcherType2Api.control_breeze_device",
) as mock_control_device:
with pytest.raises(ValueError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
{
ATTR_ENTITY_ID: ENTITY_ID,
ATTR_TARGET_TEMP_LOW: 20,
ATTR_TARGET_TEMP_HIGH: 30,
},
blocking=True,
)
assert mock_api.call_count == 2
mock_control_device.assert_not_called()
@pytest.mark.parametrize("mock_bridge", [[DEVICE]], indirect=True)
async def test_climate_fan_level(hass, mock_bridge, mock_api, monkeypatch):
"""Test climate fan level service."""
await init_integration(hass)
assert mock_bridge
# Test initial fan level - low
state = hass.states.get(ENTITY_ID)
assert state.attributes["fan_mode"] == "low"
# Test set fan level to high
with patch(
"homeassistant.components.switcher_kis.climate.SwitcherType2Api.control_breeze_device",
) as mock_control_device:
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_FAN_MODE,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_FAN_MODE: "high"},
blocking=True,
)
monkeypatch.setattr(DEVICE, "fan_level", ThermostatFanLevel.HIGH)
mock_bridge.mock_callbacks([DEVICE])
await hass.async_block_till_done()
assert mock_api.call_count == 2
mock_control_device.assert_called_once()
state = hass.states.get(ENTITY_ID)
assert state.attributes["fan_mode"] == "high"
@pytest.mark.parametrize("mock_bridge", [[DEVICE]], indirect=True)
async def test_climate_swing(hass, mock_bridge, mock_api, monkeypatch):
"""Test climate swing service."""
await init_integration(hass)
assert mock_bridge
# Test initial swing mode
state = hass.states.get(ENTITY_ID)
assert state.attributes["swing_mode"] == "off"
# Test set swing mode on
with patch(
"homeassistant.components.switcher_kis.climate.SwitcherType2Api.control_breeze_device",
) as mock_control_device:
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_SWING_MODE,
{
ATTR_ENTITY_ID: ENTITY_ID,
ATTR_SWING_MODE: "vertical",
},
blocking=True,
)
monkeypatch.setattr(DEVICE, "swing", ThermostatSwing.ON)
mock_bridge.mock_callbacks([DEVICE])
await hass.async_block_till_done()
assert mock_api.call_count == 2
mock_control_device.assert_called_once()
state = hass.states.get(ENTITY_ID)
assert state.attributes["swing_mode"] == "vertical"
# Test set swing mode off
with patch(
"homeassistant.components.switcher_kis.climate.SwitcherType2Api.control_breeze_device",
) as mock_control_device:
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_SWING_MODE,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_SWING_MODE: "off"},
blocking=True,
)
monkeypatch.setattr(DEVICE, "swing", ThermostatSwing.OFF)
mock_bridge.mock_callbacks([DEVICE])
await hass.async_block_till_done()
assert mock_api.call_count == 4
mock_control_device.assert_called_once()
state = hass.states.get(ENTITY_ID)
assert state.attributes["swing_mode"] == "off"
@pytest.mark.parametrize("mock_bridge", [[DEVICE]], indirect=True)
async def test_control_device_fail(hass, mock_bridge, mock_api, monkeypatch):
"""Test control device fail."""
await init_integration(hass)
assert mock_bridge
# Test initial hvac mode - cool
state = hass.states.get(ENTITY_ID)
assert state.state == HVACMode.COOL
# Test exception during set hvac mode
with patch(
"homeassistant.components.switcher_kis.climate.SwitcherType2Api.control_breeze_device",
side_effect=RuntimeError("fake error"),
) as mock_control_device:
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_HVAC_MODE,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_HVAC_MODE: HVACMode.HEAT},
blocking=True,
)
assert mock_api.call_count == 2
mock_control_device.assert_called_once()
state = hass.states.get(ENTITY_ID)
assert state.state == STATE_UNAVAILABLE
# Make device available again
mock_bridge.mock_callbacks([DEVICE])
await hass.async_block_till_done()
state = hass.states.get(ENTITY_ID)
assert state.state == HVACMode.COOL
# Test error response during turn on
with patch(
"homeassistant.components.switcher_kis.climate.SwitcherType2Api.control_breeze_device",
return_value=SwitcherBaseResponse(None),
) as mock_control_device:
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_HVAC_MODE,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_HVAC_MODE: HVACMode.HEAT},
blocking=True,
)
assert mock_api.call_count == 4
mock_control_device.assert_called_once()
state = hass.states.get(ENTITY_ID)
assert state.state == STATE_UNAVAILABLE
@pytest.mark.parametrize("mock_bridge", [[DEVICE]], indirect=True)
async def test_bad_update_discard(hass, mock_bridge, mock_api, monkeypatch):
"""Test that a bad update from device is discarded."""
await init_integration(hass)
assert mock_bridge
# Test initial hvac mode - cool
state = hass.states.get(ENTITY_ID)
assert state.state == HVACMode.COOL
# Device send target temperature with 0 to indicate it doesn't have data
monkeypatch.setattr(DEVICE, "target_temperature", 0)
monkeypatch.setattr(DEVICE, "mode", ThermostatMode.HEAT)
mock_bridge.mock_callbacks([DEVICE])
await hass.async_block_till_done()
# Validate state did not change
state = hass.states.get(ENTITY_ID)
assert state.state == HVACMode.COOL
@pytest.mark.parametrize("mock_bridge", [[DEVICE]], indirect=True)
async def test_climate_control_errors(hass, mock_bridge, mock_api, monkeypatch):
"""Test control with settings not supported by device."""
await init_integration(hass)
assert mock_bridge
# Dry mode does not support setting fan, temperature, swing
monkeypatch.setattr(DEVICE, "mode", ThermostatMode.DRY)
mock_bridge.mock_callbacks([DEVICE])
await hass.async_block_till_done()
# Test exception when trying set temperature
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_TEMPERATURE: 24},
blocking=True,
)
# Test exception when trying set fan level
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_FAN_MODE,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_FAN_MODE: "high"},
blocking=True,
)
# Test exception when trying set swing mode
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_SWING_MODE,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_SWING_MODE: "off"},
blocking=True,
)