Skip to content
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

Entity service #15991

Merged
merged 3 commits into from Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 24 additions & 38 deletions homeassistant/components/alarm_control_panel/__init__.py
Expand Up @@ -26,20 +26,6 @@

ENTITY_ID_FORMAT = DOMAIN + '.{}'

SERVICE_TO_METHOD = {
SERVICE_ALARM_DISARM: 'alarm_disarm',
SERVICE_ALARM_ARM_HOME: 'alarm_arm_home',
SERVICE_ALARM_ARM_AWAY: 'alarm_arm_away',
SERVICE_ALARM_ARM_NIGHT: 'alarm_arm_night',
SERVICE_ALARM_ARM_CUSTOM_BYPASS: 'alarm_arm_custom_bypass',
SERVICE_ALARM_TRIGGER: 'alarm_trigger'
}

ATTR_TO_PROPERTY = [
ATTR_CODE,
ATTR_CODE_FORMAT
]

ALARM_SERVICE_SCHEMA = vol.Schema({
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
vol.Optional(ATTR_CODE): cv.string,
Expand Down Expand Up @@ -126,30 +112,30 @@ def async_setup(hass, config):

yield from component.async_setup(config)

@asyncio.coroutine
def async_alarm_service_handler(service):
"""Map services to methods on Alarm."""
target_alarms = component.async_extract_from_service(service)

code = service.data.get(ATTR_CODE)

method = "async_{}".format(SERVICE_TO_METHOD[service.service])

update_tasks = []
for alarm in target_alarms:
yield from getattr(alarm, method)(code)

if not alarm.should_poll:
continue
update_tasks.append(alarm.async_update_ha_state(True))

if update_tasks:
yield from asyncio.wait(update_tasks, loop=hass.loop)

for service in SERVICE_TO_METHOD:
hass.services.async_register(
DOMAIN, service, async_alarm_service_handler,
schema=ALARM_SERVICE_SCHEMA)
component.async_register_entity_service(
SERVICE_ALARM_DISARM, ALARM_SERVICE_SCHEMA,
'async_alarm_disarm'
)
component.async_register_entity_service(
SERVICE_ALARM_ARM_HOME, ALARM_SERVICE_SCHEMA,
'async_alarm_arm_home'
)
component.async_register_entity_service(
SERVICE_ALARM_ARM_AWAY, ALARM_SERVICE_SCHEMA,
'async_alarm_arm_away'
)
component.async_register_entity_service(
SERVICE_ALARM_ARM_NIGHT, ALARM_SERVICE_SCHEMA,
'async_alarm_arm_night'
)
component.async_register_entity_service(
SERVICE_ALARM_ARM_CUSTOM_BYPASS, ALARM_SERVICE_SCHEMA,
'async_alarm_arm_custom_bypass'
)
component.async_register_entity_service(
SERVICE_ALARM_TRIGGER, ALARM_SERVICE_SCHEMA,
'async_alarm_trigger'
)

return True

Expand Down
31 changes: 9 additions & 22 deletions homeassistant/components/counter/__init__.py
Expand Up @@ -4,7 +4,6 @@
For more details about this component, please refer to the documentation
at https://home-assistant.io/components/counter/
"""
import asyncio
import logging

import voluptuous as vol
Expand Down Expand Up @@ -114,27 +113,15 @@ async def async_setup(hass, config):
if not entities:
return False

async def async_handler_service(service):
"""Handle a call to the counter services."""
target_counters = component.async_extract_from_service(service)

if service.service == SERVICE_INCREMENT:
attr = 'async_increment'
elif service.service == SERVICE_DECREMENT:
attr = 'async_decrement'
elif service.service == SERVICE_RESET:
attr = 'async_reset'

tasks = [getattr(counter, attr)() for counter in target_counters]
if tasks:
await asyncio.wait(tasks, loop=hass.loop)

hass.services.async_register(
DOMAIN, SERVICE_INCREMENT, async_handler_service)
hass.services.async_register(
DOMAIN, SERVICE_DECREMENT, async_handler_service)
hass.services.async_register(
DOMAIN, SERVICE_RESET, async_handler_service)
component.async_register_entity_service(
SERVICE_INCREMENT, SERVICE_SCHEMA,
'async_increment')
component.async_register_entity_service(
SERVICE_DECREMENT, SERVICE_SCHEMA,
'async_decrement')
component.async_register_entity_service(
SERVICE_RESET, SERVICE_SCHEMA,
'async_reset')

await component.async_add_entities(entities)
return True
Expand Down
80 changes: 40 additions & 40 deletions homeassistant/components/cover/__init__.py
Expand Up @@ -4,7 +4,6 @@
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/cover/
"""
import asyncio
from datetime import timedelta
import functools as ft
import logging
Expand Down Expand Up @@ -73,21 +72,6 @@
vol.All(vol.Coerce(int), vol.Range(min=0, max=100)),
})

SERVICE_TO_METHOD = {
SERVICE_OPEN_COVER: {'method': 'async_open_cover'},
SERVICE_CLOSE_COVER: {'method': 'async_close_cover'},
SERVICE_SET_COVER_POSITION: {
'method': 'async_set_cover_position',
'schema': COVER_SET_COVER_POSITION_SCHEMA},
SERVICE_STOP_COVER: {'method': 'async_stop_cover'},
SERVICE_OPEN_COVER_TILT: {'method': 'async_open_cover_tilt'},
SERVICE_CLOSE_COVER_TILT: {'method': 'async_close_cover_tilt'},
SERVICE_STOP_COVER_TILT: {'method': 'async_stop_cover_tilt'},
SERVICE_SET_COVER_TILT_POSITION: {
'method': 'async_set_cover_tilt_position',
'schema': COVER_SET_COVER_TILT_POSITION_SCHEMA},
}


@bind_hass
def is_closed(hass, entity_id=None):
Expand Down Expand Up @@ -161,30 +145,46 @@ async def async_setup(hass, config):

await component.async_setup(config)

async def async_handle_cover_service(service):
"""Handle calls to the cover services."""
covers = component.async_extract_from_service(service)
method = SERVICE_TO_METHOD.get(service.service)
params = service.data.copy()
params.pop(ATTR_ENTITY_ID, None)

# call method
update_tasks = []
for cover in covers:
await getattr(cover, method['method'])(**params)
if not cover.should_poll:
continue
update_tasks.append(cover.async_update_ha_state(True))

if update_tasks:
await asyncio.wait(update_tasks, loop=hass.loop)

for service_name in SERVICE_TO_METHOD:
schema = SERVICE_TO_METHOD[service_name].get(
'schema', COVER_SERVICE_SCHEMA)
hass.services.async_register(
DOMAIN, service_name, async_handle_cover_service,
schema=schema)
component.async_register_entity_service(
SERVICE_OPEN_COVER, COVER_SERVICE_SCHEMA,
'async_open_cover'
)

component.async_register_entity_service(
SERVICE_CLOSE_COVER, COVER_SERVICE_SCHEMA,
'async_close_cover'
)

component.async_register_entity_service(
SERVICE_SET_COVER_POSITION, COVER_SET_COVER_POSITION_SCHEMA,
'async_set_cover_position'
)

component.async_register_entity_service(
SERVICE_STOP_COVER, COVER_SERVICE_SCHEMA,
'async_stop_cover'
)

component.async_register_entity_service(
SERVICE_OPEN_COVER_TILT, COVER_SERVICE_SCHEMA,
'async_open_cover_tilt'
)

component.async_register_entity_service(
SERVICE_CLOSE_COVER_TILT, COVER_SERVICE_SCHEMA,
'async_close_cover_tilt'
)

component.async_register_entity_service(
SERVICE_STOP_COVER_TILT, COVER_SERVICE_SCHEMA,
'async_stop_cover_tilt'
)

component.async_register_entity_service(
SERVICE_SET_COVER_TILT_POSITION, COVER_SET_COVER_TILT_POSITION_SCHEMA,
'async_set_cover_tilt_position'
)

hass.helpers.intent.async_register(intent.ServiceIntentHandler(
INTENT_OPEN_COVER, DOMAIN, SERVICE_OPEN_COVER,
"Opened {}"))
Expand Down
39 changes: 14 additions & 25 deletions homeassistant/components/input_boolean.py
Expand Up @@ -4,7 +4,6 @@
For more details about this component, please refer to the documentation
at https://home-assistant.io/components/input_boolean/
"""
import asyncio
import logging

import voluptuous as vol
Expand Down Expand Up @@ -84,30 +83,20 @@ async def async_setup(hass, config):
if not entities:
return False

async def async_handler_service(service):
"""Handle a calls to the input boolean services."""
target_inputs = component.async_extract_from_service(service)

if service.service == SERVICE_TURN_ON:
attr = 'async_turn_on'
elif service.service == SERVICE_TURN_OFF:
attr = 'async_turn_off'
else:
attr = 'async_toggle'

tasks = [getattr(input_b, attr)() for input_b in target_inputs]
if tasks:
await asyncio.wait(tasks, loop=hass.loop)

hass.services.async_register(
DOMAIN, SERVICE_TURN_OFF, async_handler_service,
schema=SERVICE_SCHEMA)
hass.services.async_register(
DOMAIN, SERVICE_TURN_ON, async_handler_service,
schema=SERVICE_SCHEMA)
hass.services.async_register(
DOMAIN, SERVICE_TOGGLE, async_handler_service,
schema=SERVICE_SCHEMA)
component.async_register_entity_service(
SERVICE_TURN_ON, SERVICE_SCHEMA,
'async_turn_on'
)

component.async_register_entity_service(
SERVICE_TURN_OFF, SERVICE_SCHEMA,
'async_turn_off'
)

component.async_register_entity_service(
SERVICE_TOGGLE, SERVICE_SCHEMA,
'async_toggle'
)

await component.async_add_entities(entities)
return True
Expand Down
49 changes: 14 additions & 35 deletions homeassistant/components/input_number.py
Expand Up @@ -82,19 +82,6 @@ def _cv_input_number(cfg):
}, required=True, extra=vol.ALLOW_EXTRA)


SERVICE_TO_METHOD = {
SERVICE_SET_VALUE: {
'method': 'async_set_value',
'schema': SERVICE_SET_VALUE_SCHEMA},
SERVICE_INCREMENT: {
'method': 'async_increment',
'schema': SERVICE_DEFAULT_SCHEMA},
SERVICE_DECREMENT: {
'method': 'async_decrement',
'schema': SERVICE_DEFAULT_SCHEMA},
}


@bind_hass
def set_value(hass, entity_id, value):
"""Set input_number to value."""
Expand Down Expand Up @@ -144,28 +131,20 @@ def async_setup(hass, config):
if not entities:
return False

@asyncio.coroutine
def async_handle_service(service):
"""Handle calls to input_number services."""
target_inputs = component.async_extract_from_service(service)
method = SERVICE_TO_METHOD.get(service.service)
params = service.data.copy()
params.pop(ATTR_ENTITY_ID, None)

# call method
update_tasks = []
for target_input in target_inputs:
yield from getattr(target_input, method['method'])(**params)
if not target_input.should_poll:
continue
update_tasks.append(target_input.async_update_ha_state(True))

if update_tasks:
yield from asyncio.wait(update_tasks, loop=hass.loop)

for service, data in SERVICE_TO_METHOD.items():
hass.services.async_register(
DOMAIN, service, async_handle_service, schema=data['schema'])
component.async_register_entity_service(
SERVICE_SET_VALUE, SERVICE_SET_VALUE_SCHEMA,
'async_set_value'
)

component.async_register_entity_service(
SERVICE_INCREMENT, SERVICE_DEFAULT_SCHEMA,
'async_increment'
)

component.async_register_entity_service(
SERVICE_DECREMENT, SERVICE_DEFAULT_SCHEMA,
'async_decrement'
)

yield from component.async_add_entities(entities)
return True
Expand Down