mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
Add Switcher runner support (#79430)
* Add Switcher runner support * Retrigger docs check * Review suggestions * Move API strings to constants
This commit is contained in:
parent
180b296426
commit
5a51738b2f
@ -29,7 +29,7 @@ from .const import (
|
||||
)
|
||||
from .utils import async_start_bridge, async_stop_bridge
|
||||
|
||||
PLATFORMS = [Platform.CLIMATE, Platform.SENSOR, Platform.SWITCH]
|
||||
PLATFORMS = [Platform.CLIMATE, Platform.COVER, Platform.SENSOR, Platform.SWITCH]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
130
homeassistant/components/switcher_kis/cover.py
Normal file
130
homeassistant/components/switcher_kis/cover.py
Normal file
@ -0,0 +1,130 @@
|
||||
"""Switcher integration Cover platform."""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from aioswitcher.api import SwitcherBaseResponse, SwitcherType2Api
|
||||
from aioswitcher.device import DeviceCategory, ShutterDirection, SwitcherShutter
|
||||
|
||||
from homeassistant.components.cover import (
|
||||
ATTR_POSITION,
|
||||
CoverDeviceClass,
|
||||
CoverEntity,
|
||||
CoverEntityFeature,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
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
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
API_SET_POSITON = "set_position"
|
||||
API_STOP = "stop"
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Switcher cover from config entry."""
|
||||
|
||||
@callback
|
||||
def async_add_cover(coordinator: SwitcherDataUpdateCoordinator) -> None:
|
||||
"""Add cover from Switcher device."""
|
||||
if coordinator.data.device_type.category == DeviceCategory.SHUTTER:
|
||||
async_add_entities([SwitcherCoverEntity(coordinator)])
|
||||
|
||||
config_entry.async_on_unload(
|
||||
async_dispatcher_connect(hass, SIGNAL_DEVICE_ADD, async_add_cover)
|
||||
)
|
||||
|
||||
|
||||
class SwitcherCoverEntity(
|
||||
CoordinatorEntity[SwitcherDataUpdateCoordinator], CoverEntity
|
||||
):
|
||||
"""Representation of a Switcher cover entity."""
|
||||
|
||||
_attr_device_class = CoverDeviceClass.SHUTTER
|
||||
_attr_supported_features = (
|
||||
CoverEntityFeature.OPEN
|
||||
| CoverEntityFeature.CLOSE
|
||||
| CoverEntityFeature.SET_POSITION
|
||||
| CoverEntityFeature.STOP
|
||||
)
|
||||
|
||||
def __init__(self, coordinator: SwitcherDataUpdateCoordinator) -> None:
|
||||
"""Initialize the entity."""
|
||||
super().__init__(coordinator)
|
||||
|
||||
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._update_data()
|
||||
|
||||
@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) -> None:
|
||||
"""Update data from device."""
|
||||
data: SwitcherShutter = self.coordinator.data
|
||||
self._attr_current_cover_position = data.position
|
||||
self._attr_is_closed = data.position == 0
|
||||
self._attr_is_closing = data.direction == ShutterDirection.SHUTTER_DOWN
|
||||
self._attr_is_opening = data.direction == ShutterDirection.SHUTTER_UP
|
||||
|
||||
async def _async_call_api(self, api: str, *args: Any) -> None:
|
||||
"""Call Switcher API."""
|
||||
_LOGGER.debug("Calling api for %s, api: '%s', args: %s", self.name, api, args)
|
||||
response: SwitcherBaseResponse = None
|
||||
error = None
|
||||
|
||||
try:
|
||||
async with SwitcherType2Api(
|
||||
self.coordinator.data.ip_address, self.coordinator.data.device_id
|
||||
) as swapi:
|
||||
response = await getattr(swapi, api)(*args)
|
||||
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 api for {self.name} failed, api: '{api}', "
|
||||
f"args: {args}, response/error: {response or error}"
|
||||
)
|
||||
|
||||
async def async_close_cover(self, **kwargs: Any) -> None:
|
||||
"""Close cover."""
|
||||
await self._async_call_api(API_SET_POSITON, 0)
|
||||
|
||||
async def async_open_cover(self, **kwargs: Any) -> None:
|
||||
"""Open cover."""
|
||||
await self._async_call_api(API_SET_POSITON, 100)
|
||||
|
||||
async def async_set_cover_position(self, **kwargs: Any) -> None:
|
||||
"""Move the cover to a specific position."""
|
||||
await self._async_call_api(API_SET_POSITON, kwargs[ATTR_POSITION])
|
||||
|
||||
async def async_stop_cover(self, **_kwargs: Any) -> None:
|
||||
"""Stop the cover."""
|
||||
await self._async_call_api(API_STOP)
|
@ -3,7 +3,9 @@
|
||||
from aioswitcher.device import (
|
||||
DeviceState,
|
||||
DeviceType,
|
||||
ShutterDirection,
|
||||
SwitcherPowerPlug,
|
||||
SwitcherShutter,
|
||||
SwitcherThermostat,
|
||||
SwitcherWaterHeater,
|
||||
ThermostatFanLevel,
|
||||
@ -23,18 +25,22 @@ DUMMY_AUTO_SHUT_DOWN = "02:00:00"
|
||||
DUMMY_DEVICE_ID1 = "a123bc"
|
||||
DUMMY_DEVICE_ID2 = "cafe12"
|
||||
DUMMY_DEVICE_ID3 = "bada77"
|
||||
DUMMY_DEVICE_ID4 = "bbd164"
|
||||
DUMMY_DEVICE_NAME1 = "Plug 23BC"
|
||||
DUMMY_DEVICE_NAME2 = "Heater FE12"
|
||||
DUMMY_DEVICE_NAME3 = "Breeze AB39"
|
||||
DUMMY_DEVICE_NAME4 = "Runner DD77"
|
||||
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_IP_ADDRESS4 = "192.168.100.160"
|
||||
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_MAC_ADDRESS4 = "A1:B2:C3:45:67:DB"
|
||||
DUMMY_PHONE_ID = "1234"
|
||||
DUMMY_POWER_CONSUMPTION1 = 100
|
||||
DUMMY_POWER_CONSUMPTION2 = 2780
|
||||
@ -46,6 +52,8 @@ DUMMY_TARGET_TEMPERATURE = 23
|
||||
DUMMY_FAN_LEVEL = ThermostatFanLevel.LOW
|
||||
DUMMY_SWING = ThermostatSwing.OFF
|
||||
DUMMY_REMOTE_ID = "ELEC7001"
|
||||
DUMMY_POSITION = 54
|
||||
DUMMY_DIRECTION = ShutterDirection.SHUTTER_STOP
|
||||
|
||||
YAML_CONFIG = {
|
||||
DOMAIN: {
|
||||
@ -79,6 +87,17 @@ DUMMY_WATER_HEATER_DEVICE = SwitcherWaterHeater(
|
||||
DUMMY_AUTO_SHUT_DOWN,
|
||||
)
|
||||
|
||||
DUMMY_SHUTTER_DEVICE = SwitcherShutter(
|
||||
DeviceType.RUNNER,
|
||||
DeviceState.ON,
|
||||
DUMMY_DEVICE_ID4,
|
||||
DUMMY_IP_ADDRESS4,
|
||||
DUMMY_MAC_ADDRESS4,
|
||||
DUMMY_DEVICE_NAME4,
|
||||
DUMMY_POSITION,
|
||||
DUMMY_DIRECTION,
|
||||
)
|
||||
|
||||
DUMMY_THERMOSTAT_DEVICE = SwitcherThermostat(
|
||||
DeviceType.BREEZE,
|
||||
DeviceState.ON,
|
||||
|
183
tests/components/switcher_kis/test_cover.py
Normal file
183
tests/components/switcher_kis/test_cover.py
Normal file
@ -0,0 +1,183 @@
|
||||
"""Test the Switcher cover platform."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from aioswitcher.api import SwitcherBaseResponse
|
||||
from aioswitcher.device import ShutterDirection
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.cover import (
|
||||
ATTR_CURRENT_POSITION,
|
||||
ATTR_POSITION,
|
||||
DOMAIN as COVER_DOMAIN,
|
||||
SERVICE_CLOSE_COVER,
|
||||
SERVICE_OPEN_COVER,
|
||||
SERVICE_SET_COVER_POSITION,
|
||||
SERVICE_STOP_COVER,
|
||||
STATE_CLOSED,
|
||||
STATE_CLOSING,
|
||||
STATE_OPEN,
|
||||
STATE_OPENING,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.util import slugify
|
||||
|
||||
from . import init_integration
|
||||
from .consts import DUMMY_SHUTTER_DEVICE as DEVICE
|
||||
|
||||
ENTITY_ID = f"{COVER_DOMAIN}.{slugify(DEVICE.name)}"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mock_bridge", [[DEVICE]], indirect=True)
|
||||
async def test_cover(hass, mock_bridge, mock_api, monkeypatch):
|
||||
"""Test cover services."""
|
||||
await init_integration(hass)
|
||||
assert mock_bridge
|
||||
|
||||
# Test initial state - open
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.state == STATE_OPEN
|
||||
|
||||
# Test set position
|
||||
with patch(
|
||||
"homeassistant.components.switcher_kis.cover.SwitcherType2Api.set_position"
|
||||
) as mock_control_device:
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_SET_COVER_POSITION,
|
||||
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_POSITION: 77},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
monkeypatch.setattr(DEVICE, "position", 77)
|
||||
mock_bridge.mock_callbacks([DEVICE])
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_api.call_count == 2
|
||||
mock_control_device.assert_called_once_with(77)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.state == STATE_OPEN
|
||||
assert state.attributes[ATTR_CURRENT_POSITION] == 77
|
||||
|
||||
# Test open
|
||||
with patch(
|
||||
"homeassistant.components.switcher_kis.cover.SwitcherType2Api.set_position"
|
||||
) as mock_control_device:
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_OPEN_COVER,
|
||||
{ATTR_ENTITY_ID: ENTITY_ID},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
monkeypatch.setattr(DEVICE, "direction", ShutterDirection.SHUTTER_UP)
|
||||
mock_bridge.mock_callbacks([DEVICE])
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_api.call_count == 4
|
||||
mock_control_device.assert_called_once_with(100)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.state == STATE_OPENING
|
||||
|
||||
# Test close
|
||||
with patch(
|
||||
"homeassistant.components.switcher_kis.cover.SwitcherType2Api.set_position"
|
||||
) as mock_control_device:
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_CLOSE_COVER,
|
||||
{ATTR_ENTITY_ID: ENTITY_ID},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
monkeypatch.setattr(DEVICE, "direction", ShutterDirection.SHUTTER_DOWN)
|
||||
mock_bridge.mock_callbacks([DEVICE])
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_api.call_count == 6
|
||||
mock_control_device.assert_called_once_with(0)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.state == STATE_CLOSING
|
||||
|
||||
# Test stop
|
||||
with patch(
|
||||
"homeassistant.components.switcher_kis.cover.SwitcherType2Api.stop"
|
||||
) as mock_control_device:
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_STOP_COVER,
|
||||
{ATTR_ENTITY_ID: ENTITY_ID},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
monkeypatch.setattr(DEVICE, "direction", ShutterDirection.SHUTTER_STOP)
|
||||
mock_bridge.mock_callbacks([DEVICE])
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_api.call_count == 8
|
||||
mock_control_device.assert_called_once()
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.state == STATE_OPEN
|
||||
|
||||
# Test closed on position == 0
|
||||
monkeypatch.setattr(DEVICE, "position", 0)
|
||||
mock_bridge.mock_callbacks([DEVICE])
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.state == STATE_CLOSED
|
||||
assert state.attributes[ATTR_CURRENT_POSITION] == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mock_bridge", [[DEVICE]], indirect=True)
|
||||
async def test_cover_control_fail(hass, mock_bridge, mock_api):
|
||||
"""Test cover control fail."""
|
||||
await init_integration(hass)
|
||||
assert mock_bridge
|
||||
|
||||
# Test initial state - open
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.state == STATE_OPEN
|
||||
|
||||
# Test exception during set position
|
||||
with patch(
|
||||
"homeassistant.components.switcher_kis.cover.SwitcherType2Api.set_position",
|
||||
side_effect=RuntimeError("fake error"),
|
||||
) as mock_control_device:
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_SET_COVER_POSITION,
|
||||
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_POSITION: 44},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert mock_api.call_count == 2
|
||||
mock_control_device.assert_called_once_with(44)
|
||||
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 == STATE_OPEN
|
||||
|
||||
# Test error response during set position
|
||||
with patch(
|
||||
"homeassistant.components.switcher_kis.cover.SwitcherType2Api.set_position",
|
||||
return_value=SwitcherBaseResponse(None),
|
||||
) as mock_control_device:
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_SET_COVER_POSITION,
|
||||
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_POSITION: 27},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert mock_api.call_count == 4
|
||||
mock_control_device.assert_called_once_with(27)
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.state == STATE_UNAVAILABLE
|
Loading…
x
Reference in New Issue
Block a user