Add supported_features to cover component (#6082)

This commit is contained in:
Adam Mills 2017-02-18 21:11:03 -05:00 committed by Paulus Schoutsen
parent 3cb1a5dd89
commit 5f095b5126
4 changed files with 58 additions and 4 deletions

View file

@ -38,6 +38,15 @@ DEVICE_CLASSES = [
'garage', # Garage door control
]
SUPPORT_OPEN = 1
SUPPORT_CLOSE = 2
SUPPORT_SET_POSITION = 4
SUPPORT_STOP = 8
SUPPORT_OPEN_TILT = 16
SUPPORT_CLOSE_TILT = 32
SUPPORT_STOP_TILT = 64
SUPPORT_SET_TILT_POSITION = 128
_LOGGER = logging.getLogger(__name__)
ATTR_CURRENT_POSITION = 'current_position'
@ -226,6 +235,21 @@ class CoverDevice(Entity):
return data
@property
def supported_features(self):
"""Flag supported features."""
supported_features = SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP
if self.current_cover_position is not None:
supported_features |= SUPPORT_SET_POSITION
if self.current_cover_tilt_position is not None:
supported_features |= (
SUPPORT_OPEN_TILT | SUPPORT_CLOSE_TILT | SUPPORT_STOP_TILT |
SUPPORT_SET_TILT_POSITION)
return supported_features
@property
def is_closed(self):
"""Return if the cover is closed or not."""

View file

@ -4,7 +4,8 @@ Demo platform for the cover component.
For more details about this platform, please refer to the documentation
https://home-assistant.io/components/demo/
"""
from homeassistant.components.cover import CoverDevice
from homeassistant.components.cover import (
CoverDevice, SUPPORT_OPEN, SUPPORT_CLOSE)
from homeassistant.helpers.event import track_utc_time_change
@ -14,7 +15,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
DemoCover(hass, 'Kitchen Window'),
DemoCover(hass, 'Hall Window', 10),
DemoCover(hass, 'Living Room Window', 70, 50),
DemoCover(hass, 'Garage Door', device_class='garage'),
DemoCover(hass, 'Garage Door', device_class='garage',
supported_features=(SUPPORT_OPEN | SUPPORT_CLOSE)),
])
@ -23,12 +25,13 @@ class DemoCover(CoverDevice):
# pylint: disable=no-self-use
def __init__(self, hass, name, position=None, tilt_position=None,
device_class=None):
device_class=None, supported_features=None):
"""Initialize the cover."""
self.hass = hass
self._name = name
self._position = position
self._device_class = device_class
self._supported_features = supported_features
self._set_position = None
self._set_tilt_position = None
self._tilt_position = tilt_position
@ -71,6 +74,14 @@ class DemoCover(CoverDevice):
"""Return the class of this device, from component DEVICE_CLASSES."""
return self._device_class
@property
def supported_features(self):
"""Flag supported features."""
if self._supported_features is not None:
return self._supported_features
else:
return super().supported_features
def close_cover(self, **kwargs):
"""Close the cover."""
if self._position == 0:

View file

@ -7,7 +7,8 @@ https://home-assistant.io/components/cover.zwave/
# Because we do not compile openzwave on CI
# pylint: disable=import-error
import logging
from homeassistant.components.cover import DOMAIN
from homeassistant.components.cover import (
DOMAIN, SUPPORT_OPEN, SUPPORT_CLOSE)
from homeassistant.components.zwave import ZWaveDeviceEntity
from homeassistant.components import zwave
from homeassistant.components.zwave import workaround
@ -15,6 +16,8 @@ from homeassistant.components.cover import CoverDevice
_LOGGER = logging.getLogger(__name__)
SUPPORT_GARAGE = SUPPORT_OPEN | SUPPORT_CLOSE
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Find and return Z-Wave covers."""
@ -140,3 +143,8 @@ class ZwaveGarageDoor(zwave.ZWaveDeviceEntity, CoverDevice):
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return 'garage'
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_GARAGE

View file

@ -24,6 +24,17 @@ class TestCoverDemo(unittest.TestCase):
"""Stop down everything that was started."""
self.hass.stop()
def test_supported_features(self):
"""Test cover supported features."""
state = self.hass.states.get('cover.garage_door')
self.assertEqual(3, state.attributes.get('supported_features'))
state = self.hass.states.get('cover.kitchen_window')
self.assertEqual(11, state.attributes.get('supported_features'))
state = self.hass.states.get('cover.hall_window')
self.assertEqual(15, state.attributes.get('supported_features'))
state = self.hass.states.get('cover.living_room_window')
self.assertEqual(255, state.attributes.get('supported_features'))
def test_close_cover(self):
"""Test closing the cover."""
state = self.hass.states.get(ENTITY_COVER)