From 5e96d2141410680f369202065b3c7fd526e0e7df Mon Sep 17 00:00:00 2001 From: Captone Habiyaremye Date: Fri, 16 Oct 2020 06:51:12 -0400 Subject: [PATCH] Add device actions to water_heater (#41417) Co-authored-by: Paulus Schoutsen --- .../components/water_heater/device_action.py | 75 +++++++++++++ .../components/water_heater/strings.json | 8 ++ .../water_heater/translations/en.json | 8 ++ .../water_heater/test_device_action.py | 104 ++++++++++++++++++ 4 files changed, 195 insertions(+) create mode 100644 homeassistant/components/water_heater/device_action.py create mode 100644 homeassistant/components/water_heater/strings.json create mode 100644 homeassistant/components/water_heater/translations/en.json create mode 100644 tests/components/water_heater/test_device_action.py diff --git a/homeassistant/components/water_heater/device_action.py b/homeassistant/components/water_heater/device_action.py new file mode 100644 index 00000000000..991929580d1 --- /dev/null +++ b/homeassistant/components/water_heater/device_action.py @@ -0,0 +1,75 @@ +"""Provides device automations for Water Heater.""" +from typing import List, Optional + +import voluptuous as vol + +from homeassistant.const import ( + ATTR_ENTITY_ID, + CONF_DEVICE_ID, + CONF_DOMAIN, + CONF_ENTITY_ID, + CONF_TYPE, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, +) +from homeassistant.core import Context, HomeAssistant +from homeassistant.helpers import entity_registry +import homeassistant.helpers.config_validation as cv + +from . import DOMAIN + +ACTION_TYPES = {"turn_on", "turn_off"} + +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 Water Heater devices.""" + registry = await entity_registry.async_get_registry(hass) + actions = [] + + for entry in entity_registry.async_entries_for_device(registry, device_id): + if entry.domain != DOMAIN: + continue + + actions.append( + { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + CONF_TYPE: "turn_on", + } + ) + actions.append( + { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + CONF_TYPE: "turn_off", + } + ) + + 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] == "turn_on": + service = SERVICE_TURN_ON + elif config[CONF_TYPE] == "turn_off": + service = SERVICE_TURN_OFF + + await hass.services.async_call( + DOMAIN, service, service_data, blocking=True, context=context + ) diff --git a/homeassistant/components/water_heater/strings.json b/homeassistant/components/water_heater/strings.json new file mode 100644 index 00000000000..8f5709ac155 --- /dev/null +++ b/homeassistant/components/water_heater/strings.json @@ -0,0 +1,8 @@ +{ + "device_automation": { + "action_type": { + "turn_on": "Turn on {entity_name}", + "turn_off": "Turn off {entity_name}" + } + } +} \ No newline at end of file diff --git a/homeassistant/components/water_heater/translations/en.json b/homeassistant/components/water_heater/translations/en.json new file mode 100644 index 00000000000..d6abfbb995d --- /dev/null +++ b/homeassistant/components/water_heater/translations/en.json @@ -0,0 +1,8 @@ +{ + "device_automation": { + "action_type": { + "turn_off": "Turn off {entity_name}", + "turn_on": "Turn on {entity_name}" + } + } +} \ No newline at end of file diff --git a/tests/components/water_heater/test_device_action.py b/tests/components/water_heater/test_device_action.py new file mode 100644 index 00000000000..3d3e70444b7 --- /dev/null +++ b/tests/components/water_heater/test_device_action.py @@ -0,0 +1,104 @@ +"""The tests for Water Heater device actions.""" +import pytest + +import homeassistant.components.automation as automation +from homeassistant.components.water_heater import DOMAIN +from homeassistant.helpers import device_registry +from homeassistant.setup import async_setup_component + +from tests.common import ( + MockConfigEntry, + assert_lists_same, + async_get_device_automations, + async_mock_service, + mock_device_registry, + mock_registry, +) + + +@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(hass, device_reg, entity_reg): + """Test we get the expected actions from a water_heater.""" + 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", "5678", device_id=device_entry.id) + expected_actions = [ + { + "domain": DOMAIN, + "type": "turn_on", + "device_id": device_entry.id, + "entity_id": "water_heater.test_5678", + }, + { + "domain": DOMAIN, + "type": "turn_off", + "device_id": device_entry.id, + "entity_id": "water_heater.test_5678", + }, + ] + actions = await async_get_device_automations(hass, "action", device_entry.id) + assert_lists_same(actions, expected_actions) + + +async def test_action(hass): + """Test for turn_on and turn_off actions.""" + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: [ + { + "trigger": { + "platform": "event", + "event_type": "test_event_turn_off", + }, + "action": { + "domain": DOMAIN, + "device_id": "abcdefgh", + "entity_id": "water_heater.entity", + "type": "turn_off", + }, + }, + { + "trigger": { + "platform": "event", + "event_type": "test_event_turn_on", + }, + "action": { + "domain": DOMAIN, + "device_id": "abcdefgh", + "entity_id": "water_heater.entity", + "type": "turn_on", + }, + }, + ] + }, + ) + + turn_off_calls = async_mock_service(hass, "water_heater", "turn_off") + turn_on_calls = async_mock_service(hass, "water_heater", "turn_on") + + hass.bus.async_fire("test_event_turn_off") + await hass.async_block_till_done() + assert len(turn_off_calls) == 1 + assert len(turn_on_calls) == 0 + + hass.bus.async_fire("test_event_turn_on") + await hass.async_block_till_done() + assert len(turn_off_calls) == 1 + assert len(turn_on_calls) == 1