mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Remove service helper (6) (#16920)
* Update automation * Update group * Async_create_task
This commit is contained in:
parent
70b901017f
commit
f879ac0993
@ -115,21 +115,6 @@ def is_on(hass, entity_id):
|
|||||||
return hass.states.is_state(entity_id, STATE_ON)
|
return hass.states.is_state(entity_id, STATE_ON)
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
|
||||||
def reload(hass):
|
|
||||||
"""Reload the automation from config."""
|
|
||||||
hass.services.call(DOMAIN, SERVICE_RELOAD)
|
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
|
||||||
def async_reload(hass):
|
|
||||||
"""Reload the automation from config.
|
|
||||||
|
|
||||||
Returns a coroutine object.
|
|
||||||
"""
|
|
||||||
return hass.services.async_call(DOMAIN, SERVICE_RELOAD)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass, config):
|
async def async_setup(hass, config):
|
||||||
"""Set up the automation."""
|
"""Set up the automation."""
|
||||||
component = EntityComponent(_LOGGER, DOMAIN, hass,
|
component = EntityComponent(_LOGGER, DOMAIN, hass,
|
||||||
|
@ -3,10 +3,9 @@ import asyncio
|
|||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from homeassistant.const import CONF_ID
|
|
||||||
from homeassistant.components.config import EditIdBasedConfigView
|
from homeassistant.components.config import EditIdBasedConfigView
|
||||||
from homeassistant.components.automation import (
|
from homeassistant.const import CONF_ID, SERVICE_RELOAD
|
||||||
PLATFORM_SCHEMA, DOMAIN, async_reload)
|
from homeassistant.components.automation import DOMAIN, PLATFORM_SCHEMA
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
|
|
||||||
@ -16,9 +15,13 @@ CONFIG_PATH = 'automations.yaml'
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_setup(hass):
|
def async_setup(hass):
|
||||||
"""Set up the Automation config API."""
|
"""Set up the Automation config API."""
|
||||||
|
async def hook(hass):
|
||||||
|
"""post_write_hook for Config View that reloads automations."""
|
||||||
|
await hass.services.async_call(DOMAIN, SERVICE_RELOAD)
|
||||||
|
|
||||||
hass.http.register_view(EditAutomationConfigView(
|
hass.http.register_view(EditAutomationConfigView(
|
||||||
DOMAIN, 'config', CONFIG_PATH, cv.string,
|
DOMAIN, 'config', CONFIG_PATH, cv.string,
|
||||||
PLATFORM_SCHEMA, post_write_hook=async_reload
|
PLATFORM_SCHEMA, post_write_hook=hook
|
||||||
))
|
))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ from homeassistant.setup import async_prepare_setup_platform
|
|||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.loader import bind_hass
|
from homeassistant.loader import bind_hass
|
||||||
from homeassistant.components import group, zone
|
from homeassistant.components import group, zone
|
||||||
|
from homeassistant.components.group import DOMAIN as DOMAIN_GROUP, SERVICE_SET
|
||||||
from homeassistant.components.zone.zone import async_active_zone
|
from homeassistant.components.zone.zone import async_active_zone
|
||||||
from homeassistant.config import load_yaml_config_file, async_log_exception
|
from homeassistant.config import load_yaml_config_file, async_log_exception
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
@ -319,9 +320,13 @@ class DeviceTracker:
|
|||||||
|
|
||||||
# During init, we ignore the group
|
# During init, we ignore the group
|
||||||
if self.group and self.track_new:
|
if self.group and self.track_new:
|
||||||
self.group.async_set_group(
|
self.hass.async_create_task(
|
||||||
util.slugify(GROUP_NAME_ALL_DEVICES), visible=False,
|
self.hass.async_call(
|
||||||
name=GROUP_NAME_ALL_DEVICES, add=[device.entity_id])
|
DOMAIN_GROUP, SERVICE_SET, dict(
|
||||||
|
object_id=util.slugify(GROUP_NAME_ALL_DEVICES),
|
||||||
|
visible=False,
|
||||||
|
name=GROUP_NAME_ALL_DEVICES,
|
||||||
|
add=[device.entity_id])))
|
||||||
|
|
||||||
self.hass.bus.async_fire(EVENT_NEW_DEVICE, {
|
self.hass.bus.async_fire(EVENT_NEW_DEVICE, {
|
||||||
ATTR_ENTITY_ID: device.entity_id,
|
ATTR_ENTITY_ID: device.entity_id,
|
||||||
@ -354,10 +359,13 @@ class DeviceTracker:
|
|||||||
entity_ids = [dev.entity_id for dev in self.devices.values()
|
entity_ids = [dev.entity_id for dev in self.devices.values()
|
||||||
if dev.track]
|
if dev.track]
|
||||||
|
|
||||||
self.group = self.hass.components.group
|
self.hass.async_create_task(
|
||||||
self.group.async_set_group(
|
self.hass.services.async_call(
|
||||||
util.slugify(GROUP_NAME_ALL_DEVICES), visible=False,
|
DOMAIN_GROUP, SERVICE_SET, dict(
|
||||||
name=GROUP_NAME_ALL_DEVICES, entity_ids=entity_ids)
|
object_id=util.slugify(GROUP_NAME_ALL_DEVICES),
|
||||||
|
visible=False,
|
||||||
|
name=GROUP_NAME_ALL_DEVICES,
|
||||||
|
entities=entity_ids)))
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_update_stale(self, now: dt_util.dt.datetime):
|
def async_update_stale(self, now: dt_util.dt.datetime):
|
||||||
|
@ -120,63 +120,6 @@ def is_on(hass, entity_id):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@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))
|
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
|
||||||
def remove(hass, name):
|
|
||||||
"""Remove a user group."""
|
|
||||||
hass.add_job(async_remove, hass, name)
|
|
||||||
|
|
||||||
|
|
||||||
@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
|
@bind_hass
|
||||||
def expand_entity_ids(hass, entity_ids):
|
def expand_entity_ids(hass, entity_ids):
|
||||||
"""Return entity_ids with group entity ids replaced by their members.
|
"""Return entity_ids with group entity ids replaced by their members.
|
||||||
|
@ -190,10 +190,13 @@ class EntityComponent:
|
|||||||
sorted(self.entities,
|
sorted(self.entities,
|
||||||
key=lambda entity: entity.name or entity.entity_id)]
|
key=lambda entity: entity.name or entity.entity_id)]
|
||||||
|
|
||||||
self.hass.components.group.async_set_group(
|
self.hass.async_create_task(
|
||||||
slugify(self.group_name), name=self.group_name,
|
self.hass.services.async_call(
|
||||||
visible=False, entity_ids=ids
|
'group', 'set', dict(
|
||||||
)
|
object_id=slugify(self.group_name),
|
||||||
|
name=self.group_name,
|
||||||
|
visible=False,
|
||||||
|
entities=ids)))
|
||||||
|
|
||||||
async def _async_reset(self):
|
async def _async_reset(self):
|
||||||
"""Remove entities and reset the entity component to initial values.
|
"""Remove entities and reset the entity component to initial values.
|
||||||
@ -212,7 +215,9 @@ class EntityComponent:
|
|||||||
self.config = None
|
self.config = None
|
||||||
|
|
||||||
if self.group_name is not None:
|
if self.group_name is not None:
|
||||||
self.hass.components.group.async_remove(slugify(self.group_name))
|
await self.hass.services.async_call(
|
||||||
|
'group', 'remove', dict(
|
||||||
|
object_id=slugify(self.group_name)))
|
||||||
|
|
||||||
async def async_remove_entity(self, entity_id):
|
async def async_remove_entity(self, entity_id):
|
||||||
"""Remove an entity managed by one of the platforms."""
|
"""Remove an entity managed by one of the platforms."""
|
||||||
|
@ -3,12 +3,66 @@
|
|||||||
All containing methods are legacy helpers that should not be used by new
|
All containing methods are legacy helpers that should not be used by new
|
||||||
components. Instead call the service directly.
|
components. Instead call the service directly.
|
||||||
"""
|
"""
|
||||||
from homeassistant.components.group import ATTR_VISIBLE, DOMAIN, \
|
from homeassistant.components.group import (
|
||||||
SERVICE_SET_VISIBILITY
|
ATTR_ADD_ENTITIES, ATTR_CONTROL, ATTR_ENTITIES, ATTR_OBJECT_ID, ATTR_VIEW,
|
||||||
from homeassistant.const import ATTR_ENTITY_ID
|
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
|
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
|
@bind_hass
|
||||||
def set_visibility(hass, entity_id=None, visible=True):
|
def set_visibility(hass, entity_id=None, visible=True):
|
||||||
"""Hide or shows a group."""
|
"""Hide or shows a group."""
|
||||||
|
@ -368,7 +368,7 @@ class TestComponentsGroup(unittest.TestCase):
|
|||||||
}}}):
|
}}}):
|
||||||
with patch('homeassistant.config.find_config_file',
|
with patch('homeassistant.config.find_config_file',
|
||||||
return_value=''):
|
return_value=''):
|
||||||
group.reload(self.hass)
|
common.reload(self.hass)
|
||||||
self.hass.block_till_done()
|
self.hass.block_till_done()
|
||||||
|
|
||||||
assert sorted(self.hass.states.entity_ids()) == \
|
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
|
# The old way would create a new group modify_group1 because
|
||||||
# internally it didn't know anything about those created in the config
|
# 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()
|
self.hass.block_till_done()
|
||||||
|
|
||||||
group_state = self.hass.states.get(
|
group_state = self.hass.states.get(
|
||||||
@ -442,7 +442,7 @@ def test_service_group_set_group_remove_group(hass):
|
|||||||
'group': {}
|
'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()
|
yield from hass.async_block_till_done()
|
||||||
|
|
||||||
group_state = hass.states.get('group.user_test_group')
|
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[group.ATTR_AUTO]
|
||||||
assert group_state.attributes['friendly_name'] == "Test"
|
assert group_state.attributes['friendly_name'] == "Test"
|
||||||
|
|
||||||
group.async_set_group(
|
common.async_set_group(
|
||||||
hass, 'user_test_group', view=True, visible=False,
|
hass, 'user_test_group', view=True, visible=False,
|
||||||
entity_ids=['test.entity_bla1'])
|
entity_ids=['test.entity_bla1'])
|
||||||
yield from hass.async_block_till_done()
|
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 group_state.attributes['friendly_name'] == "Test"
|
||||||
assert list(group_state.attributes['entity_id']) == ['test.entity_bla1']
|
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",
|
hass, 'user_test_group', icon="mdi:camera", name="Test2",
|
||||||
control="hidden", add=['test.entity_id2'])
|
control="hidden", add=['test.entity_id2'])
|
||||||
yield from hass.async_block_till_done()
|
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([
|
assert sorted(list(group_state.attributes['entity_id'])) == sorted([
|
||||||
'test.entity_bla1', 'test.entity_id2'])
|
'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()
|
yield from hass.async_block_till_done()
|
||||||
|
|
||||||
group_state = hass.states.get('group.user_test_group')
|
group_state = hass.states.get('group.user_test_group')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user