Entity service (#15991)

* Add entity service helper

* Use entity service helper

* Context
This commit is contained in:
Paulus Schoutsen 2018-08-16 09:50:11 +02:00 committed by GitHub
parent e52ba87af1
commit b682e48e12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 276 additions and 387 deletions

View file

@ -4,7 +4,6 @@ Component to keep track of user controlled booleans for within automation.
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
@ -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)
component.async_register_entity_service(
SERVICE_TURN_ON, SERVICE_SCHEMA,
'async_turn_on'
)
if service.service == SERVICE_TURN_ON:
attr = 'async_turn_on'
elif service.service == SERVICE_TURN_OFF:
attr = 'async_turn_off'
else:
attr = 'async_toggle'
component.async_register_entity_service(
SERVICE_TURN_OFF, SERVICE_SCHEMA,
'async_turn_off'
)
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_TOGGLE, SERVICE_SCHEMA,
'async_toggle'
)
await component.async_add_entities(entities)
return True