-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Add device triggers to ISY994 integration #169694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shbatm
wants to merge
6
commits into
home-assistant:dev
Choose a base branch
from
shbatm:isy994-device-triggers
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
327b5a1
Add device triggers to ISY994 integration
shbatm 8e23c93
Address review: validate subtype node and use sensor domain for KPL b…
shbatm 3c9b9ef
Cover defensive branches in isy994 device_trigger tests
shbatm 9e6c48b
Potential fix for pull request finding
shbatm 210a2e2
Merge branch 'dev' into isy994-device-triggers
shbatm cfe9490
Rename on_fast/off_fast trigger types to fast_on/fast_off to align wi…
shbatm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -695,3 +695,5 @@ | |
| HTTP_PORT = 80 | ||
| SCHEME_HTTPS = "https" | ||
| HTTPS_PORT = 443 | ||
|
|
||
| EVENT_ISY994_CONTROL = "isy994_control" | ||
This file contains hidden or 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,191 @@ | ||
| """Provides device triggers for ISY994 Insteon devices. | ||
|
|
||
| Triggers are exposed for any entity whose underlying ISY node has a | ||
| ``node_def_id`` in :data:`SUPPORTED_NODE_DEF_IDS`. This covers SwitchLinc | ||
| dimmers and relays, KeypadLinc loads (dimmer or relay), and the secondary | ||
| ``KeypadButton_ADV`` child nodes that share the same on/off/fast/fade | ||
| command set. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Iterator | ||
| from typing import Final, cast | ||
|
|
||
| from pyisy.constants import ( | ||
| CMD_FADE_DOWN, | ||
| CMD_FADE_STOP, | ||
| CMD_FADE_UP, | ||
| CMD_OFF, | ||
| CMD_OFF_FAST, | ||
| CMD_ON, | ||
| CMD_ON_FAST, | ||
| ) | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components.device_automation import ( | ||
| DEVICE_TRIGGER_BASE_SCHEMA, | ||
| InvalidDeviceAutomationConfig, | ||
| ) | ||
| from homeassistant.components.homeassistant.triggers import event as event_trigger | ||
| from homeassistant.config_entries import ConfigEntryState | ||
| 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 device_registry as dr, entity_registry as er | ||
| from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo | ||
| from homeassistant.helpers.typing import ConfigType | ||
|
|
||
| from .const import DOMAIN, EVENT_ISY994_CONTROL | ||
| from .models import IsyData | ||
|
|
||
| CONF_SUBTYPE: Final = "subtype" | ||
|
|
||
| TRIGGER_TYPES: Final[dict[str, str]] = { | ||
| "on": CMD_ON, | ||
| "off": CMD_OFF, | ||
| "on_fast": CMD_ON_FAST, | ||
| "off_fast": CMD_OFF_FAST, | ||
| "fade_up": CMD_FADE_UP, | ||
| "fade_down": CMD_FADE_DOWN, | ||
| "fade_stop": CMD_FADE_STOP, | ||
| } | ||
|
|
||
| SUPPORTED_NODE_DEF_IDS: Final = frozenset( | ||
| { | ||
| "BallastRelayLampSwitch_ADV", | ||
| "DimmerLampSwitch_ADV", | ||
| "DimmerSwitchOnly_ADV", | ||
| "KeypadButton_ADV", | ||
| "KeypadDimmer_ADV", | ||
| "KeypadRelay_ADV", | ||
| "RelayLampOnly_ADV", | ||
| "RelayLampSwitch_ADV", | ||
| "RelaySwitchOnlyPlusQuery_ADV", | ||
| } | ||
|
shbatm marked this conversation as resolved.
|
||
| ) | ||
|
|
||
| TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend( | ||
| { | ||
| vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES), | ||
| vol.Required(CONF_SUBTYPE): str, | ||
| vol.Optional(CONF_ENTITY_ID): str, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def _resolve_isy_data(hass: HomeAssistant, device_id: str) -> IsyData | None: | ||
| """Return the IsyData backing the given Home Assistant device, if any.""" | ||
| device = dr.async_get(hass).async_get(device_id) | ||
| if device is None: | ||
| return None | ||
| for entry_id in device.config_entries: | ||
| entry = hass.config_entries.async_get_entry(entry_id) | ||
| if ( | ||
| entry is not None | ||
| and entry.domain == DOMAIN | ||
| and entry.state is ConfigEntryState.LOADED | ||
| ): | ||
| return cast(IsyData, entry.runtime_data) | ||
| return None | ||
|
|
||
|
|
||
| def _supported_button_entries( | ||
| hass: HomeAssistant, device_id: str, isy_data: IsyData | ||
| ) -> Iterator[tuple[er.RegistryEntry, str]]: | ||
| """Yield (entity, node_address) for each entity backed by a supported node.""" | ||
| prefix = f"{isy_data.uuid}_" | ||
| for entry in er.async_entries_for_device( | ||
| er.async_get(hass), device_id, include_disabled_entities=False | ||
| ): | ||
| if entry.platform != DOMAIN or not entry.unique_id.startswith(prefix): | ||
| continue | ||
| address = entry.unique_id[len(prefix) :] | ||
| node = isy_data.root.nodes.get_by_id(address) | ||
| if node is None: | ||
| continue | ||
| if getattr(node, "node_def_id", None) in SUPPORTED_NODE_DEF_IDS: | ||
| yield entry, address | ||
|
|
||
|
|
||
| async def async_get_triggers( | ||
| hass: HomeAssistant, device_id: str | ||
| ) -> list[dict[str, str]]: | ||
| """List device triggers for supported ISY Insteon load/button nodes.""" | ||
| isy_data = _resolve_isy_data(hass, device_id) | ||
| if isy_data is None: | ||
| return [] | ||
|
|
||
| triggers: list[dict[str, str]] = [] | ||
| for entry, address in _supported_button_entries(hass, device_id, isy_data): | ||
| triggers.extend( | ||
| { | ||
| CONF_PLATFORM: "device", | ||
| CONF_DOMAIN: DOMAIN, | ||
| CONF_DEVICE_ID: device_id, | ||
| CONF_ENTITY_ID: entry.entity_id, | ||
| CONF_TYPE: trigger_type, | ||
| CONF_SUBTYPE: address, | ||
| } | ||
| for trigger_type in TRIGGER_TYPES | ||
| ) | ||
| return triggers | ||
|
|
||
|
|
||
| async def async_get_trigger_capabilities( | ||
| hass: HomeAssistant, config: ConfigType | ||
| ) -> dict[str, vol.Schema]: | ||
| """No additional fields — type and subtype fully describe the trigger.""" | ||
| return {} | ||
|
|
||
|
|
||
| async def async_attach_trigger( | ||
| hass: HomeAssistant, | ||
| config: ConfigType, | ||
| action: TriggerActionType, | ||
| trigger_info: TriggerInfo, | ||
| ) -> CALLBACK_TYPE: | ||
| """Attach a trigger that filters isy994_control events.""" | ||
| device_id: str = config[CONF_DEVICE_ID] | ||
| isy_data = _resolve_isy_data(hass, device_id) | ||
| if isy_data is None: | ||
| raise InvalidDeviceAutomationConfig( | ||
| f"ISY device {device_id} not found or not loaded" | ||
| ) | ||
|
|
||
| address = config[CONF_SUBTYPE] | ||
| node = isy_data.root.nodes.get_by_id(address) | ||
| if node is None or getattr(node, "node_def_id", None) not in SUPPORTED_NODE_DEF_IDS: | ||
| raise InvalidDeviceAutomationConfig( | ||
| f"ISY node {address} is not a supported device-trigger source" | ||
| ) | ||
|
|
||
| target_unique_id = f"{isy_data.uuid}_{address}" | ||
| target_entity_id: str | None = None | ||
| for entry in er.async_entries_for_device(er.async_get(hass), device_id): | ||
| if entry.platform == DOMAIN and entry.unique_id == target_unique_id: | ||
| target_entity_id = entry.entity_id | ||
| break | ||
| if target_entity_id is None: | ||
| raise InvalidDeviceAutomationConfig( | ||
| f"No ISY entity found for device {device_id} subtype {address}" | ||
| ) | ||
|
|
||
| event_config = event_trigger.TRIGGER_SCHEMA( | ||
| { | ||
| event_trigger.CONF_PLATFORM: "event", | ||
| event_trigger.CONF_EVENT_TYPE: EVENT_ISY994_CONTROL, | ||
| event_trigger.CONF_EVENT_DATA: { | ||
| CONF_ENTITY_ID: target_entity_id, | ||
| "control": TRIGGER_TYPES[config[CONF_TYPE]], | ||
| }, | ||
| } | ||
| ) | ||
| return await event_trigger.async_attach_trigger( | ||
| hass, event_config, action, trigger_info, platform_type="device" | ||
| ) | ||
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-
_ADVdevices are older generation and I do not have any to test that they emit the same control events. This subset was chosen to represent the intitial "known working" set of device types within insteon and can be expanded later.