mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 18:27:51 +00:00
Merge pull request #844 from balloob/delay-group-switch-sensors
Fix calling turn_on for groups with mixed content
This commit is contained in:
commit
033bd30391
@ -87,13 +87,21 @@ def setup(hass, config):
|
|||||||
lambda item: util.split_entity_id(item)[0])
|
lambda item: util.split_entity_id(item)[0])
|
||||||
|
|
||||||
for domain, ent_ids in by_domain:
|
for domain, ent_ids in by_domain:
|
||||||
|
# We want to block for all calls and only return when all calls
|
||||||
|
# have been processed. If a service does not exist it causes a 10
|
||||||
|
# second delay while we're blocking waiting for a response.
|
||||||
|
# But services can be registered on other HA instances that are
|
||||||
|
# listening to the bus too. So as a in between solution, we'll
|
||||||
|
# block only if the service is defined in the current HA instance.
|
||||||
|
blocking = hass.services.has_service(domain, service.service)
|
||||||
|
|
||||||
# Create a new dict for this call
|
# Create a new dict for this call
|
||||||
data = dict(service.data)
|
data = dict(service.data)
|
||||||
|
|
||||||
# ent_ids is a generator, convert it to a list.
|
# ent_ids is a generator, convert it to a list.
|
||||||
data[ATTR_ENTITY_ID] = list(ent_ids)
|
data[ATTR_ENTITY_ID] = list(ent_ids)
|
||||||
|
|
||||||
hass.services.call(domain, service.service, data, True)
|
hass.services.call(domain, service.service, data, blocking)
|
||||||
|
|
||||||
hass.services.register(ha.DOMAIN, SERVICE_TURN_OFF, handle_turn_service)
|
hass.services.register(ha.DOMAIN, SERVICE_TURN_OFF, handle_turn_service)
|
||||||
hass.services.register(ha.DOMAIN, SERVICE_TURN_ON, handle_turn_service)
|
hass.services.register(ha.DOMAIN, SERVICE_TURN_ON, handle_turn_service)
|
||||||
|
@ -36,7 +36,7 @@ def extract_entity_ids(hass, service):
|
|||||||
service_ent_id = service.data[ATTR_ENTITY_ID]
|
service_ent_id = service.data[ATTR_ENTITY_ID]
|
||||||
|
|
||||||
if isinstance(service_ent_id, str):
|
if isinstance(service_ent_id, str):
|
||||||
return group.expand_entity_ids(hass, [service_ent_id.lower()])
|
return group.expand_entity_ids(hass, [service_ent_id])
|
||||||
|
|
||||||
return [ent_id for ent_id in group.expand_entity_ids(hass, service_ent_id)]
|
return [ent_id for ent_id in group.expand_entity_ids(hass, service_ent_id)]
|
||||||
|
|
||||||
|
@ -6,20 +6,22 @@ Tests core compoments.
|
|||||||
"""
|
"""
|
||||||
# pylint: disable=protected-access,too-many-public-methods
|
# pylint: disable=protected-access,too-many-public-methods
|
||||||
import unittest
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
import homeassistant.core as ha
|
import homeassistant.core as ha
|
||||||
import homeassistant.loader as loader
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
STATE_ON, STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF)
|
STATE_ON, STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF)
|
||||||
import homeassistant.components as comps
|
import homeassistant.components as comps
|
||||||
|
|
||||||
|
from tests.common import get_test_home_assistant
|
||||||
|
|
||||||
|
|
||||||
class TestComponentsCore(unittest.TestCase):
|
class TestComponentsCore(unittest.TestCase):
|
||||||
""" Tests homeassistant.components module. """
|
""" Tests homeassistant.components module. """
|
||||||
|
|
||||||
def setUp(self): # pylint: disable=invalid-name
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
""" Init needed objects. """
|
""" Init needed objects. """
|
||||||
self.hass = ha.HomeAssistant()
|
self.hass = get_test_home_assistant()
|
||||||
self.assertTrue(comps.setup(self.hass, {}))
|
self.assertTrue(comps.setup(self.hass, {}))
|
||||||
|
|
||||||
self.hass.states.set('light.Bowl', STATE_ON)
|
self.hass.states.set('light.Bowl', STATE_ON)
|
||||||
@ -58,3 +60,24 @@ class TestComponentsCore(unittest.TestCase):
|
|||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
self.assertEqual(1, len(runs))
|
self.assertEqual(1, len(runs))
|
||||||
|
|
||||||
|
@patch('homeassistant.core.ServiceRegistry.call')
|
||||||
|
def test_turn_on_to_not_block_for_domains_without_service(self, mock_call):
|
||||||
|
self.hass.services.register('light', SERVICE_TURN_ON, lambda x: x)
|
||||||
|
|
||||||
|
# We can't test if our service call results in services being called
|
||||||
|
# because by mocking out the call service method, we mock out all
|
||||||
|
# So we mimick how the service registry calls services
|
||||||
|
service_call = ha.ServiceCall('homeassistant', 'turn_on', {
|
||||||
|
'entity_id': ['light.test', 'sensor.bla', 'light.bla']
|
||||||
|
})
|
||||||
|
self.hass.services._services['homeassistant']['turn_on'](service_call)
|
||||||
|
|
||||||
|
self.assertEqual(2, mock_call.call_count)
|
||||||
|
self.assertEqual(
|
||||||
|
('light', 'turn_on', {'entity_id': ['light.bla', 'light.test']},
|
||||||
|
True),
|
||||||
|
mock_call.call_args_list[0][0])
|
||||||
|
self.assertEqual(
|
||||||
|
('sensor', 'turn_on', {'entity_id': ['sensor.bla']}, False),
|
||||||
|
mock_call.call_args_list[1][0])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user