deConz rewrite to use async await syntax (#13151)

* Rewrite to use async await syntax

* Fix hound comment
pull/13132/head^2
Kane610 2018-03-13 08:47:45 +01:00 committed by Martin Hjelmare
parent 53351423dd
commit 8a1687accb
6 changed files with 37 additions and 61 deletions

View File

@ -4,8 +4,6 @@ Support for deCONZ binary sensor.
For more details about this component, please refer to the documentation at For more details about this component, please refer to the documentation at
https://home-assistant.io/components/binary_sensor.deconz/ https://home-assistant.io/components/binary_sensor.deconz/
""" """
import asyncio
from homeassistant.components.binary_sensor import BinarySensorDevice from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.components.deconz import ( from homeassistant.components.deconz import (
DOMAIN as DATA_DECONZ, DATA_DECONZ_ID) DOMAIN as DATA_DECONZ, DATA_DECONZ_ID)
@ -15,8 +13,8 @@ from homeassistant.core import callback
DEPENDENCIES = ['deconz'] DEPENDENCIES = ['deconz']
@asyncio.coroutine async def async_setup_platform(hass, config, async_add_devices,
def async_setup_platform(hass, config, async_add_devices, discovery_info=None): discovery_info=None):
"""Set up the deCONZ binary sensor.""" """Set up the deCONZ binary sensor."""
if discovery_info is None: if discovery_info is None:
return return
@ -39,8 +37,7 @@ class DeconzBinarySensor(BinarySensorDevice):
"""Set up sensor and add update callback to get data from websocket.""" """Set up sensor and add update callback to get data from websocket."""
self._sensor = sensor self._sensor = sensor
@asyncio.coroutine async def async_added_to_hass(self):
def async_added_to_hass(self):
"""Subscribe sensors events.""" """Subscribe sensors events."""
self._sensor.register_async_callback(self.async_update_callback) self._sensor.register_async_callback(self.async_update_callback)
self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._sensor.deconz_id self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._sensor.deconz_id

View File

@ -4,8 +4,6 @@ Support for deCONZ devices.
For more details about this component, please refer to the documentation at For more details about this component, please refer to the documentation at
https://home-assistant.io/components/deconz/ https://home-assistant.io/components/deconz/
""" """
import asyncio
import logging import logging
import voluptuous as vol import voluptuous as vol
@ -19,7 +17,7 @@ from homeassistant.helpers import discovery
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.util.json import load_json, save_json from homeassistant.util.json import load_json, save_json
REQUIREMENTS = ['pydeconz==30'] REQUIREMENTS = ['pydeconz==31']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -57,30 +55,28 @@ Unlock your deCONZ gateway to register with Home Assistant.
""" """
@asyncio.coroutine async def async_setup(hass, config):
def async_setup(hass, config):
"""Set up services and configuration for deCONZ component.""" """Set up services and configuration for deCONZ component."""
result = False result = False
config_file = yield from hass.async_add_job( config_file = await hass.async_add_job(
load_json, hass.config.path(CONFIG_FILE)) load_json, hass.config.path(CONFIG_FILE))
@asyncio.coroutine async def async_deconz_discovered(service, discovery_info):
def async_deconz_discovered(service, discovery_info):
"""Call when deCONZ gateway has been found.""" """Call when deCONZ gateway has been found."""
deconz_config = {} deconz_config = {}
deconz_config[CONF_HOST] = discovery_info.get(CONF_HOST) deconz_config[CONF_HOST] = discovery_info.get(CONF_HOST)
deconz_config[CONF_PORT] = discovery_info.get(CONF_PORT) deconz_config[CONF_PORT] = discovery_info.get(CONF_PORT)
yield from async_request_configuration(hass, config, deconz_config) await async_request_configuration(hass, config, deconz_config)
if config_file: if config_file:
result = yield from async_setup_deconz(hass, config, config_file) result = await async_setup_deconz(hass, config, config_file)
if not result and DOMAIN in config and CONF_HOST in config[DOMAIN]: if not result and DOMAIN in config and CONF_HOST in config[DOMAIN]:
deconz_config = config[DOMAIN] deconz_config = config[DOMAIN]
if CONF_API_KEY in deconz_config: if CONF_API_KEY in deconz_config:
result = yield from async_setup_deconz(hass, config, deconz_config) result = await async_setup_deconz(hass, config, deconz_config)
else: else:
yield from async_request_configuration(hass, config, deconz_config) await async_request_configuration(hass, config, deconz_config)
return True return True
if not result: if not result:
@ -89,8 +85,7 @@ def async_setup(hass, config):
return True return True
@asyncio.coroutine async def async_setup_deconz(hass, config, deconz_config):
def async_setup_deconz(hass, config, deconz_config):
"""Set up a deCONZ session. """Set up a deCONZ session.
Load config, group, light and sensor data for server information. Load config, group, light and sensor data for server information.
@ -100,7 +95,7 @@ def async_setup_deconz(hass, config, deconz_config):
from pydeconz import DeconzSession from pydeconz import DeconzSession
websession = async_get_clientsession(hass) websession = async_get_clientsession(hass)
deconz = DeconzSession(hass.loop, websession, **deconz_config) deconz = DeconzSession(hass.loop, websession, **deconz_config)
result = yield from deconz.async_load_parameters() result = await deconz.async_load_parameters()
if result is False: if result is False:
_LOGGER.error("Failed to communicate with deCONZ") _LOGGER.error("Failed to communicate with deCONZ")
return False return False
@ -113,8 +108,7 @@ def async_setup_deconz(hass, config, deconz_config):
hass, component, DOMAIN, {}, config)) hass, component, DOMAIN, {}, config))
deconz.start() deconz.start()
@asyncio.coroutine async def async_configure(call):
def async_configure(call):
"""Set attribute of device in deCONZ. """Set attribute of device in deCONZ.
Field is a string representing a specific device in deCONZ Field is a string representing a specific device in deCONZ
@ -140,7 +134,7 @@ def async_setup_deconz(hass, config, deconz_config):
if field is None: if field is None:
_LOGGER.error('Could not find the entity %s', entity_id) _LOGGER.error('Could not find the entity %s', entity_id)
return return
yield from deconz.async_put_state(field, data) await deconz.async_put_state(field, data)
hass.services.async_register( hass.services.async_register(
DOMAIN, 'configure', async_configure, schema=SERVICE_SCHEMA) DOMAIN, 'configure', async_configure, schema=SERVICE_SCHEMA)
@ -159,21 +153,19 @@ def async_setup_deconz(hass, config, deconz_config):
return True return True
@asyncio.coroutine async def async_request_configuration(hass, config, deconz_config):
def async_request_configuration(hass, config, deconz_config):
"""Request configuration steps from the user.""" """Request configuration steps from the user."""
configurator = hass.components.configurator configurator = hass.components.configurator
@asyncio.coroutine async def async_configuration_callback(data):
def async_configuration_callback(data):
"""Set up actions to do when our configuration callback is called.""" """Set up actions to do when our configuration callback is called."""
from pydeconz.utils import async_get_api_key from pydeconz.utils import async_get_api_key
api_key = yield from async_get_api_key(hass.loop, **deconz_config) api_key = await async_get_api_key(hass.loop, **deconz_config)
if api_key: if api_key:
deconz_config[CONF_API_KEY] = api_key deconz_config[CONF_API_KEY] = api_key
result = yield from async_setup_deconz(hass, config, deconz_config) result = await async_setup_deconz(hass, config, deconz_config)
if result: if result:
yield from hass.async_add_job( await hass.async_add_job(
save_json, hass.config.path(CONFIG_FILE), deconz_config) save_json, hass.config.path(CONFIG_FILE), deconz_config)
configurator.async_request_done(request_id) configurator.async_request_done(request_id)
return return

View File

@ -4,8 +4,6 @@ Support for deCONZ light.
For more details about this component, please refer to the documentation at For more details about this component, please refer to the documentation at
https://home-assistant.io/components/light.deconz/ https://home-assistant.io/components/light.deconz/
""" """
import asyncio
from homeassistant.components.deconz import ( from homeassistant.components.deconz import (
DOMAIN as DATA_DECONZ, DATA_DECONZ_ID) DOMAIN as DATA_DECONZ, DATA_DECONZ_ID)
from homeassistant.components.light import ( from homeassistant.components.light import (
@ -19,8 +17,8 @@ from homeassistant.util.color import color_RGB_to_xy
DEPENDENCIES = ['deconz'] DEPENDENCIES = ['deconz']
@asyncio.coroutine async def async_setup_platform(hass, config, async_add_devices,
def async_setup_platform(hass, config, async_add_devices, discovery_info=None): discovery_info=None):
"""Set up the deCONZ light.""" """Set up the deCONZ light."""
if discovery_info is None: if discovery_info is None:
return return
@ -59,8 +57,7 @@ class DeconzLight(Light):
if self._light.effect is not None: if self._light.effect is not None:
self._features |= SUPPORT_EFFECT self._features |= SUPPORT_EFFECT
@asyncio.coroutine async def async_added_to_hass(self):
def async_added_to_hass(self):
"""Subscribe to lights events.""" """Subscribe to lights events."""
self._light.register_async_callback(self.async_update_callback) self._light.register_async_callback(self.async_update_callback)
self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._light.deconz_id self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._light.deconz_id
@ -120,8 +117,7 @@ class DeconzLight(Light):
"""No polling needed.""" """No polling needed."""
return False return False
@asyncio.coroutine async def async_turn_on(self, **kwargs):
def async_turn_on(self, **kwargs):
"""Turn on light.""" """Turn on light."""
data = {'on': True} data = {'on': True}
@ -157,10 +153,9 @@ class DeconzLight(Light):
else: else:
data['effect'] = 'none' data['effect'] = 'none'
yield from self._light.async_set_state(data) await self._light.async_set_state(data)
@asyncio.coroutine async def async_turn_off(self, **kwargs):
def async_turn_off(self, **kwargs):
"""Turn off light.""" """Turn off light."""
data = {'on': False} data = {'on': False}
@ -176,4 +171,4 @@ class DeconzLight(Light):
data['alert'] = 'lselect' data['alert'] = 'lselect'
del data['on'] del data['on']
yield from self._light.async_set_state(data) await self._light.async_set_state(data)

View File

@ -4,8 +4,6 @@ Support for deCONZ scenes.
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/scene.deconz/ https://home-assistant.io/components/scene.deconz/
""" """
import asyncio
from homeassistant.components.deconz import ( from homeassistant.components.deconz import (
DOMAIN as DATA_DECONZ, DATA_DECONZ_ID) DOMAIN as DATA_DECONZ, DATA_DECONZ_ID)
from homeassistant.components.scene import Scene from homeassistant.components.scene import Scene
@ -13,8 +11,8 @@ from homeassistant.components.scene import Scene
DEPENDENCIES = ['deconz'] DEPENDENCIES = ['deconz']
@asyncio.coroutine async def async_setup_platform(hass, config, async_add_devices,
def async_setup_platform(hass, config, async_add_devices, discovery_info=None): discovery_info=None):
"""Set up scenes for deCONZ component.""" """Set up scenes for deCONZ component."""
if discovery_info is None: if discovery_info is None:
return return
@ -34,15 +32,13 @@ class DeconzScene(Scene):
"""Set up a scene.""" """Set up a scene."""
self._scene = scene self._scene = scene
@asyncio.coroutine async def async_added_to_hass(self):
def async_added_to_hass(self):
"""Subscribe to sensors events.""" """Subscribe to sensors events."""
self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._scene.deconz_id self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._scene.deconz_id
@asyncio.coroutine async def async_activate(self):
def async_activate(self):
"""Activate the scene.""" """Activate the scene."""
yield from self._scene.async_set_state({}) await self._scene.async_set_state({})
@property @property
def name(self): def name(self):

View File

@ -4,8 +4,6 @@ Support for deCONZ sensor.
For more details about this component, please refer to the documentation at For more details about this component, please refer to the documentation at
https://home-assistant.io/components/sensor.deconz/ https://home-assistant.io/components/sensor.deconz/
""" """
import asyncio
from homeassistant.components.deconz import ( from homeassistant.components.deconz import (
DOMAIN as DATA_DECONZ, DATA_DECONZ_ID) DOMAIN as DATA_DECONZ, DATA_DECONZ_ID)
from homeassistant.const import ATTR_BATTERY_LEVEL, CONF_EVENT, CONF_ID from homeassistant.const import ATTR_BATTERY_LEVEL, CONF_EVENT, CONF_ID
@ -19,8 +17,8 @@ DEPENDENCIES = ['deconz']
ATTR_EVENT_ID = 'event_id' ATTR_EVENT_ID = 'event_id'
@asyncio.coroutine async def async_setup_platform(hass, config, async_add_devices,
def async_setup_platform(hass, config, async_add_devices, discovery_info=None): discovery_info=None):
"""Set up the deCONZ sensors.""" """Set up the deCONZ sensors."""
if discovery_info is None: if discovery_info is None:
return return
@ -48,8 +46,7 @@ class DeconzSensor(Entity):
"""Set up sensor and add update callback to get data from websocket.""" """Set up sensor and add update callback to get data from websocket."""
self._sensor = sensor self._sensor = sensor
@asyncio.coroutine async def async_added_to_hass(self):
def async_added_to_hass(self):
"""Subscribe to sensors events.""" """Subscribe to sensors events."""
self._sensor.register_async_callback(self.async_update_callback) self._sensor.register_async_callback(self.async_update_callback)
self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._sensor.deconz_id self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._sensor.deconz_id
@ -125,8 +122,7 @@ class DeconzBattery(Entity):
self._device_class = 'battery' self._device_class = 'battery'
self._unit_of_measurement = "%" self._unit_of_measurement = "%"
@asyncio.coroutine async def async_added_to_hass(self):
def async_added_to_hass(self):
"""Subscribe to sensors events.""" """Subscribe to sensors events."""
self._device.register_async_callback(self.async_update_callback) self._device.register_async_callback(self.async_update_callback)
self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._device.deconz_id self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._device.deconz_id

View File

@ -701,7 +701,7 @@ pycsspeechtts==1.0.2
pydaikin==0.4 pydaikin==0.4
# homeassistant.components.deconz # homeassistant.components.deconz
pydeconz==30 pydeconz==31
# homeassistant.components.zwave # homeassistant.components.zwave
pydispatcher==2.0.5 pydispatcher==2.0.5