From 6d083969c2bb22307c5c75693d5470cfab731642 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 18 Oct 2019 02:20:10 +0200 Subject: [PATCH] Add device action support to the lock integration (#27499) * Add device action support to the lock integration * Check that the enitity supports open service --- .../components/lock/device_action.py | 92 ++++++++++ homeassistant/components/lock/strings.json | 5 + tests/components/lock/test_device_action.py | 170 ++++++++++++++++++ .../custom_components/test/lock.py | 54 ++++++ 4 files changed, 321 insertions(+) create mode 100644 homeassistant/components/lock/device_action.py create mode 100644 tests/components/lock/test_device_action.py create mode 100644 tests/testing_config/custom_components/test/lock.py diff --git a/homeassistant/components/lock/device_action.py b/homeassistant/components/lock/device_action.py new file mode 100644 index 00000000000..c678bfc17cf --- /dev/null +++ b/homeassistant/components/lock/device_action.py @@ -0,0 +1,92 @@ +"""Provides device automations for Lock.""" +from typing import Optional, List +import voluptuous as vol + +from homeassistant.const import ( + ATTR_ENTITY_ID, + ATTR_SUPPORTED_FEATURES, + CONF_DOMAIN, + CONF_TYPE, + CONF_DEVICE_ID, + CONF_ENTITY_ID, + SERVICE_LOCK, + SERVICE_OPEN, + SERVICE_UNLOCK, +) +from homeassistant.core import HomeAssistant, Context +from homeassistant.helpers import entity_registry +import homeassistant.helpers.config_validation as cv +from . import DOMAIN, SUPPORT_OPEN + +ACTION_TYPES = {"lock", "unlock", "open"} + +ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend( + { + vol.Required(CONF_TYPE): vol.In(ACTION_TYPES), + vol.Required(CONF_ENTITY_ID): cv.entity_domain(DOMAIN), + } +) + + +async def async_get_actions(hass: HomeAssistant, device_id: str) -> List[dict]: + """List device actions for Lock devices.""" + registry = await entity_registry.async_get_registry(hass) + actions = [] + + # Get all the integrations entities for this device + for entry in entity_registry.async_entries_for_device(registry, device_id): + if entry.domain != DOMAIN: + continue + + # Add actions for each entity that belongs to this integration + actions.append( + { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + CONF_TYPE: "lock", + } + ) + actions.append( + { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + CONF_TYPE: "unlock", + } + ) + + state = hass.states.get(entry.entity_id) + if state: + features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0) + if features & (SUPPORT_OPEN): + actions.append( + { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + CONF_TYPE: "open", + } + ) + + return actions + + +async def async_call_action_from_config( + hass: HomeAssistant, config: dict, variables: dict, context: Optional[Context] +) -> None: + """Execute a device action.""" + config = ACTION_SCHEMA(config) + + service_data = {ATTR_ENTITY_ID: config[CONF_ENTITY_ID]} + + if config[CONF_TYPE] == "lock": + service = SERVICE_LOCK + elif config[CONF_TYPE] == "unlock": + service = SERVICE_UNLOCK + elif config[CONF_TYPE] == "open": + service = SERVICE_OPEN + + await hass.services.async_call( + DOMAIN, service, service_data, blocking=True, context=context + ) diff --git a/homeassistant/components/lock/strings.json b/homeassistant/components/lock/strings.json index baa9bc1604f..9c858916476 100644 --- a/homeassistant/components/lock/strings.json +++ b/homeassistant/components/lock/strings.json @@ -1,5 +1,10 @@ { "device_automation": { + "action_type": { + "lock": "Lock {entity_name}", + "open": "Open {entity_name}", + "unlock": "Unlock {entity_name}" + }, "condition_type": { "is_locked": "{entity_name} is locked", "is_unlocked": "{entity_name} is unlocked" diff --git a/tests/components/lock/test_device_action.py b/tests/components/lock/test_device_action.py new file mode 100644 index 00000000000..2006f9b3ff1 --- /dev/null +++ b/tests/components/lock/test_device_action.py @@ -0,0 +1,170 @@ +"""The tests for Lock device actions.""" +import pytest + +from homeassistant.components.lock import DOMAIN +from homeassistant.const import CONF_PLATFORM +from homeassistant.setup import async_setup_component +import homeassistant.components.automation as automation +from homeassistant.helpers import device_registry + +from tests.common import ( + MockConfigEntry, + assert_lists_same, + async_mock_service, + mock_device_registry, + mock_registry, + async_get_device_automations, +) + + +@pytest.fixture +def device_reg(hass): + """Return an empty, loaded, registry.""" + return mock_device_registry(hass) + + +@pytest.fixture +def entity_reg(hass): + """Return an empty, loaded, registry.""" + return mock_registry(hass) + + +async def test_get_actions_support_open(hass, device_reg, entity_reg): + """Test we get the expected actions from a lock which supports open.""" + platform = getattr(hass.components, f"test.{DOMAIN}") + platform.init() + assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) + + config_entry = MockConfigEntry(domain="test", data={}) + config_entry.add_to_hass(hass) + device_entry = device_reg.async_get_or_create( + config_entry_id=config_entry.entry_id, + connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + ) + entity_reg.async_get_or_create( + DOMAIN, + "test", + platform.ENTITIES["support_open"].unique_id, + device_id=device_entry.id, + ) + + expected_actions = [ + { + "domain": DOMAIN, + "type": "lock", + "device_id": device_entry.id, + "entity_id": "lock.support_open_lock", + }, + { + "domain": DOMAIN, + "type": "unlock", + "device_id": device_entry.id, + "entity_id": "lock.support_open_lock", + }, + { + "domain": DOMAIN, + "type": "open", + "device_id": device_entry.id, + "entity_id": "lock.support_open_lock", + }, + ] + actions = await async_get_device_automations(hass, "action", device_entry.id) + assert_lists_same(actions, expected_actions) + + +async def test_get_actions_not_support_open(hass, device_reg, entity_reg): + """Test we get the expected actions from a lock which doesn't support open.""" + platform = getattr(hass.components, f"test.{DOMAIN}") + platform.init() + assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) + + config_entry = MockConfigEntry(domain="test", data={}) + config_entry.add_to_hass(hass) + device_entry = device_reg.async_get_or_create( + config_entry_id=config_entry.entry_id, + connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + ) + entity_reg.async_get_or_create( + DOMAIN, + "test", + platform.ENTITIES["no_support_open"].unique_id, + device_id=device_entry.id, + ) + + expected_actions = [ + { + "domain": DOMAIN, + "type": "lock", + "device_id": device_entry.id, + "entity_id": "lock.no_support_open_lock", + }, + { + "domain": DOMAIN, + "type": "unlock", + "device_id": device_entry.id, + "entity_id": "lock.no_support_open_lock", + }, + ] + actions = await async_get_device_automations(hass, "action", device_entry.id) + assert_lists_same(actions, expected_actions) + + +async def test_action(hass): + """Test for lock actions.""" + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: [ + { + "trigger": {"platform": "event", "event_type": "test_event_lock"}, + "action": { + "domain": DOMAIN, + "device_id": "abcdefgh", + "entity_id": "lock.entity", + "type": "lock", + }, + }, + { + "trigger": {"platform": "event", "event_type": "test_event_unlock"}, + "action": { + "domain": DOMAIN, + "device_id": "abcdefgh", + "entity_id": "lock.entity", + "type": "unlock", + }, + }, + { + "trigger": {"platform": "event", "event_type": "test_event_open"}, + "action": { + "domain": DOMAIN, + "device_id": "abcdefgh", + "entity_id": "lock.entity", + "type": "open", + }, + }, + ] + }, + ) + + lock_calls = async_mock_service(hass, "lock", "lock") + unlock_calls = async_mock_service(hass, "lock", "unlock") + open_calls = async_mock_service(hass, "lock", "open") + + hass.bus.async_fire("test_event_lock") + await hass.async_block_till_done() + assert len(lock_calls) == 1 + assert len(unlock_calls) == 0 + assert len(open_calls) == 0 + + hass.bus.async_fire("test_event_unlock") + await hass.async_block_till_done() + assert len(lock_calls) == 1 + assert len(unlock_calls) == 1 + assert len(open_calls) == 0 + + hass.bus.async_fire("test_event_open") + await hass.async_block_till_done() + assert len(lock_calls) == 1 + assert len(unlock_calls) == 1 + assert len(open_calls) == 1 diff --git a/tests/testing_config/custom_components/test/lock.py b/tests/testing_config/custom_components/test/lock.py new file mode 100644 index 00000000000..db6ce38b097 --- /dev/null +++ b/tests/testing_config/custom_components/test/lock.py @@ -0,0 +1,54 @@ +""" +Provide a mock lock platform. + +Call init before using it in your tests to ensure clean test data. +""" +from homeassistant.components.lock import LockDevice, SUPPORT_OPEN +from tests.common import MockEntity + +ENTITIES = {} + + +def init(empty=False): + """Initialize the platform with entities.""" + global ENTITIES + + ENTITIES = ( + {} + if empty + else { + "support_open": MockLock( + name=f"Support open Lock", + is_locked=True, + supported_features=SUPPORT_OPEN, + unique_id="unique_support_open", + ), + "no_support_open": MockLock( + name=f"No support open Lock", + is_locked=True, + supported_features=0, + unique_id="unique_no_support_open", + ), + } + ) + + +async def async_setup_platform( + hass, config, async_add_entities_callback, discovery_info=None +): + """Return mock entities.""" + async_add_entities_callback(list(ENTITIES.values())) + + +class MockLock(MockEntity, LockDevice): + """Mock Lock class.""" + + @property + def is_locked(self): + """Return true if the lock is locked.""" + return self._handle("is_locked") + + @property + def supported_features(self): + """Return the class of this sensor.""" + return self._handle("supported_features")