mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Allow targeting areas in service calls (#21472)
* Allow targeting areas in service calls * Lint + Type * Address comments
This commit is contained in:
@@ -15,8 +15,11 @@ from homeassistant.helpers import service, template
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.auth.permissions import PolicyPermissions
|
||||
|
||||
from tests.common import get_test_home_assistant, mock_service, mock_coro
|
||||
from homeassistant.helpers import (
|
||||
device_registry as dev_reg, entity_registry as ent_reg)
|
||||
from tests.common import (
|
||||
get_test_home_assistant, mock_service, mock_coro, mock_registry,
|
||||
mock_device_registry)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -163,29 +166,83 @@ class TestServiceHelpers(unittest.TestCase):
|
||||
})
|
||||
assert 3 == mock_log.call_count
|
||||
|
||||
def test_extract_entity_ids(self):
|
||||
"""Test extract_entity_ids method."""
|
||||
self.hass.states.set('light.Bowl', STATE_ON)
|
||||
self.hass.states.set('light.Ceiling', STATE_OFF)
|
||||
self.hass.states.set('light.Kitchen', STATE_OFF)
|
||||
|
||||
loader.get_component(self.hass, 'group').Group.create_group(
|
||||
self.hass, 'test', ['light.Ceiling', 'light.Kitchen'])
|
||||
async def test_extract_entity_ids(hass):
|
||||
"""Test extract_entity_ids method."""
|
||||
hass.states.async_set('light.Bowl', STATE_ON)
|
||||
hass.states.async_set('light.Ceiling', STATE_OFF)
|
||||
hass.states.async_set('light.Kitchen', STATE_OFF)
|
||||
|
||||
call = ha.ServiceCall('light', 'turn_on',
|
||||
{ATTR_ENTITY_ID: 'light.Bowl'})
|
||||
await loader.get_component(hass, 'group').Group.async_create_group(
|
||||
hass, 'test', ['light.Ceiling', 'light.Kitchen'])
|
||||
|
||||
assert ['light.bowl'] == \
|
||||
service.extract_entity_ids(self.hass, call)
|
||||
call = ha.ServiceCall('light', 'turn_on',
|
||||
{ATTR_ENTITY_ID: 'light.Bowl'})
|
||||
|
||||
call = ha.ServiceCall('light', 'turn_on',
|
||||
{ATTR_ENTITY_ID: 'group.test'})
|
||||
assert {'light.bowl'} == \
|
||||
await service.async_extract_entity_ids(hass, call)
|
||||
|
||||
assert ['light.ceiling', 'light.kitchen'] == \
|
||||
service.extract_entity_ids(self.hass, call)
|
||||
call = ha.ServiceCall('light', 'turn_on',
|
||||
{ATTR_ENTITY_ID: 'group.test'})
|
||||
|
||||
assert ['group.test'] == service.extract_entity_ids(
|
||||
self.hass, call, expand_group=False)
|
||||
assert {'light.ceiling', 'light.kitchen'} == \
|
||||
await service.async_extract_entity_ids(hass, call)
|
||||
|
||||
assert {'group.test'} == await service.async_extract_entity_ids(
|
||||
hass, call, expand_group=False)
|
||||
|
||||
|
||||
async def test_extract_entity_ids_from_area(hass):
|
||||
"""Test extract_entity_ids method with areas."""
|
||||
hass.states.async_set('light.Bowl', STATE_ON)
|
||||
hass.states.async_set('light.Ceiling', STATE_OFF)
|
||||
hass.states.async_set('light.Kitchen', STATE_OFF)
|
||||
|
||||
device_in_area = dev_reg.DeviceEntry(area_id='test-area')
|
||||
device_no_area = dev_reg.DeviceEntry()
|
||||
device_diff_area = dev_reg.DeviceEntry(area_id='diff-area')
|
||||
|
||||
mock_device_registry(hass, {
|
||||
device_in_area.id: device_in_area,
|
||||
device_no_area.id: device_no_area,
|
||||
device_diff_area.id: device_diff_area,
|
||||
})
|
||||
|
||||
entity_in_area = ent_reg.RegistryEntry(
|
||||
entity_id='light.in_area',
|
||||
unique_id='in-area-id',
|
||||
platform='test',
|
||||
device_id=device_in_area.id,
|
||||
)
|
||||
entity_no_area = ent_reg.RegistryEntry(
|
||||
entity_id='light.no_area',
|
||||
unique_id='no-area-id',
|
||||
platform='test',
|
||||
device_id=device_no_area.id,
|
||||
)
|
||||
entity_diff_area = ent_reg.RegistryEntry(
|
||||
entity_id='light.diff_area',
|
||||
unique_id='diff-area-id',
|
||||
platform='test',
|
||||
device_id=device_diff_area.id,
|
||||
)
|
||||
mock_registry(hass, {
|
||||
entity_in_area.entity_id: entity_in_area,
|
||||
entity_no_area.entity_id: entity_no_area,
|
||||
entity_diff_area.entity_id: entity_diff_area,
|
||||
})
|
||||
|
||||
call = ha.ServiceCall('light', 'turn_on',
|
||||
{'area_id': 'test-area'})
|
||||
|
||||
assert {'light.in_area'} == \
|
||||
await service.async_extract_entity_ids(hass, call)
|
||||
|
||||
call = ha.ServiceCall('light', 'turn_on',
|
||||
{'area_id': ['test-area', 'diff-area']})
|
||||
|
||||
assert {'light.in_area', 'light.diff_area'} == \
|
||||
await service.async_extract_entity_ids(hass, call)
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
|
||||
Reference in New Issue
Block a user