mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +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]
|
supported_features = state.attributes[ATTR_SUPPORTED_FEATURES]
|
||||||
|
|
||||||
# Add conditions for each entity that belongs to this integration
|
# 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 += [
|
conditions += [
|
||||||
{
|
{**base_condition, CONF_TYPE: CONDITION_DISARMED},
|
||||||
CONF_CONDITION: "device",
|
{**base_condition, CONF_TYPE: CONDITION_TRIGGERED},
|
||||||
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,
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
if supported_features & SUPPORT_ALARM_ARM_HOME:
|
if supported_features & SUPPORT_ALARM_ARM_HOME:
|
||||||
conditions.append(
|
conditions.append({**base_condition, CONF_TYPE: CONDITION_ARMED_HOME})
|
||||||
{
|
|
||||||
CONF_CONDITION: "device",
|
|
||||||
CONF_DEVICE_ID: device_id,
|
|
||||||
CONF_DOMAIN: DOMAIN,
|
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
|
||||||
CONF_TYPE: CONDITION_ARMED_HOME,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if supported_features & SUPPORT_ALARM_ARM_AWAY:
|
if supported_features & SUPPORT_ALARM_ARM_AWAY:
|
||||||
conditions.append(
|
conditions.append({**base_condition, CONF_TYPE: CONDITION_ARMED_AWAY})
|
||||||
{
|
|
||||||
CONF_CONDITION: "device",
|
|
||||||
CONF_DEVICE_ID: device_id,
|
|
||||||
CONF_DOMAIN: DOMAIN,
|
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
|
||||||
CONF_TYPE: CONDITION_ARMED_AWAY,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if supported_features & SUPPORT_ALARM_ARM_NIGHT:
|
if supported_features & SUPPORT_ALARM_ARM_NIGHT:
|
||||||
conditions.append(
|
conditions.append({**base_condition, CONF_TYPE: CONDITION_ARMED_NIGHT})
|
||||||
{
|
|
||||||
CONF_CONDITION: "device",
|
|
||||||
CONF_DEVICE_ID: device_id,
|
|
||||||
CONF_DOMAIN: DOMAIN,
|
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
|
||||||
CONF_TYPE: CONDITION_ARMED_NIGHT,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if supported_features & SUPPORT_ALARM_ARM_CUSTOM_BYPASS:
|
if supported_features & SUPPORT_ALARM_ARM_CUSTOM_BYPASS:
|
||||||
conditions.append(
|
conditions.append(
|
||||||
{
|
{**base_condition, CONF_TYPE: CONDITION_ARMED_CUSTOM_BYPASS}
|
||||||
CONF_CONDITION: "device",
|
|
||||||
CONF_DEVICE_ID: device_id,
|
|
||||||
CONF_DOMAIN: DOMAIN,
|
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
|
||||||
CONF_TYPE: CONDITION_ARMED_CUSTOM_BYPASS,
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return conditions
|
return conditions
|
||||||
|
@ -54,29 +54,20 @@ async def async_get_conditions(
|
|||||||
|
|
||||||
state = hass.states.get(entry.entity_id)
|
state = hass.states.get(entry.entity_id)
|
||||||
|
|
||||||
conditions.append(
|
base_condition = {
|
||||||
{
|
CONF_CONDITION: "device",
|
||||||
CONF_CONDITION: "device",
|
CONF_DEVICE_ID: device_id,
|
||||||
CONF_DEVICE_ID: device_id,
|
CONF_DOMAIN: DOMAIN,
|
||||||
CONF_DOMAIN: DOMAIN,
|
CONF_ENTITY_ID: entry.entity_id,
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
}
|
||||||
CONF_TYPE: "is_hvac_mode",
|
|
||||||
}
|
conditions.append({**base_condition, CONF_TYPE: "is_hvac_mode"})
|
||||||
)
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
state
|
state
|
||||||
and state.attributes[ATTR_SUPPORTED_FEATURES] & const.SUPPORT_PRESET_MODE
|
and state.attributes[ATTR_SUPPORTED_FEATURES] & const.SUPPORT_PRESET_MODE
|
||||||
):
|
):
|
||||||
conditions.append(
|
conditions.append({**base_condition, CONF_TYPE: "is_preset_mode"})
|
||||||
{
|
|
||||||
CONF_CONDITION: "device",
|
|
||||||
CONF_DEVICE_ID: device_id,
|
|
||||||
CONF_DOMAIN: DOMAIN,
|
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
|
||||||
CONF_TYPE: "is_preset_mode",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return conditions
|
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)
|
supports_open_close = supported_features & (SUPPORT_OPEN | SUPPORT_CLOSE)
|
||||||
|
|
||||||
# Add conditions for each entity that belongs to this integration
|
# 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:
|
if supports_open_close:
|
||||||
conditions.append(
|
conditions += [
|
||||||
{
|
{**base_condition, CONF_TYPE: cond} for cond in STATE_CONDITION_TYPES
|
||||||
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",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if supported_features & SUPPORT_SET_POSITION:
|
if supported_features & SUPPORT_SET_POSITION:
|
||||||
conditions.append(
|
conditions.append({**base_condition, CONF_TYPE: "is_position"})
|
||||||
{
|
|
||||||
CONF_CONDITION: "device",
|
|
||||||
CONF_DEVICE_ID: device_id,
|
|
||||||
CONF_DOMAIN: DOMAIN,
|
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
|
||||||
CONF_TYPE: "is_position",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if supported_features & SUPPORT_SET_TILT_POSITION:
|
if supported_features & SUPPORT_SET_TILT_POSITION:
|
||||||
conditions.append(
|
conditions.append({**base_condition, CONF_TYPE: "is_tilt_position"})
|
||||||
{
|
|
||||||
CONF_CONDITION: "device",
|
|
||||||
CONF_DEVICE_ID: device_id,
|
|
||||||
CONF_DOMAIN: DOMAIN,
|
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
|
||||||
CONF_TYPE: "is_tilt_position",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return conditions
|
return conditions
|
||||||
|
|
||||||
|
@ -43,24 +43,14 @@ async def async_get_conditions(
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
# Add conditions for each entity that belongs to this integration
|
# Add conditions for each entity that belongs to this integration
|
||||||
conditions.append(
|
base_condition = {
|
||||||
{
|
CONF_CONDITION: "device",
|
||||||
CONF_CONDITION: "device",
|
CONF_DEVICE_ID: device_id,
|
||||||
CONF_DEVICE_ID: device_id,
|
CONF_DOMAIN: DOMAIN,
|
||||||
CONF_DOMAIN: DOMAIN,
|
CONF_ENTITY_ID: entry.entity_id,
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
}
|
||||||
CONF_TYPE: "is_home",
|
|
||||||
}
|
conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES]
|
||||||
)
|
|
||||||
conditions.append(
|
|
||||||
{
|
|
||||||
CONF_CONDITION: "device",
|
|
||||||
CONF_DEVICE_ID: device_id,
|
|
||||||
CONF_DOMAIN: DOMAIN,
|
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
|
||||||
CONF_TYPE: "is_not_home",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return conditions
|
return conditions
|
||||||
|
|
||||||
|
@ -42,24 +42,14 @@ async def async_get_conditions(
|
|||||||
if entry.domain != DOMAIN:
|
if entry.domain != DOMAIN:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
conditions.append(
|
base_condition = {
|
||||||
{
|
CONF_CONDITION: "device",
|
||||||
CONF_CONDITION: "device",
|
CONF_DEVICE_ID: device_id,
|
||||||
CONF_DEVICE_ID: device_id,
|
CONF_DOMAIN: DOMAIN,
|
||||||
CONF_DOMAIN: DOMAIN,
|
CONF_ENTITY_ID: entry.entity_id,
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
}
|
||||||
CONF_TYPE: "is_on",
|
|
||||||
}
|
conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES]
|
||||||
)
|
|
||||||
conditions.append(
|
|
||||||
{
|
|
||||||
CONF_CONDITION: "device",
|
|
||||||
CONF_DEVICE_ID: device_id,
|
|
||||||
CONF_DOMAIN: DOMAIN,
|
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
|
||||||
CONF_TYPE: "is_off",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return conditions
|
return conditions
|
||||||
|
|
||||||
|
@ -41,24 +41,14 @@ async def async_get_conditions(hass: HomeAssistant, device_id: str) -> list[dict
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
# Add conditions for each entity that belongs to this integration
|
# Add conditions for each entity that belongs to this integration
|
||||||
conditions.append(
|
base_condition = {
|
||||||
{
|
CONF_CONDITION: "device",
|
||||||
CONF_CONDITION: "device",
|
CONF_DEVICE_ID: device_id,
|
||||||
CONF_DEVICE_ID: device_id,
|
CONF_DOMAIN: DOMAIN,
|
||||||
CONF_DOMAIN: DOMAIN,
|
CONF_ENTITY_ID: entry.entity_id,
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
}
|
||||||
CONF_TYPE: "is_locked",
|
|
||||||
}
|
conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES]
|
||||||
)
|
|
||||||
conditions.append(
|
|
||||||
{
|
|
||||||
CONF_CONDITION: "device",
|
|
||||||
CONF_DEVICE_ID: device_id,
|
|
||||||
CONF_DOMAIN: DOMAIN,
|
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
|
||||||
CONF_TYPE: "is_unlocked",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return conditions
|
return conditions
|
||||||
|
|
||||||
|
@ -46,51 +46,14 @@ async def async_get_conditions(
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
# Add conditions for each entity that belongs to this integration
|
# Add conditions for each entity that belongs to this integration
|
||||||
conditions.append(
|
base_condition = {
|
||||||
{
|
CONF_CONDITION: "device",
|
||||||
CONF_CONDITION: "device",
|
CONF_DEVICE_ID: device_id,
|
||||||
CONF_DEVICE_ID: device_id,
|
CONF_DOMAIN: DOMAIN,
|
||||||
CONF_DOMAIN: DOMAIN,
|
CONF_ENTITY_ID: entry.entity_id,
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
}
|
||||||
CONF_TYPE: "is_on",
|
|
||||||
}
|
conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES]
|
||||||
)
|
|
||||||
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",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return conditions
|
return conditions
|
||||||
|
|
||||||
|
@ -40,24 +40,14 @@ async def async_get_conditions(
|
|||||||
if entry.domain != DOMAIN:
|
if entry.domain != DOMAIN:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
conditions.append(
|
base_condition = {
|
||||||
{
|
CONF_CONDITION: "device",
|
||||||
CONF_CONDITION: "device",
|
CONF_DEVICE_ID: device_id,
|
||||||
CONF_DEVICE_ID: device_id,
|
CONF_DOMAIN: DOMAIN,
|
||||||
CONF_DOMAIN: DOMAIN,
|
CONF_ENTITY_ID: entry.entity_id,
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
}
|
||||||
CONF_TYPE: "is_cleaning",
|
|
||||||
}
|
conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES]
|
||||||
)
|
|
||||||
conditions.append(
|
|
||||||
{
|
|
||||||
CONF_CONDITION: "device",
|
|
||||||
CONF_DEVICE_ID: device_id,
|
|
||||||
CONF_DOMAIN: DOMAIN,
|
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
|
||||||
CONF_TYPE: "is_docked",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return conditions
|
return conditions
|
||||||
|
|
||||||
|
@ -45,24 +45,14 @@ async def async_get_conditions(
|
|||||||
|
|
||||||
# Add conditions for each entity that belongs to this integration
|
# Add conditions for each entity that belongs to this integration
|
||||||
# TODO add your own conditions.
|
# TODO add your own conditions.
|
||||||
conditions.append(
|
base_condition = {
|
||||||
{
|
CONF_CONDITION: "device",
|
||||||
CONF_CONDITION: "device",
|
CONF_DEVICE_ID: device_id,
|
||||||
CONF_DEVICE_ID: device_id,
|
CONF_DOMAIN: DOMAIN,
|
||||||
CONF_DOMAIN: DOMAIN,
|
CONF_ENTITY_ID: entry.entity_id,
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
}
|
||||||
CONF_TYPE: "is_on",
|
|
||||||
}
|
conditions += [{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES]
|
||||||
)
|
|
||||||
conditions.append(
|
|
||||||
{
|
|
||||||
CONF_CONDITION: "device",
|
|
||||||
CONF_DEVICE_ID: device_id,
|
|
||||||
CONF_DOMAIN: DOMAIN,
|
|
||||||
CONF_ENTITY_ID: entry.entity_id,
|
|
||||||
CONF_TYPE: "is_off",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return conditions
|
return conditions
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user