Simplify device action code (#51263)
parent
a8650f4e59
commit
489c73b4da
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue