-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
Add websock command to query device for triggers #24044
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e137572
Add websock command to query device for triggers
emontnemery c22ba80
Lint
emontnemery a445235
Refactor
emontnemery 60ffcfa
Add support for domain automations
emontnemery 3e076df
Make device automation an automation platform
emontnemery d948bc7
lint
emontnemery 6307d04
Support device_id in light trigger
emontnemery 0b5ad46
Review comments
emontnemery deb08a6
Add tests
emontnemery 0ac5eb2
Add tests
emontnemery e1a1d6d
lint
emontnemery 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
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.
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.
Include a
FROM
or else a color update will trigger it too.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.
fixed.