generated from astrandb/hass_cc_template
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #351 from astrandb/DeviceAutomations
Add device automation triggers and conditions
- Loading branch information
Showing
3 changed files
with
188 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""Provide the device conditions for Miele integration.""" | ||
from __future__ import annotations | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.const import ( | ||
ATTR_ENTITY_ID, | ||
CONF_CONDITION, | ||
CONF_DEVICE_ID, | ||
CONF_DOMAIN, | ||
CONF_ENTITY_ID, | ||
CONF_TYPE, | ||
) | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.helpers import ( | ||
condition, | ||
config_validation as cv, | ||
entity_registry as er, | ||
) | ||
from homeassistant.helpers.typing import ConfigType, TemplateVarsType | ||
|
||
from .const import DOMAIN, STATE_STATUS | ||
|
||
CONDITION_TYPES = list(STATE_STATUS.values()) | ||
|
||
CONDITION_SCHEMA = cv.DEVICE_CONDITION_BASE_SCHEMA.extend( | ||
{ | ||
vol.Required(CONF_ENTITY_ID): cv.entity_id, | ||
vol.Required(CONF_TYPE): vol.In(CONDITION_TYPES), | ||
} | ||
) | ||
|
||
|
||
async def async_get_conditions( | ||
hass: HomeAssistant, device_id: str | ||
) -> list[dict[str, str]]: | ||
"""List device conditions for miele devices.""" | ||
registry = er.async_get(hass) | ||
conditions = [] | ||
|
||
# Get all the integrations entities for this device | ||
for entry in er.async_entries_for_device(registry, device_id): | ||
if entry.translation_key == "status": | ||
# 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 += [ | ||
{**base_condition, CONF_TYPE: cond} for cond in CONDITION_TYPES | ||
] | ||
|
||
return conditions | ||
|
||
|
||
@callback | ||
def async_condition_from_config( | ||
hass: HomeAssistant, config: ConfigType | ||
) -> condition.ConditionCheckerType: | ||
"""Create a function to test a device condition.""" | ||
state = config[CONF_TYPE] | ||
|
||
@callback | ||
def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool: | ||
"""Test if an entity is a certain state.""" | ||
return condition.state(hass, config[ATTR_ENTITY_ID], state) | ||
|
||
return test_is_state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"""Provides device triggers for miele integration.""" | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA | ||
from homeassistant.components.homeassistant.triggers import state as state_trigger | ||
from homeassistant.const import ( | ||
CONF_DEVICE_ID, | ||
CONF_DOMAIN, | ||
CONF_ENTITY_ID, | ||
CONF_PLATFORM, | ||
CONF_TYPE, | ||
) | ||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant | ||
from homeassistant.helpers import config_validation as cv, entity_registry as er | ||
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo | ||
from homeassistant.helpers.typing import ConfigType | ||
|
||
from .const import DOMAIN, STATE_STATUS | ||
|
||
TRIGGER_TYPES = list(STATE_STATUS.values()) | ||
|
||
TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend( | ||
{ | ||
vol.Required(CONF_ENTITY_ID): cv.entity_id, | ||
vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES), | ||
} | ||
) | ||
|
||
|
||
async def async_get_triggers( | ||
hass: HomeAssistant, device_id: str | ||
) -> list[dict[str, Any]]: | ||
"""List device triggers for Miele devices.""" | ||
registry = er.async_get(hass) | ||
triggers = [] | ||
|
||
# Get all the integrations entities for this device | ||
for entry in er.async_entries_for_device(registry, device_id): | ||
if entry.translation_key == "status": | ||
# 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, | ||
} | ||
for state_value in TRIGGER_TYPES: | ||
triggers.append({**base_trigger, CONF_TYPE: state_value}) | ||
|
||
return triggers | ||
|
||
|
||
async def async_attach_trigger( | ||
hass: HomeAssistant, | ||
config: ConfigType, | ||
action: TriggerActionType, | ||
trigger_info: TriggerInfo, | ||
) -> CALLBACK_TYPE: | ||
"""Attach a trigger.""" | ||
|
||
to_state = config[CONF_TYPE] | ||
state_config = { | ||
state_trigger.CONF_PLATFORM: "state", | ||
CONF_ENTITY_ID: config[CONF_ENTITY_ID], | ||
state_trigger.CONF_TO: to_state, | ||
} | ||
state_config = await state_trigger.async_validate_trigger_config(hass, state_config) | ||
return await state_trigger.async_attach_trigger( | ||
hass, state_config, action, trigger_info, platform_type="device" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters