mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
Simplify device condition code (#51266)
This commit is contained in:
parent
489c73b4da
commit
04e9acc20a
@ -79,61 +79,26 @@ async def async_get_conditions(
|
||||
supported_features = state.attributes[ATTR_SUPPORTED_FEATURES]
|
||||
|
||||
# Add conditions for each entity that belongs to this integration
|
||||
base_condition = {
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
}
|
||||
|
||||
conditions += [
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: CONDITION_DISARMED,
|
||||
},
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: CONDITION_TRIGGERED,
|
||||
},
|
||||
{**base_condition, CONF_TYPE: CONDITION_DISARMED},
|
||||
{**base_condition, CONF_TYPE: CONDITION_TRIGGERED},
|
||||
]
|
||||
if supported_features & SUPPORT_ALARM_ARM_HOME:
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: CONDITION_ARMED_HOME,
|
||||
}
|
||||
)
|
||||
conditions.append({**base_condition, CONF_TYPE: CONDITION_ARMED_HOME})
|
||||
if supported_features & SUPPORT_ALARM_ARM_AWAY:
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: CONDITION_ARMED_AWAY,
|
||||
}
|
||||
)
|
||||
conditions.append({**base_condition, CONF_TYPE: CONDITION_ARMED_AWAY})
|
||||
if supported_features & SUPPORT_ALARM_ARM_NIGHT:
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: CONDITION_ARMED_NIGHT,
|
||||
}
|
||||
)
|
||||
conditions.append({**base_condition, CONF_TYPE: CONDITION_ARMED_NIGHT})
|
||||
if supported_features & SUPPORT_ALARM_ARM_CUSTOM_BYPASS:
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: CONDITION_ARMED_CUSTOM_BYPASS,
|
||||
}
|
||||
{**base_condition, CONF_TYPE: CONDITION_ARMED_CUSTOM_BYPASS}
|
||||
)
|
||||
|
||||
return conditions
|
||||
|
@ -54,29 +54,20 @@ async def async_get_conditions(
|
||||
|
||||
state = hass.states.get(entry.entity_id)
|
||||
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_hvac_mode",
|
||||
}
|
||||
)
|
||||
base_condition = {
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
}
|
||||
|
||||
conditions.append({**base_condition, CONF_TYPE: "is_hvac_mode"})
|
||||
|
||||
if (
|
||||
state
|
||||
and state.attributes[ATTR_SUPPORTED_FEATURES] & const.SUPPORT_PRESET_MODE
|
||||
):
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_preset_mode",
|
||||
}
|
||||
)
|
||||
conditions.append({**base_condition, CONF_TYPE: "is_preset_mode"})
|
||||
|
||||
return conditions
|
||||
|
||||
|
@ -85,63 +85,21 @@ async def async_get_conditions(hass: HomeAssistant, device_id: str) -> list[dict
|
||||
supports_open_close = supported_features & (SUPPORT_OPEN | SUPPORT_CLOSE)
|
||||
|
||||
# Add conditions for each entity that belongs to this integration
|
||||
base_condition = {
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
}
|
||||
|
||||
if supports_open_close:
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_open",
|
||||
}
|
||||
)
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_closed",
|
||||
}
|
||||
)
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_opening",
|
||||
}
|
||||
)
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_closing",
|
||||
}
|
||||
)
|
||||
conditions += [
|
||||
{**base_condition, CONF_TYPE: cond} for cond in STATE_CONDITION_TYPES
|
||||
]
|
||||
if supported_features & SUPPORT_SET_POSITION:
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_position",
|
||||
}
|
||||
)
|
||||
conditions.append({**base_condition, CONF_TYPE: "is_position"})
|
||||
if supported_features & SUPPORT_SET_TILT_POSITION:
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_tilt_position",
|
||||
}
|
||||
)
|
||||
conditions.append({**base_condition, CONF_TYPE: "is_tilt_position"})
|
||||
|
||||
return conditions
|
||||
|
||||
|
@ -43,24 +43,14 @@ async def async_get_conditions(
|
||||
continue
|
||||
|
||||
# Add conditions for each entity that belongs to this integration
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_home",
|
||||
}
|
||||
)
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_not_home",
|
||||
}
|
||||
)
|
||||
base_condition = {
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
}
|
||||
|
||||
conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES]
|
||||
|
||||
return conditions
|
||||
|
||||
|
@ -42,24 +42,14 @@ async def async_get_conditions(
|
||||
if entry.domain != DOMAIN:
|
||||
continue
|
||||
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_on",
|
||||
}
|
||||
)
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_off",
|
||||
}
|
||||
)
|
||||
base_condition = {
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
}
|
||||
|
||||
conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES]
|
||||
|
||||
return conditions
|
||||
|
||||
|
@ -41,24 +41,14 @@ async def async_get_conditions(hass: HomeAssistant, device_id: str) -> list[dict
|
||||
continue
|
||||
|
||||
# Add conditions for each entity that belongs to this integration
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_locked",
|
||||
}
|
||||
)
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_unlocked",
|
||||
}
|
||||
)
|
||||
base_condition = {
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
}
|
||||
|
||||
conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES]
|
||||
|
||||
return conditions
|
||||
|
||||
|
@ -46,51 +46,14 @@ async def async_get_conditions(
|
||||
continue
|
||||
|
||||
# Add conditions for each entity that belongs to this integration
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_on",
|
||||
}
|
||||
)
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_off",
|
||||
}
|
||||
)
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_idle",
|
||||
}
|
||||
)
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_paused",
|
||||
}
|
||||
)
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_playing",
|
||||
}
|
||||
)
|
||||
base_condition = {
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
}
|
||||
|
||||
conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES]
|
||||
|
||||
return conditions
|
||||
|
||||
|
@ -40,24 +40,14 @@ async def async_get_conditions(
|
||||
if entry.domain != DOMAIN:
|
||||
continue
|
||||
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_cleaning",
|
||||
}
|
||||
)
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_docked",
|
||||
}
|
||||
)
|
||||
base_condition = {
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
}
|
||||
|
||||
conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES]
|
||||
|
||||
return conditions
|
||||
|
||||
|
@ -45,24 +45,14 @@ async def async_get_conditions(
|
||||
|
||||
# Add conditions for each entity that belongs to this integration
|
||||
# TODO add your own conditions.
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_on",
|
||||
}
|
||||
)
|
||||
conditions.append(
|
||||
{
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "is_off",
|
||||
}
|
||||
)
|
||||
base_condition = {
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
}
|
||||
|
||||
conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES]
|
||||
|
||||
return conditions
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user