Convert switch to AsnycIO (#4382)
* Convert switch to AsnycIO * Move update entity to service * use time better for faster handling * Change to suggestion from paulus * Use new shedule_update_ha_state * fix lint * minimize executor calls
This commit is contained in:
1 parent
5d8a465c18
commit
41aaeb715a
18 files changed
+80
-56
No files matched your search
@@ -4,6 +4,7 @@ Component to interface with various switches that can be controlled remotely.
|
||||
For more details about this component, please refer to the documentation
|
||||
at https://home-assistant.io/components/switch/
|
||||
"""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
import os
|
||||
@@ -69,38 +70,50 @@ def toggle(hass, entity_id=None):
|
||||
hass.services.call(DOMAIN, SERVICE_TOGGLE, data)
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
@asyncio.coroutine
|
||||
def async_setup(hass, config):
|
||||
"""Track states and offer events for switches."""
|
||||
component = EntityComponent(
|
||||
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_SWITCHES)
|
||||
component.setup(config)
|
||||
yield from component.async_setup(config)
|
||||
|
||||
def handle_switch_service(service):
|
||||
@asyncio.coroutine
|
||||
def async_handle_switch_service(service):
|
||||
"""Handle calls to the switch services."""
|
||||
target_switches = component.extract_from_service(service)
|
||||
target_switches = component.async_extract_from_service(service)
|
||||
|
||||
update_tasks = []
|
||||
for switch in target_switches:
|
||||
if service.service == SERVICE_TURN_ON:
|
||||
switch.turn_on()
|
||||
yield from switch.async_turn_on()
|
||||
elif service.service == SERVICE_TOGGLE:
|
||||
switch.toggle()
|
||||
yield from switch.async_toggle()
|
||||
else:
|
||||
switch.turn_off()
|
||||
yield from switch.async_turn_off()
|
||||
|
||||
if switch.should_poll:
|
||||
switch.update_ha_state(True)
|
||||
update_coro = switch.async_update_ha_state(True)
|
||||
if hasattr(switch, 'async_update'):
|
||||
update_tasks.append(hass.loop.create_task(update_coro))
|
||||
else:
|
||||
yield from update_coro
|
||||
|
||||
descriptions = load_yaml_config_file(
|
||||
os.path.join(os.path.dirname(__file__), 'services.yaml'))
|
||||
hass.services.register(DOMAIN, SERVICE_TURN_OFF, handle_switch_service,
|
||||
descriptions.get(SERVICE_TURN_OFF),
|
||||
schema=SWITCH_SERVICE_SCHEMA)
|
||||
hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_switch_service,
|
||||
descriptions.get(SERVICE_TURN_ON),
|
||||
schema=SWITCH_SERVICE_SCHEMA)
|
||||
hass.services.register(DOMAIN, SERVICE_TOGGLE, handle_switch_service,
|
||||
descriptions.get(SERVICE_TOGGLE),
|
||||
schema=SWITCH_SERVICE_SCHEMA)
|
||||
if update_tasks:
|
||||
yield from asyncio.wait(update_tasks, loop=hass.loop)
|
||||
|
||||
descriptions = yield from hass.loop.run_in_executor(
|
||||
None, load_yaml_config_file, os.path.join(
|
||||
os.path.dirname(__file__), 'services.yaml'))
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_TURN_OFF, async_handle_switch_service,
|
||||
descriptions.get(SERVICE_TURN_OFF), schema=SWITCH_SERVICE_SCHEMA)
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_TURN_ON, async_handle_switch_service,
|
||||
descriptions.get(SERVICE_TURN_ON), schema=SWITCH_SERVICE_SCHEMA)
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_TOGGLE, async_handle_switch_service,
|
||||
descriptions.get(SERVICE_TOGGLE), schema=SWITCH_SERVICE_SCHEMA)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
Reference in new issue
Block a user