From 0ba4ee139838b48c76f23b032146f65218e4fd3a Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 8 Oct 2019 19:06:17 +0200 Subject: [PATCH] Validate generated device actions (#27262) * Validate generated actions * Use hass.services.async_call instead of service.async_call_from_config --- .../components/device_automation/toggle_entity.py | 12 +++++------- homeassistant/components/zha/device_action.py | 11 ++++------- .../device_action/tests/test_device_action.py | 5 +++-- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/device_automation/toggle_entity.py b/homeassistant/components/device_automation/toggle_entity.py index 29110144c14..98a1af9c4ca 100644 --- a/homeassistant/components/device_automation/toggle_entity.py +++ b/homeassistant/components/device_automation/toggle_entity.py @@ -17,6 +17,7 @@ from homeassistant.components.device_automation.const import ( CONF_TURNED_ON, ) from homeassistant.const import ( + ATTR_ENTITY_ID, CONF_CONDITION, CONF_ENTITY_ID, CONF_FOR, @@ -24,7 +25,7 @@ from homeassistant.const import ( CONF_TYPE, ) from homeassistant.helpers.entity_registry import async_entries_for_device -from homeassistant.helpers import condition, config_validation as cv, service +from homeassistant.helpers import condition, config_validation as cv from homeassistant.helpers.typing import ConfigType, TemplateVarsType from . import TRIGGER_BASE_SCHEMA @@ -112,13 +113,10 @@ async def async_call_action_from_config( else: action = "toggle" - service_action = { - service.CONF_SERVICE: "{}.{}".format(domain, action), - CONF_ENTITY_ID: config[CONF_ENTITY_ID], - } + service_data = {ATTR_ENTITY_ID: config[CONF_ENTITY_ID]} - await service.async_call_from_config( - hass, service_action, blocking=True, variables=variables, context=context + await hass.services.async_call( + domain, action, service_data, blocking=True, context=context ) diff --git a/homeassistant/components/zha/device_action.py b/homeassistant/components/zha/device_action.py index 460676a75a0..60cfa0eec00 100644 --- a/homeassistant/components/zha/device_action.py +++ b/homeassistant/components/zha/device_action.py @@ -5,7 +5,7 @@ import voluptuous as vol from homeassistant.const import CONF_DEVICE_ID, CONF_DOMAIN, CONF_TYPE from homeassistant.core import Context, HomeAssistant -from homeassistant.helpers import config_validation as cv, service +from homeassistant.helpers import config_validation as cv from homeassistant.helpers.typing import ConfigType, TemplateVarsType from . import DOMAIN @@ -78,13 +78,10 @@ async def _execute_service_based_action( service_name = SERVICE_NAMES[action_type] zha_device = await async_get_zha_device(hass, config[CONF_DEVICE_ID]) - service_action = { - service.CONF_SERVICE: "{}.{}".format(DOMAIN, service_name), - ATTR_DATA: {ATTR_IEEE: str(zha_device.ieee)}, - } + service_data = {ATTR_IEEE: str(zha_device.ieee)} - await service.async_call_from_config( - hass, service_action, blocking=True, variables=variables, context=context + await hass.services.async_call( + DOMAIN, service_name, service_data, blocking=True, context=context ) diff --git a/script/scaffold/templates/device_action/tests/test_device_action.py b/script/scaffold/templates/device_action/tests/test_device_action.py index f8a00bf1ec8..b65c8257531 100644 --- a/script/scaffold/templates/device_action/tests/test_device_action.py +++ b/script/scaffold/templates/device_action/tests/test_device_action.py @@ -8,6 +8,7 @@ from homeassistant.helpers import device_registry from tests.common import ( MockConfigEntry, + assert_lists_same, async_mock_service, mock_device_registry, mock_registry, @@ -28,7 +29,7 @@ def entity_reg(hass): async def test_get_actions(hass, device_reg, entity_reg): - """Test we get the expected actions from a switch.""" + """Test we get the expected actions from a NEW_DOMAIN.""" config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) device_entry = device_reg.async_get_or_create( @@ -51,7 +52,7 @@ async def test_get_actions(hass, device_reg, entity_reg): }, ] actions = await async_get_device_automations(hass, "action", device_entry.id) - assert actions == expected_actions + assert_lists_same(actions, expected_actions) async def test_action(hass):