Use async syntax for cover platforms (#15230)

This commit is contained in:
cdce8p 2018-06-30 18:10:59 +02:00 committed by GitHub
parent 0aad056ca7
commit 3da4642194
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 74 deletions

View File

@ -4,7 +4,6 @@ Support for Lutron Caseta shades.
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/cover.lutron_caseta/ https://home-assistant.io/components/cover.lutron_caseta/
""" """
import asyncio
import logging import logging
from homeassistant.components.cover import ( from homeassistant.components.cover import (
@ -18,8 +17,8 @@ _LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['lutron_caseta'] DEPENDENCIES = ['lutron_caseta']
@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 Lutron Caseta shades as a cover device.""" """Set up the Lutron Caseta shades as a cover device."""
devs = [] devs = []
bridge = hass.data[LUTRON_CASETA_SMARTBRIDGE] bridge = hass.data[LUTRON_CASETA_SMARTBRIDGE]
@ -49,25 +48,21 @@ class LutronCasetaCover(LutronCasetaDevice, CoverDevice):
"""Return the current position of cover.""" """Return the current position of cover."""
return self._state['current_state'] return self._state['current_state']
@asyncio.coroutine async def async_close_cover(self, **kwargs):
def async_close_cover(self, **kwargs):
"""Close the cover.""" """Close the cover."""
self._smartbridge.set_value(self._device_id, 0) self._smartbridge.set_value(self._device_id, 0)
@asyncio.coroutine async def async_open_cover(self, **kwargs):
def async_open_cover(self, **kwargs):
"""Open the cover.""" """Open the cover."""
self._smartbridge.set_value(self._device_id, 100) self._smartbridge.set_value(self._device_id, 100)
@asyncio.coroutine async def async_set_cover_position(self, **kwargs):
def async_set_cover_position(self, **kwargs):
"""Move the shade to a specific position.""" """Move the shade to a specific position."""
if ATTR_POSITION in kwargs: if ATTR_POSITION in kwargs:
position = kwargs[ATTR_POSITION] position = kwargs[ATTR_POSITION]
self._smartbridge.set_value(self._device_id, position) self._smartbridge.set_value(self._device_id, position)
@asyncio.coroutine async def async_update(self):
def async_update(self):
"""Call when forcing a refresh of the device.""" """Call when forcing a refresh of the device."""
self._state = self._smartbridge.get_device_by_id(self._device_id) self._state = self._smartbridge.get_device_by_id(self._device_id)
_LOGGER.debug(self._state) _LOGGER.debug(self._state)

View File

@ -4,7 +4,6 @@ Support for MQTT cover devices.
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/cover.mqtt/ https://home-assistant.io/components/cover.mqtt/
""" """
import asyncio
import logging import logging
import voluptuous as vol import voluptuous as vol
@ -93,8 +92,8 @@ PLATFORM_SCHEMA = mqtt.MQTT_BASE_PLATFORM_SCHEMA.extend({
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema) }).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema)
@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 MQTT Cover.""" """Set up the MQTT Cover."""
if discovery_info is not None: if discovery_info is not None:
config = PLATFORM_SCHEMA(discovery_info) config = PLATFORM_SCHEMA(discovery_info)
@ -174,10 +173,9 @@ class MqttCover(MqttAvailability, CoverDevice):
self._position_topic = position_topic self._position_topic = position_topic
self._set_position_template = set_position_template self._set_position_template = set_position_template
@asyncio.coroutine async def async_added_to_hass(self):
def async_added_to_hass(self):
"""Subscribe MQTT events.""" """Subscribe MQTT events."""
yield from super().async_added_to_hass() await super().async_added_to_hass()
@callback @callback
def tilt_updated(topic, payload, qos): def tilt_updated(topic, payload, qos):
@ -218,7 +216,7 @@ class MqttCover(MqttAvailability, CoverDevice):
# Force into optimistic mode. # Force into optimistic mode.
self._optimistic = True self._optimistic = True
else: else:
yield from mqtt.async_subscribe( await mqtt.async_subscribe(
self.hass, self._state_topic, self.hass, self._state_topic,
state_message_received, self._qos) state_message_received, self._qos)
@ -227,7 +225,7 @@ class MqttCover(MqttAvailability, CoverDevice):
else: else:
self._tilt_optimistic = False self._tilt_optimistic = False
self._tilt_value = STATE_UNKNOWN self._tilt_value = STATE_UNKNOWN
yield from mqtt.async_subscribe( await mqtt.async_subscribe(
self.hass, self._tilt_status_topic, tilt_updated, self._qos) self.hass, self._tilt_status_topic, tilt_updated, self._qos)
@property @property
@ -278,8 +276,7 @@ class MqttCover(MqttAvailability, CoverDevice):
return supported_features return supported_features
@asyncio.coroutine async def async_open_cover(self, **kwargs):
def async_open_cover(self, **kwargs):
"""Move the cover up. """Move the cover up.
This method is a coroutine. This method is a coroutine.
@ -292,8 +289,7 @@ class MqttCover(MqttAvailability, CoverDevice):
self._state = False self._state = False
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
@asyncio.coroutine async def async_close_cover(self, **kwargs):
def async_close_cover(self, **kwargs):
"""Move the cover down. """Move the cover down.
This method is a coroutine. This method is a coroutine.
@ -306,8 +302,7 @@ class MqttCover(MqttAvailability, CoverDevice):
self._state = True self._state = True
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
@asyncio.coroutine async def async_stop_cover(self, **kwargs):
def async_stop_cover(self, **kwargs):
"""Stop the device. """Stop the device.
This method is a coroutine. This method is a coroutine.
@ -316,8 +311,7 @@ class MqttCover(MqttAvailability, CoverDevice):
self.hass, self._command_topic, self._payload_stop, self._qos, self.hass, self._command_topic, self._payload_stop, self._qos,
self._retain) self._retain)
@asyncio.coroutine async def async_open_cover_tilt(self, **kwargs):
def async_open_cover_tilt(self, **kwargs):
"""Tilt the cover open.""" """Tilt the cover open."""
mqtt.async_publish(self.hass, self._tilt_command_topic, mqtt.async_publish(self.hass, self._tilt_command_topic,
self._tilt_open_position, self._qos, self._tilt_open_position, self._qos,
@ -326,8 +320,7 @@ class MqttCover(MqttAvailability, CoverDevice):
self._tilt_value = self._tilt_open_position self._tilt_value = self._tilt_open_position
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
@asyncio.coroutine async def async_close_cover_tilt(self, **kwargs):
def async_close_cover_tilt(self, **kwargs):
"""Tilt the cover closed.""" """Tilt the cover closed."""
mqtt.async_publish(self.hass, self._tilt_command_topic, mqtt.async_publish(self.hass, self._tilt_command_topic,
self._tilt_closed_position, self._qos, self._tilt_closed_position, self._qos,
@ -336,8 +329,7 @@ class MqttCover(MqttAvailability, CoverDevice):
self._tilt_value = self._tilt_closed_position self._tilt_value = self._tilt_closed_position
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
@asyncio.coroutine async def async_set_cover_tilt_position(self, **kwargs):
def async_set_cover_tilt_position(self, **kwargs):
"""Move the cover tilt to a specific position.""" """Move the cover tilt to a specific position."""
if ATTR_TILT_POSITION not in kwargs: if ATTR_TILT_POSITION not in kwargs:
return return
@ -350,8 +342,7 @@ class MqttCover(MqttAvailability, CoverDevice):
mqtt.async_publish(self.hass, self._tilt_command_topic, mqtt.async_publish(self.hass, self._tilt_command_topic,
level, self._qos, self._retain) level, self._qos, self._retain)
@asyncio.coroutine async def async_set_cover_position(self, **kwargs):
def async_set_cover_position(self, **kwargs):
"""Move the cover to a specific position.""" """Move the cover to a specific position."""
if ATTR_POSITION in kwargs: if ATTR_POSITION in kwargs:
position = kwargs[ATTR_POSITION] position = kwargs[ATTR_POSITION]

View File

@ -4,7 +4,6 @@ Support for Rflink Cover devices.
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/cover.rflink/ https://home-assistant.io/components/cover.rflink/
""" """
import asyncio
import logging import logging
import voluptuous as vol import voluptuous as vol
@ -79,8 +78,8 @@ def devices_from_config(domain_config, hass=None):
return devices return devices
@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 Rflink cover platform.""" """Set up the Rflink cover platform."""
async_add_devices(devices_from_config(config, hass)) async_add_devices(devices_from_config(config, hass))

View File

@ -4,7 +4,6 @@ Support for covers which integrate with other components.
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/cover.template/ https://home-assistant.io/components/cover.template/
""" """
import asyncio
import logging import logging
import voluptuous as vol import voluptuous as vol
@ -72,8 +71,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
}) })
@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 Template cover.""" """Set up the Template cover."""
covers = [] covers = []
@ -199,8 +198,7 @@ class CoverTemplate(CoverDevice):
if self._entity_picture_template is not None: if self._entity_picture_template is not None:
self._entity_picture_template.hass = self.hass self._entity_picture_template.hass = self.hass
@asyncio.coroutine async def async_added_to_hass(self):
def async_added_to_hass(self):
"""Register callbacks.""" """Register callbacks."""
@callback @callback
def template_cover_state_listener(entity, old_state, new_state): def template_cover_state_listener(entity, old_state, new_state):
@ -277,70 +275,62 @@ class CoverTemplate(CoverDevice):
"""Return the polling state.""" """Return the polling state."""
return False return False
@asyncio.coroutine async def async_open_cover(self, **kwargs):
def async_open_cover(self, **kwargs):
"""Move the cover up.""" """Move the cover up."""
if self._open_script: if self._open_script:
yield from self._open_script.async_run() await self._open_script.async_run()
elif self._position_script: elif self._position_script:
yield from self._position_script.async_run({"position": 100}) await self._position_script.async_run({"position": 100})
if self._optimistic: if self._optimistic:
self._position = 100 self._position = 100
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
@asyncio.coroutine async def async_close_cover(self, **kwargs):
def async_close_cover(self, **kwargs):
"""Move the cover down.""" """Move the cover down."""
if self._close_script: if self._close_script:
yield from self._close_script.async_run() await self._close_script.async_run()
elif self._position_script: elif self._position_script:
yield from self._position_script.async_run({"position": 0}) await self._position_script.async_run({"position": 0})
if self._optimistic: if self._optimistic:
self._position = 0 self._position = 0
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
@asyncio.coroutine async def async_stop_cover(self, **kwargs):
def async_stop_cover(self, **kwargs):
"""Fire the stop action.""" """Fire the stop action."""
if self._stop_script: if self._stop_script:
yield from self._stop_script.async_run() await self._stop_script.async_run()
@asyncio.coroutine async def async_set_cover_position(self, **kwargs):
def async_set_cover_position(self, **kwargs):
"""Set cover position.""" """Set cover position."""
self._position = kwargs[ATTR_POSITION] self._position = kwargs[ATTR_POSITION]
yield from self._position_script.async_run( await self._position_script.async_run(
{"position": self._position}) {"position": self._position})
if self._optimistic: if self._optimistic:
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
@asyncio.coroutine async def async_open_cover_tilt(self, **kwargs):
def async_open_cover_tilt(self, **kwargs):
"""Tilt the cover open.""" """Tilt the cover open."""
self._tilt_value = 100 self._tilt_value = 100
yield from self._tilt_script.async_run({"tilt": self._tilt_value}) await self._tilt_script.async_run({"tilt": self._tilt_value})
if self._tilt_optimistic: if self._tilt_optimistic:
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
@asyncio.coroutine async def async_close_cover_tilt(self, **kwargs):
def async_close_cover_tilt(self, **kwargs):
"""Tilt the cover closed.""" """Tilt the cover closed."""
self._tilt_value = 0 self._tilt_value = 0
yield from self._tilt_script.async_run( await self._tilt_script.async_run(
{"tilt": self._tilt_value}) {"tilt": self._tilt_value})
if self._tilt_optimistic: if self._tilt_optimistic:
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
@asyncio.coroutine async def async_set_cover_tilt_position(self, **kwargs):
def async_set_cover_tilt_position(self, **kwargs):
"""Move the cover tilt to a specific position.""" """Move the cover tilt to a specific position."""
self._tilt_value = kwargs[ATTR_TILT_POSITION] self._tilt_value = kwargs[ATTR_TILT_POSITION]
yield from self._tilt_script.async_run({"tilt": self._tilt_value}) await self._tilt_script.async_run({"tilt": self._tilt_value})
if self._tilt_optimistic: if self._tilt_optimistic:
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
@asyncio.coroutine async def async_update(self):
def async_update(self):
"""Update the state from the template.""" """Update the state from the template."""
if self._template is not None: if self._template is not None:
try: try:

View File

@ -5,7 +5,6 @@ For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/cover.velbus/ https://home-assistant.io/components/cover.velbus/
""" """
import logging import logging
import asyncio
import time import time
import voluptuous as vol import voluptuous as vol
@ -70,15 +69,14 @@ class VelbusCover(CoverDevice):
self._open_channel = open_channel self._open_channel = open_channel
self._close_channel = close_channel self._close_channel = close_channel
@asyncio.coroutine async def async_added_to_hass(self):
def async_added_to_hass(self):
"""Add listener for Velbus messages on bus.""" """Add listener for Velbus messages on bus."""
def _init_velbus(): def _init_velbus():
"""Initialize Velbus on startup.""" """Initialize Velbus on startup."""
self._velbus.subscribe(self._on_message) self._velbus.subscribe(self._on_message)
self.get_status() self.get_status()
yield from self.hass.async_add_job(_init_velbus) await self.hass.async_add_job(_init_velbus)
def _on_message(self, message): def _on_message(self, message):
import velbus import velbus

View File

@ -4,8 +4,6 @@ Support for Wink Covers.
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/cover.wink/ https://home-assistant.io/components/cover.wink/
""" """
import asyncio
from homeassistant.components.cover import CoverDevice, STATE_UNKNOWN, \ from homeassistant.components.cover import CoverDevice, STATE_UNKNOWN, \
ATTR_POSITION ATTR_POSITION
from homeassistant.components.wink import WinkDevice, DOMAIN from homeassistant.components.wink import WinkDevice, DOMAIN
@ -34,8 +32,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class WinkCoverDevice(WinkDevice, CoverDevice): class WinkCoverDevice(WinkDevice, CoverDevice):
"""Representation of a Wink cover device.""" """Representation of a Wink cover device."""
@asyncio.coroutine async def async_added_to_hass(self):
def async_added_to_hass(self):
"""Call when entity is added to hass.""" """Call when entity is added to hass."""
self.hass.data[DOMAIN]['entities']['cover'].append(self) self.hass.data[DOMAIN]['entities']['cover'].append(self)