mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Add switch to Yale Smart Living (#126366)
This commit is contained in:
parent
d66c28dd6a
commit
3137f75221
@ -40,6 +40,7 @@ PLATFORMS = [
|
|||||||
Platform.BUTTON,
|
Platform.BUTTON,
|
||||||
Platform.LOCK,
|
Platform.LOCK,
|
||||||
Platform.SENSOR,
|
Platform.SENSOR,
|
||||||
|
Platform.SWITCH,
|
||||||
]
|
]
|
||||||
|
|
||||||
STATE_MAP = {
|
STATE_MAP = {
|
||||||
|
@ -45,6 +45,7 @@ class YaleLockEntity(CoordinatorEntity[YaleDataUpdateCoordinator]):
|
|||||||
identifiers={(DOMAIN, lock.sid())},
|
identifiers={(DOMAIN, lock.sid())},
|
||||||
via_device=(DOMAIN, coordinator.entry.data[CONF_USERNAME]),
|
via_device=(DOMAIN, coordinator.entry.data[CONF_USERNAME]),
|
||||||
)
|
)
|
||||||
|
self.lock_data = lock
|
||||||
|
|
||||||
|
|
||||||
class YaleAlarmEntity(CoordinatorEntity[YaleDataUpdateCoordinator], Entity):
|
class YaleAlarmEntity(CoordinatorEntity[YaleDataUpdateCoordinator], Entity):
|
||||||
|
@ -58,7 +58,6 @@ class YaleDoorlock(YaleLockEntity, LockEntity):
|
|||||||
"""Initialize the Yale Lock Device."""
|
"""Initialize the Yale Lock Device."""
|
||||||
super().__init__(coordinator, lock)
|
super().__init__(coordinator, lock)
|
||||||
self._attr_code_format = rf"^\d{{{code_format}}}$"
|
self._attr_code_format = rf"^\d{{{code_format}}}$"
|
||||||
self.lock_data = lock
|
|
||||||
|
|
||||||
async def async_unlock(self, **kwargs: Any) -> None:
|
async def async_unlock(self, **kwargs: Any) -> None:
|
||||||
"""Send unlock command."""
|
"""Send unlock command."""
|
||||||
|
@ -6,5 +6,5 @@
|
|||||||
"documentation": "https://www.home-assistant.io/integrations/yale_smart_alarm",
|
"documentation": "https://www.home-assistant.io/integrations/yale_smart_alarm",
|
||||||
"iot_class": "cloud_polling",
|
"iot_class": "cloud_polling",
|
||||||
"loggers": ["yalesmartalarmclient"],
|
"loggers": ["yalesmartalarmclient"],
|
||||||
"requirements": ["yalesmartalarmclient==0.4.2"]
|
"requirements": ["yalesmartalarmclient==0.4.3"]
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,11 @@
|
|||||||
"panic": {
|
"panic": {
|
||||||
"name": "Panic button"
|
"name": "Panic button"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"switch": {
|
||||||
|
"autolock": {
|
||||||
|
"name": "Autolock"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"exceptions": {
|
"exceptions": {
|
||||||
|
59
homeassistant/components/yale_smart_alarm/switch.py
Normal file
59
homeassistant/components/yale_smart_alarm/switch.py
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
"""Switches for Yale Alarm."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from yalesmartalarmclient import YaleLock
|
||||||
|
|
||||||
|
from homeassistant.components.switch import SwitchEntity
|
||||||
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from . import YaleConfigEntry
|
||||||
|
from .coordinator import YaleDataUpdateCoordinator
|
||||||
|
from .entity import YaleLockEntity
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: YaleConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
|
"""Set up the Yale switch entry."""
|
||||||
|
|
||||||
|
coordinator = entry.runtime_data
|
||||||
|
|
||||||
|
async_add_entities(
|
||||||
|
YaleAutolockSwitch(coordinator, lock)
|
||||||
|
for lock in coordinator.locks
|
||||||
|
if lock.supports_lock_config()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class YaleAutolockSwitch(YaleLockEntity, SwitchEntity):
|
||||||
|
"""Representation of a Yale autolock switch."""
|
||||||
|
|
||||||
|
_attr_translation_key = "autolock"
|
||||||
|
|
||||||
|
def __init__(self, coordinator: YaleDataUpdateCoordinator, lock: YaleLock) -> None:
|
||||||
|
"""Initialize the Yale Autolock Switch."""
|
||||||
|
super().__init__(coordinator, lock)
|
||||||
|
self._attr_unique_id = f"{lock.sid()}-autolock"
|
||||||
|
self._attr_is_on = self.lock_data.autolock()
|
||||||
|
|
||||||
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
|
"""Turn the entity on."""
|
||||||
|
if await self.hass.async_add_executor_job(self.lock_data.set_autolock, True):
|
||||||
|
self._attr_is_on = True
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
|
"""Turn the entity off."""
|
||||||
|
if await self.hass.async_add_executor_job(self.lock_data.set_autolock, False):
|
||||||
|
self._attr_is_on = False
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_coordinator_update(self) -> None:
|
||||||
|
"""Handle updated data from the coordinator."""
|
||||||
|
self._attr_is_on = self.lock_data.autolock()
|
||||||
|
super()._handle_coordinator_update()
|
@ -3002,7 +3002,7 @@ xmltodict==0.13.0
|
|||||||
xs1-api-client==3.0.0
|
xs1-api-client==3.0.0
|
||||||
|
|
||||||
# homeassistant.components.yale_smart_alarm
|
# homeassistant.components.yale_smart_alarm
|
||||||
yalesmartalarmclient==0.4.2
|
yalesmartalarmclient==0.4.3
|
||||||
|
|
||||||
# homeassistant.components.august
|
# homeassistant.components.august
|
||||||
# homeassistant.components.yale
|
# homeassistant.components.yale
|
||||||
|
@ -2388,7 +2388,7 @@ xknxproject==3.7.1
|
|||||||
xmltodict==0.13.0
|
xmltodict==0.13.0
|
||||||
|
|
||||||
# homeassistant.components.yale_smart_alarm
|
# homeassistant.components.yale_smart_alarm
|
||||||
yalesmartalarmclient==0.4.2
|
yalesmartalarmclient==0.4.3
|
||||||
|
|
||||||
# homeassistant.components.august
|
# homeassistant.components.august
|
||||||
# homeassistant.components.yale
|
# homeassistant.components.yale
|
||||||
|
@ -64,6 +64,7 @@ async def load_config_entry(
|
|||||||
client.auth = Mock()
|
client.auth = Mock()
|
||||||
client.auth.get_authenticated = Mock(return_value=data)
|
client.auth.get_authenticated = Mock(return_value=data)
|
||||||
client.auth.post_authenticated = Mock(return_value={"code": "000"})
|
client.auth.post_authenticated = Mock(return_value={"code": "000"})
|
||||||
|
client.auth.put_authenticated = Mock(return_value={"code": "000"})
|
||||||
client.lock_api = YaleDoorManAPI(client.auth)
|
client.lock_api = YaleDoorManAPI(client.auth)
|
||||||
locks = [
|
locks = [
|
||||||
YaleLock(device, lock_api=client.lock_api)
|
YaleLock(device, lock_api=client.lock_api)
|
||||||
|
277
tests/components/yale_smart_alarm/snapshots/test_switch.ambr
Normal file
277
tests/components/yale_smart_alarm/snapshots/test_switch.ambr
Normal file
@ -0,0 +1,277 @@
|
|||||||
|
# serializer version: 1
|
||||||
|
# name: test_switch[load_platforms0][switch.device1_autolock-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'switch',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'switch.device1_autolock',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': None,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Autolock',
|
||||||
|
'platform': 'yale_smart_alarm',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'autolock',
|
||||||
|
'unique_id': '1111-autolock',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_switch[load_platforms0][switch.device1_autolock-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'friendly_name': 'Device1 Autolock',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'switch.device1_autolock',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'on',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_switch[load_platforms0][switch.device2_autolock-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'switch',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'switch.device2_autolock',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': None,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Autolock',
|
||||||
|
'platform': 'yale_smart_alarm',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'autolock',
|
||||||
|
'unique_id': '2222-autolock',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_switch[load_platforms0][switch.device2_autolock-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'friendly_name': 'Device2 Autolock',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'switch.device2_autolock',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'on',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_switch[load_platforms0][switch.device3_autolock-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'switch',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'switch.device3_autolock',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': None,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Autolock',
|
||||||
|
'platform': 'yale_smart_alarm',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'autolock',
|
||||||
|
'unique_id': '3333-autolock',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_switch[load_platforms0][switch.device3_autolock-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'friendly_name': 'Device3 Autolock',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'switch.device3_autolock',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'on',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_switch[load_platforms0][switch.device7_autolock-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'switch',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'switch.device7_autolock',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': None,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Autolock',
|
||||||
|
'platform': 'yale_smart_alarm',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'autolock',
|
||||||
|
'unique_id': '7777-autolock',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_switch[load_platforms0][switch.device7_autolock-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'friendly_name': 'Device7 Autolock',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'switch.device7_autolock',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'on',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_switch[load_platforms0][switch.device8_autolock-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'switch',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'switch.device8_autolock',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': None,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Autolock',
|
||||||
|
'platform': 'yale_smart_alarm',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'autolock',
|
||||||
|
'unique_id': '8888-autolock',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_switch[load_platforms0][switch.device8_autolock-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'friendly_name': 'Device8 Autolock',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'switch.device8_autolock',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'on',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_switch[load_platforms0][switch.device9_autolock-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'switch',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'switch.device9_autolock',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': None,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Autolock',
|
||||||
|
'platform': 'yale_smart_alarm',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'autolock',
|
||||||
|
'unique_id': '9999-autolock',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_switch[load_platforms0][switch.device9_autolock-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'friendly_name': 'Device9 Autolock',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'switch.device9_autolock',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'on',
|
||||||
|
})
|
||||||
|
# ---
|
46
tests/components/yale_smart_alarm/test_switch.py
Normal file
46
tests/components/yale_smart_alarm/test_switch.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
"""The test for the Yale smart living switch."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from unittest.mock import Mock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
from yalesmartalarmclient import YaleSmartAlarmData
|
||||||
|
|
||||||
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SERVICE_TURN_OFF
|
||||||
|
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, Platform
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry, snapshot_platform
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"load_platforms",
|
||||||
|
[[Platform.SWITCH]],
|
||||||
|
)
|
||||||
|
async def test_switch(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entity_registry: er.EntityRegistry,
|
||||||
|
load_config_entry: tuple[MockConfigEntry, Mock],
|
||||||
|
get_data: YaleSmartAlarmData,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
) -> None:
|
||||||
|
"""Test the Yale Smart Living autolock switch."""
|
||||||
|
|
||||||
|
await snapshot_platform(
|
||||||
|
hass, entity_registry, snapshot, load_config_entry[0].entry_id
|
||||||
|
)
|
||||||
|
|
||||||
|
await hass.services.async_call(
|
||||||
|
SWITCH_DOMAIN,
|
||||||
|
SERVICE_TURN_OFF,
|
||||||
|
{
|
||||||
|
ATTR_ENTITY_ID: "switch.device1_autolock",
|
||||||
|
},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
state = hass.states.get("switch.device1_autolock")
|
||||||
|
assert state.state == STATE_OFF
|
Loading…
x
Reference in New Issue
Block a user