From 489c73b4da3089548e25b6a12a3de76427f01c23 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 31 May 2021 09:47:15 +0200 Subject: [PATCH] Simplify device action code (#51263) --- .../alarm_control_panel/device_action.py | 51 +++----------- .../components/climate/device_action.py | 24 +++---- .../components/cover/device_action.py | 69 ++++--------------- homeassistant/components/fan/device_action.py | 22 ++---- .../components/humidifier/device_action.py | 23 ++----- .../components/light/device_action.py | 40 +++++------ .../components/lock/device_action.py | 33 +++------ .../components/vacuum/device_action.py | 24 +++---- .../components/water_heater/device_action.py | 24 +++---- .../integration/device_action.py | 24 +++---- 10 files changed, 95 insertions(+), 239 deletions(-) diff --git a/homeassistant/components/alarm_control_panel/device_action.py b/homeassistant/components/alarm_control_panel/device_action.py index 506552f8a50..bb2188807bb 100644 --- a/homeassistant/components/alarm_control_panel/device_action.py +++ b/homeassistant/components/alarm_control_panel/device_action.py @@ -70,51 +70,22 @@ async def async_get_actions( supported_features = state.attributes[ATTR_SUPPORTED_FEATURES] + base_action = { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + } + # Add actions for each entity that belongs to this integration if supported_features & SUPPORT_ALARM_ARM_AWAY: - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "arm_away", - } - ) + actions.append({**base_action, CONF_TYPE: "arm_away"}) if supported_features & SUPPORT_ALARM_ARM_HOME: - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "arm_home", - } - ) + actions.append({**base_action, CONF_TYPE: "arm_home"}) if supported_features & SUPPORT_ALARM_ARM_NIGHT: - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "arm_night", - } - ) - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "disarm", - } - ) + actions.append({**base_action, CONF_TYPE: "arm_night"}) + actions.append({**base_action, CONF_TYPE: "disarm"}) if supported_features & SUPPORT_ALARM_TRIGGER: - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "trigger", - } - ) + actions.append({**base_action, CONF_TYPE: "trigger"}) return actions diff --git a/homeassistant/components/climate/device_action.py b/homeassistant/components/climate/device_action.py index 02474a47f96..de46eae0d7b 100644 --- a/homeassistant/components/climate/device_action.py +++ b/homeassistant/components/climate/device_action.py @@ -54,23 +54,15 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: if state is None: continue - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "set_hvac_mode", - } - ) + base_action = { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + } + + actions.append({**base_action, CONF_TYPE: "set_hvac_mode"}) if state.attributes[ATTR_SUPPORTED_FEATURES] & const.SUPPORT_PRESET_MODE: - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "set_preset_mode", - } - ) + actions.append({**base_action, CONF_TYPE: "set_preset_mode"}) return actions diff --git a/homeassistant/components/cover/device_action.py b/homeassistant/components/cover/device_action.py index 74eef8102df..24c8a0c8b3b 100644 --- a/homeassistant/components/cover/device_action.py +++ b/homeassistant/components/cover/device_action.py @@ -75,72 +75,29 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: supported_features = state.attributes[ATTR_SUPPORTED_FEATURES] # Add actions for each entity that belongs to this integration + base_action = { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + } + if supported_features & SUPPORT_SET_POSITION: - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "set_position", - } - ) + actions.append({**base_action, CONF_TYPE: "set_position"}) else: if supported_features & SUPPORT_OPEN: - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "open", - } - ) + actions.append({**base_action, CONF_TYPE: "open"}) if supported_features & SUPPORT_CLOSE: - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "close", - } - ) + actions.append({**base_action, CONF_TYPE: "close"}) if supported_features & SUPPORT_STOP: - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "stop", - } - ) + actions.append({**base_action, CONF_TYPE: "stop"}) if supported_features & SUPPORT_SET_TILT_POSITION: - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "set_tilt_position", - } - ) + actions.append({**base_action, CONF_TYPE: "set_tilt_position"}) else: if supported_features & SUPPORT_OPEN_TILT: - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "open_tilt", - } - ) + actions.append({**base_action, CONF_TYPE: "open_tilt"}) if supported_features & SUPPORT_CLOSE_TILT: - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "close_tilt", - } - ) + actions.append({**base_action, CONF_TYPE: "close_tilt"}) return actions diff --git a/homeassistant/components/fan/device_action.py b/homeassistant/components/fan/device_action.py index f4611d353d5..ddf6a76d3c8 100644 --- a/homeassistant/components/fan/device_action.py +++ b/homeassistant/components/fan/device_action.py @@ -38,22 +38,12 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: 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", - } - ) + base_action = { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + } + actions += [{**base_action, CONF_TYPE: action} for action in ACTION_TYPES] return actions diff --git a/homeassistant/components/humidifier/device_action.py b/homeassistant/components/humidifier/device_action.py index fa9c1eb71e7..7e11b65fd2e 100644 --- a/homeassistant/components/humidifier/device_action.py +++ b/homeassistant/components/humidifier/device_action.py @@ -52,28 +52,19 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: state = hass.states.get(entry.entity_id) - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "set_humidity", - } - ) + base_action = { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + } + actions.append({**base_action, CONF_TYPE: "set_humidity"}) # We need a state or else we can't populate the available modes. if state is None: continue if state.attributes[ATTR_SUPPORTED_FEATURES] & const.SUPPORT_MODES: - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "set_mode", - } - ) + actions.append({**base_action, CONF_TYPE: "set_mode"}) return actions diff --git a/homeassistant/components/light/device_action.py b/homeassistant/components/light/device_action.py index 3de2218d7c7..f533cc75586 100644 --- a/homeassistant/components/light/device_action.py +++ b/homeassistant/components/light/device_action.py @@ -11,7 +11,14 @@ from homeassistant.components.light import ( VALID_BRIGHTNESS_PCT, VALID_FLASH, ) -from homeassistant.const import ATTR_ENTITY_ID, CONF_DOMAIN, CONF_TYPE, SERVICE_TURN_ON +from homeassistant.const import ( + ATTR_ENTITY_ID, + CONF_DEVICE_ID, + CONF_DOMAIN, + CONF_ENTITY_ID, + CONF_TYPE, + SERVICE_TURN_ON, +) from homeassistant.core import Context, HomeAssistant, HomeAssistantError from homeassistant.helpers import config_validation as cv, entity_registry as er from homeassistant.helpers.entity import get_supported_features @@ -111,35 +118,22 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: supported_color_modes = get_supported_color_modes(hass, entry.entity_id) supported_features = get_supported_features(hass, entry.entity_id) + base_action = { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + } + if brightness_supported(supported_color_modes): actions.extend( ( - { - CONF_TYPE: TYPE_BRIGHTNESS_INCREASE, - "device_id": device_id, - "entity_id": entry.entity_id, - "domain": DOMAIN, - }, - { - CONF_TYPE: TYPE_BRIGHTNESS_DECREASE, - "device_id": device_id, - "entity_id": entry.entity_id, - "domain": DOMAIN, - }, + {**base_action, CONF_TYPE: TYPE_BRIGHTNESS_INCREASE}, + {**base_action, CONF_TYPE: TYPE_BRIGHTNESS_DECREASE}, ) ) if supported_features & SUPPORT_FLASH: - actions.extend( - ( - { - CONF_TYPE: TYPE_FLASH, - "device_id": device_id, - "entity_id": entry.entity_id, - "domain": DOMAIN, - }, - ) - ) + actions.append({**base_action, CONF_TYPE: TYPE_FLASH}) return actions diff --git a/homeassistant/components/lock/device_action.py b/homeassistant/components/lock/device_action.py index cb0e2b0daad..06a133465aa 100644 --- a/homeassistant/components/lock/device_action.py +++ b/homeassistant/components/lock/device_action.py @@ -41,35 +41,20 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: 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", - } - ) + base_action = { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + } + + actions.append({**base_action, CONF_TYPE: "lock"}) + actions.append({**base_action, 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", - } - ) + actions.append({**base_action, CONF_TYPE: "open"}) return actions diff --git a/homeassistant/components/vacuum/device_action.py b/homeassistant/components/vacuum/device_action.py index 2308882469e..a4df68c3b93 100644 --- a/homeassistant/components/vacuum/device_action.py +++ b/homeassistant/components/vacuum/device_action.py @@ -36,22 +36,14 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: if entry.domain != DOMAIN: continue - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "clean", - } - ) - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "dock", - } - ) + base_action = { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + } + + actions.append({**base_action, CONF_TYPE: "clean"}) + actions.append({**base_action, CONF_TYPE: "dock"}) return actions diff --git a/homeassistant/components/water_heater/device_action.py b/homeassistant/components/water_heater/device_action.py index e1c84be8753..3662dee9a5e 100644 --- a/homeassistant/components/water_heater/device_action.py +++ b/homeassistant/components/water_heater/device_action.py @@ -37,22 +37,14 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: 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", - } - ) + base_action = { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + } + + actions.append({**base_action, CONF_TYPE: "turn_on"}) + actions.append({**base_action, CONF_TYPE: "turn_off"}) return actions diff --git a/script/scaffold/templates/device_action/integration/device_action.py b/script/scaffold/templates/device_action/integration/device_action.py index 3bd1c0b91b3..720e472851c 100644 --- a/script/scaffold/templates/device_action/integration/device_action.py +++ b/script/scaffold/templates/device_action/integration/device_action.py @@ -48,22 +48,14 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: # Add actions for each entity that belongs to this integration # TODO add your own actions. - 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", - } - ) + base_action = { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + } + + actions.append({**base_action, CONF_TYPE: "turn_on"}) + actions.append({**base_action, CONF_TYPE: "turn_off"}) return actions