8000 Simplify device trigger code by emontnemery · Pull Request #48507 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 16 additions & 40 deletions homeassistant/components/alarm_control_panel/device_trigger.py
10000
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,8 @@

from . import DOMAIN

TRIGGER_TYPES = {
"triggered",
"disarmed",
"arming",
"armed_home",
"armed_away",
"armed_night",
}
BASIC_TRIGGER_TYPES = {"triggered", "disarmed", "arming"}
TRIGGER_TYPES = BASIC_TRIGGER_TYPES | {"armed_home", "armed_away", "armed_night"}

TRIGGER_SCHEMA = TRIGGER_BASE_SCHEMA.extend(
{
Expand Down Expand Up @@ -67,56 +61,38 @@ async def async_get_triggers(hass: HomeAssistant, device_id: str) -> list[dict]:
supported_features = entity_state.attributes[ATTR_SUPPORTED_FEATURES]

# Add triggers for each entity that belongs to this integration
base_trigger = {
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
}

triggers += [
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
CONF_TYPE: "disarmed",
},
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
CONF_TYPE: "triggered",
},
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
CONF_TYPE: "arming",
},
**base_trigger,
CONF_TYPE: trigger,
}
for trigger in BASIC_TRIGGER_TYPES
]
if supported_features & SUPPORT_ALARM_ARM_HOME:
triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
**base_trigger,
CONF_TYPE: "armed_home",
}
)
if supported_features & SUPPORT_ALARM_ARM_AWAY:
triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
**base_trigger,
CONF_TYPE: "armed_away",
}
)
if supported_features & SUPPORT_ALARM_ARM_NIGHT:
triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
**base_trigger,
CONF_TYPE: "armed_night",
}
)
Expand Down
22 changes: 10 additions & 12 deletions homeassistant/components/climate/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,32 @@ async def async_get_triggers(hass: HomeAssistant, device_id: str) -> list[dict]:
state = hass.states.get(entry.entity_id)

# Add triggers for each entity that belongs to this integration
base_trigger = {
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
}

triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
**base_trigger,
CONF_TYPE: "hvac_mode_changed",
}
)

if state and const.ATTR_CURRENT_TEMPERATURE in state.attributes:
triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
**base_trigger,
CONF_TYPE: "current_temperature_changed",
}
)

if state and const.ATTR_CURRENT_HUMIDITY in state.attributes:
triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
**base_trigger,
CONF_TYPE: "current_humidity_changed",
}
)
Expand Down
56 changes: 14 additions & 42 deletions homeassistant/components/cover/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,60 +85,32 @@ async def async_get_triggers(hass: HomeAssistant, device_id: str) -> list[dict]:
supports_open_close = supported_features & (SUPPORT_OPEN | SUPPORT_CLOSE)

# Add triggers for each entity that belongs to this integration
base_trigger = {
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
}

if supports_open_close:
triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
CONF_TYPE: "opened",
}
)
triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
CONF_TYPE: "closed",
}
)
triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
CONF_TYPE: "opening",
}
)
triggers.append(
triggers += [
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
CONF_TYPE: "closing",
**base_trigger,
CONF_TYPE: trigger,
}
)
for trigger in STATE_TRIGGER_TYPES
]
if supported_features & SUPPORT_SET_POSITION:
triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
**base_trigger,
CONF_TYPE: "position",
}
)
if supported_features & SUPPORT_SET_TILT_POSITION:
triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
**base_trigger,
CONF_TYPE: "tilt_position",
}
)
Expand Down
6D4E
3 changes: 0 additions & 3 deletions homeassistant/components/device_automation/toggle_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,12 @@ async def async_attach_trigger(
"""Listen for state changes based on configuration."""
trigger_type = config[CONF_TYPE]
if trigger_type == CONF_TURNED_ON:
from_state = "off"
to_state = "on"
else:
from_state = "on"
to_state = "off"
state_config = {
CONF_PLATFORM: "state",
state_trigger.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
state_trigger.CONF_FROM: from_state,
state_trigger.CONF_TO: to_state,
}
if CONF_FOR in config:
Expand Down
16 changes: 4 additions & 12 deletions homeassistant/components/lock/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,16 @@ async def async_get_triggers(hass: HomeAssistant, device_id: str) -> list[dict]:
continue

# Add triggers for each entity that belongs to this integration
triggers.append(
triggers += [
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
CONF_TYPE: "locked",
CONF_TYPE: trigger,
}
)
triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
CONF_TYPE: "unlocked",
}
)
for trigger in TRIGGER_TYPES
]

return triggers

Expand Down
16 changes: 4 additions & 12 deletions homeassistant/components/vacuum/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,16 @@ async def async_get_triggers(hass: HomeAssistant, device_id: str) -> list[dict]:
if entry.domain != DOMAIN:
continue

triggers.append(
triggers += [
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
CONF_TYPE: "cleaning",
CONF_TYPE: trigger,
}
)
triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
CONF_TYPE: "docked",
}
)
for trigger in TRIGGER_TYPES
]

return triggers

Expand Down
0