Remove service helper (6) (#16920)

* Update automation

* Update group

* Async_create_task
This commit is contained in:
cdce8p authored and Paulus Schoutsen committed 2018-09-27 23:14:09 +02:00
1 parent 70b901017f
commit f879ac0993
7 files changed
+95 -97

No files matched your search

+57 -3
View File
@@ -3,12 +3,66 @@
All containing methods are legacy helpers that should not be used by new
components. Instead call the service directly.
"""
from homeassistant.components.group import ATTR_VISIBLE, DOMAIN, \
SERVICE_SET_VISIBILITY
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.components.group import (
ATTR_ADD_ENTITIES, ATTR_CONTROL, ATTR_ENTITIES, ATTR_OBJECT_ID, ATTR_VIEW,
ATTR_VISIBLE, DOMAIN, SERVICE_REMOVE, SERVICE_SET, SERVICE_SET_VISIBILITY)
from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ICON, ATTR_NAME, SERVICE_RELOAD)
from homeassistant.core import callback
from homeassistant.loader import bind_hass
@bind_hass
def reload(hass):
"""Reload the automation from config."""
hass.add_job(async_reload, hass)
@callback
@bind_hass
def async_reload(hass):
"""Reload the automation from config."""
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_RELOAD))
@bind_hass
def set_group(hass, object_id, name=None, entity_ids=None, visible=None,
icon=None, view=None, control=None, add=None):
"""Create/Update a group."""
hass.add_job(
async_set_group, hass, object_id, name, entity_ids, visible, icon,
view, control, add)
@callback
@bind_hass
def async_set_group(hass, object_id, name=None, entity_ids=None, visible=None,
icon=None, view=None, control=None, add=None):
"""Create/Update a group."""
data = {
key: value for key, value in [
(ATTR_OBJECT_ID, object_id),
(ATTR_NAME, name),
(ATTR_ENTITIES, entity_ids),
(ATTR_VISIBLE, visible),
(ATTR_ICON, icon),
(ATTR_VIEW, view),
(ATTR_CONTROL, control),
(ATTR_ADD_ENTITIES, add),
] if value is not None
}
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_SET, data))
@callback
@bind_hass
def async_remove(hass, object_id):
"""Remove a user group."""
data = {ATTR_OBJECT_ID: object_id}
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_REMOVE, data))
@bind_hass
def set_visibility(hass, entity_id=None, visible=True):
"""Hide or shows a group."""
+6 -6
View File
@@ -368,7 +368,7 @@ class TestComponentsGroup(unittest.TestCase):
}}}):
with patch('homeassistant.config.find_config_file',
return_value=''):
group.reload(self.hass)
common.reload(self.hass)
self.hass.block_till_done()
assert sorted(self.hass.states.entity_ids()) == \
@@ -409,7 +409,7 @@ class TestComponentsGroup(unittest.TestCase):
# The old way would create a new group modify_group1 because
# internally it didn't know anything about those created in the config
group.set_group(self.hass, 'modify_group', icon="mdi:play")
common.set_group(self.hass, 'modify_group', icon="mdi:play")
self.hass.block_till_done()
group_state = self.hass.states.get(
@@ -442,7 +442,7 @@ def test_service_group_set_group_remove_group(hass):
'group': {}
})
group.async_set_group(hass, 'user_test_group', name="Test")
common.async_set_group(hass, 'user_test_group', name="Test")
yield from hass.async_block_till_done()
group_state = hass.states.get('group.user_test_group')
@@ -450,7 +450,7 @@ def test_service_group_set_group_remove_group(hass):
assert group_state.attributes[group.ATTR_AUTO]
assert group_state.attributes['friendly_name'] == "Test"
group.async_set_group(
common.async_set_group(
hass, 'user_test_group', view=True, visible=False,
entity_ids=['test.entity_bla1'])
yield from hass.async_block_till_done()
@@ -463,7 +463,7 @@ def test_service_group_set_group_remove_group(hass):
assert group_state.attributes['friendly_name'] == "Test"
assert list(group_state.attributes['entity_id']) == ['test.entity_bla1']
group.async_set_group(
common.async_set_group(
hass, 'user_test_group', icon="mdi:camera", name="Test2",
control="hidden", add=['test.entity_id2'])
yield from hass.async_block_till_done()
@@ -479,7 +479,7 @@ def test_service_group_set_group_remove_group(hass):
assert sorted(list(group_state.attributes['entity_id'])) == sorted([
'test.entity_bla1', 'test.entity_id2'])
group.async_remove(hass, 'user_test_group')
common.async_remove(hass, 'user_test_group')
yield from hass.async_block_till_done()
group_state = hass.states.get('group.user_test_group')