-
-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add websock command to query device for triggers (#24044)
* Add websock command to query device for triggers * Lint * Refactor * Add support for domain automations * Make device automation an automation platform * lint * Support device_id in light trigger * Review comments * Add tests * Add tests * lint
- Loading branch information
1 parent
168f20b
commit 935240f
Showing
9 changed files
with
388 additions
and
1 deletion.
There are no files selected for viewing
Validating CODEOWNERS rules …
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
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
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,18 @@ | ||
"""Offer device oriented automation.""" | ||
import voluptuous as vol | ||
|
||
from homeassistant.const import CONF_DOMAIN, CONF_PLATFORM | ||
from homeassistant.loader import async_get_integration | ||
|
||
|
||
TRIGGER_SCHEMA = vol.Schema({ | ||
vol.Required(CONF_PLATFORM): 'device', | ||
vol.Required(CONF_DOMAIN): str, | ||
}, extra=vol.ALLOW_EXTRA) | ||
|
||
|
||
async def async_trigger(hass, config, action, automation_info): | ||
"""Listen for trigger.""" | ||
integration = await async_get_integration(hass, config[CONF_DOMAIN]) | ||
platform = integration.get_platform('device_automation') | ||
return await platform.async_trigger(hass, config, action, automation_info) |
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,80 @@ | ||
"""Helpers for device automations.""" | ||
import asyncio | ||
import logging | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.components import websocket_api | ||
from homeassistant.core import split_entity_id | ||
from homeassistant.helpers.entity_registry import async_entries_for_device | ||
from homeassistant.loader import async_get_integration, IntegrationNotFound | ||
|
||
DOMAIN = 'device_automation' | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup(hass, config): | ||
"""Set up device automation.""" | ||
hass.components.websocket_api.async_register_command( | ||
websocket_device_automation_list_triggers) | ||
return True | ||
|
||
|
||
async def _async_get_device_automation_triggers(hass, domain, device_id): | ||
"""List device triggers.""" | ||
integration = None | ||
try: | ||
integration = await async_get_integration(hass, domain) | ||
except IntegrationNotFound: | ||
_LOGGER.warning('Integration %s not found', domain) | ||
return None | ||
|
||
try: | ||
platform = integration.get_platform('device_automation') | ||
except ImportError: | ||
# The domain does not have device automations | ||
return None | ||
|
||
if hasattr(platform, 'async_get_triggers'): | ||
return await platform.async_get_triggers(hass, device_id) | ||
|
||
|
||
async def async_get_device_automation_triggers(hass, device_id): | ||
"""List device triggers.""" | ||
device_registry, entity_registry = await asyncio.gather( | ||
hass.helpers.device_registry.async_get_registry(), | ||
hass.helpers.entity_registry.async_get_registry()) | ||
|
||
domains = set() | ||
triggers = [] | ||
device = device_registry.async_get(device_id) | ||
for entry_id in device.config_entries: | ||
config_entry = hass.config_entries.async_get_entry(entry_id) | ||
domains.add(config_entry.domain) | ||
|
||
entities = async_entries_for_device(entity_registry, device_id) | ||
for entity in entities: | ||
domains.add(split_entity_id(entity.entity_id)[0]) | ||
|
||
device_triggers = await asyncio.gather(*[ | ||
_async_get_device_automation_triggers(hass, domain, device_id) | ||
for domain in domains | ||
]) | ||
for device_trigger in device_triggers: | ||
if device_trigger is not None: | ||
triggers.extend(device_trigger) | ||
|
||
return triggers | ||
|
||
|
||
@websocket_api.async_response | ||
@websocket_api.websocket_command({ | ||
vol.Required('type'): 'device_automation/list_triggers', | ||
vol.Required('device_id'): str, | ||
}) | ||
async def websocket_device_automation_list_triggers(hass, connection, msg): | ||
"""Handle request for device triggers.""" | ||
device_id = msg['device_id'] | ||
triggers = await async_get_device_automation_triggers(hass, device_id) | ||
connection.send_result(msg['id'], {'triggers': triggers}) |
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,12 @@ | ||
{ | ||
"domain": "device_automation", | ||
"name": "Device automation", | ||
"documentation": "https://www.home-assistant.io/components/device_automation", | ||
"requirements": [], | ||
"dependencies": [ | ||
"webhook" | ||
], | ||
"codeowners": [ | ||
"@home-assistant/core" | ||
] | ||
} |
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,80 @@ | ||
"""Provides device automations for lights.""" | ||
import voluptuous as vol | ||
|
||
import homeassistant.components.automation.state as state | ||
from homeassistant.core import split_entity_id | ||
from homeassistant.const import ( | ||
CONF_DEVICE_ID, CONF_DOMAIN, CONF_ENTITY_ID, CONF_PLATFORM, CONF_TYPE) | ||
import homeassistant.helpers.config_validation as cv | ||
from homeassistant.helpers.entity_registry import async_entries_for_device | ||
from . import DOMAIN | ||
|
||
CONF_TURN_OFF = 'turn_off' | ||
CONF_TURN_ON = 'turn_on' | ||
|
||
ENTITY_TRIGGERS = [ | ||
{ | ||
# Trigger when light is turned on | ||
CONF_PLATFORM: 'device', | ||
CONF_DOMAIN: DOMAIN, | ||
CONF_TYPE: CONF_TURN_OFF, | ||
}, | ||
{ | ||
# Trigger when light is turned off | ||
CONF_PLATFORM: 'device', | ||
CONF_DOMAIN: DOMAIN, | ||
CONF_TYPE: CONF_TURN_ON, | ||
}, | ||
] | ||
|
||
TRIGGER_SCHEMA = vol.All(vol.Schema({ | ||
vol.Required(CONF_PLATFORM): 'device', | ||
vol.Optional(CONF_DEVICE_ID): str, | ||
vol.Required(CONF_DOMAIN): DOMAIN, | ||
vol.Required(CONF_ENTITY_ID): cv.entity_id, | ||
vol.Required(CONF_TYPE): str, | ||
})) | ||
|
||
|
||
def _is_domain(entity, domain): | ||
return split_entity_id(entity.entity_id)[0] == domain | ||
|
||
|
||
async def async_attach_trigger(hass, config, action, automation_info): | ||
"""Listen for state changes based on configuration.""" | ||
trigger_type = config.get(CONF_TYPE) | ||
if trigger_type == CONF_TURN_ON: | ||
from_state = 'off' | ||
to_state = 'on' | ||
else: | ||
from_state = 'on' | ||
to_state = 'off' | ||
state_config = { | ||
state.CONF_ENTITY_ID: config[CONF_ENTITY_ID], | ||
state.CONF_FROM: from_state, | ||
state.CONF_TO: to_state | ||
} | ||
|
||
return await state.async_trigger(hass, state_config, action, | ||
automation_info) | ||
|
||
|
||
async def async_trigger(hass, config, action, automation_info): | ||
"""Temporary so existing automation framework can be used for testing.""" | ||
return await async_attach_trigger(hass, config, action, automation_info) | ||
|
||
|
||
async def async_get_triggers(hass, device_id): | ||
"""List device triggers.""" | ||
triggers = [] | ||
entity_registry = await hass.helpers.entity_registry.async_get_registry() | ||
|
||
entities = async_entries_for_device(entity_registry, device_id) | ||
domain_entities = [x for x in entities if _is_domain(x, DOMAIN)] | ||
for entity in domain_entities: | ||
for trigger in ENTITY_TRIGGERS: | ||
trigger = dict(trigger) | ||
trigger.update(device_id=device_id, entity_id=entity.entity_id) | ||
triggers.append(trigger) | ||
|
||
return triggers |
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
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,67 @@ | ||
"""The test for light device automation.""" | ||
import pytest | ||
|
||
from homeassistant.setup import async_setup_component | ||
from homeassistant.components.websocket_api.const import TYPE_RESULT | ||
from homeassistant.helpers import device_registry | ||
|
||
|
||
from tests.common import ( | ||
MockConfigEntry, mock_device_registry, mock_registry) | ||
|
||
|
||
@pytest.fixture | ||
def device_reg(hass): | ||
"""Return an empty, loaded, registry.""" | ||
return mock_device_registry(hass) | ||
|
||
|
||
@pytest.fixture | ||
def entity_reg(hass): | ||
"""Return an empty, loaded, registry.""" | ||
return mock_registry(hass) | ||
|
||
|
||
def _same_triggers(a, b): | ||
if len(a) != len(b): | ||
return False | ||
|
||
for d in a: | ||
if d not in b: | ||
return False | ||
return True | ||
|
||
|
||
async def test_websocket_get_triggers( | ||
hass, hass_ws_client, device_reg, entity_reg): | ||
"""Test we get the expected triggers from a light through websocket.""" | ||
await async_setup_component(hass, 'device_automation', {}) | ||
config_entry = MockConfigEntry(domain='test', data={}) | ||
config_entry.add_to_hass(hass) | ||
device_entry = device_reg.async_get_or_create( | ||
config_entry_id=config_entry.entry_id, | ||
connections={ | ||
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF') | ||
}) | ||
entity_reg.async_get_or_create( | ||
'light', 'test', '5678', device_id=device_entry.id) | ||
expected_triggers = [ | ||
{'platform': 'device', 'domain': 'light', 'type': 'turn_off', | ||
'device_id': device_entry.id, 'entity_id': 'light.test_5678'}, | ||
{'platform': 'device', 'domain': 'light', 'type': 'turn_on', | ||
'device_id': device_entry.id, 'entity_id': 'light.test_5678'}, | ||
] | ||
|
||
client = await hass_ws_client(hass) | ||
await client.send_json({ | ||
'id': 1, | ||
'type': 'device_automation/list_triggers', | ||
'device_id': device_entry.id | ||
}) | ||
msg = await client.receive_json() | ||
|
||
assert msg['id'] == 1 | ||
assert msg['type'] == TYPE_RESULT | ||
assert msg['success'] | ||
triggers = msg['result']['triggers'] | ||
assert _same_triggers(triggers, expected_triggers) |
Oops, something went wrong.