From b272e16b4450d296afca3f549b051361f545ecab Mon Sep 17 00:00:00 2001 From: Thibaut Date: Mon, 23 Nov 2020 19:13:52 +0100 Subject: [PATCH] Fix RTS cover with set position available (#43555) --- homeassistant/components/somfy/__init__.py | 13 ++++-- homeassistant/components/somfy/cover.py | 52 +++++++++++++++++----- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/somfy/__init__.py b/homeassistant/components/somfy/__init__.py index 99b0a2ee564..728e54b456f 100644 --- a/homeassistant/components/somfy/__init__.py +++ b/homeassistant/components/somfy/__init__.py @@ -164,12 +164,12 @@ class SomfyEntity(CoordinatorEntity, Entity): return self.coordinator.data[self._id] @property - def unique_id(self): + def unique_id(self) -> str: """Return the unique id base on the id returned by Somfy.""" return self._id @property - def name(self): + def name(self) -> str: """Return the name of the device.""" return self.device.name @@ -188,13 +188,18 @@ class SomfyEntity(CoordinatorEntity, Entity): "manufacturer": "Somfy", } - def has_capability(self, capability): + def has_capability(self, capability: str) -> bool: """Test if device has a capability.""" capabilities = self.device.capabilities return bool([c for c in capabilities if c.name == capability]) + def has_state(self, state: str) -> bool: + """Test if device has a state.""" + states = self.device.states + return bool([c for c in states if c.name == state]) + @property - def assumed_state(self): + def assumed_state(self) -> bool: """Return if the device has an assumed state.""" return not bool(self.device.states) diff --git a/homeassistant/components/somfy/cover.py b/homeassistant/components/somfy/cover.py index 605d58a941b..696412ac3c7 100644 --- a/homeassistant/components/somfy/cover.py +++ b/homeassistant/components/somfy/cover.py @@ -8,6 +8,14 @@ from homeassistant.components.cover import ( ATTR_TILT_POSITION, DEVICE_CLASS_BLIND, DEVICE_CLASS_SHUTTER, + SUPPORT_CLOSE, + SUPPORT_CLOSE_TILT, + SUPPORT_OPEN, + SUPPORT_OPEN_TILT, + SUPPORT_SET_POSITION, + SUPPORT_SET_TILT_POSITION, + SUPPORT_STOP, + SUPPORT_STOP_TILT, CoverEntity, ) from homeassistant.const import STATE_CLOSED, STATE_OPEN @@ -57,10 +65,32 @@ class SomfyCover(SomfyEntity, RestoreEntity, CoverEntity): self.cover = None self._create_device() - def _create_device(self): + def _create_device(self) -> Blind: """Update the device with the latest data.""" self.cover = Blind(self.device, self.api) + @property + def supported_features(self) -> int: + """Flag supported features.""" + supported_features = 0 + if self.has_capability("open"): + supported_features |= SUPPORT_OPEN + if self.has_capability("close"): + supported_features |= SUPPORT_CLOSE + if self.has_capability("stop"): + supported_features |= SUPPORT_STOP + if self.has_capability("position"): + supported_features |= SUPPORT_SET_POSITION + if self.has_capability("rotation"): + supported_features |= ( + SUPPORT_OPEN_TILT + | SUPPORT_CLOSE_TILT + | SUPPORT_STOP_TILT + | SUPPORT_SET_TILT_POSITION + ) + + return supported_features + async def async_close_cover(self, **kwargs): """Close the cover.""" self._is_closing = True @@ -105,10 +135,9 @@ class SomfyCover(SomfyEntity, RestoreEntity, CoverEntity): @property def current_cover_position(self): """Return the current position of cover shutter.""" - position = None - if self.has_capability("position"): - position = 100 - self.cover.get_position() - return position + if not self.has_state("position"): + return None + return 100 - self.cover.get_position() @property def is_opening(self): @@ -125,25 +154,24 @@ class SomfyCover(SomfyEntity, RestoreEntity, CoverEntity): return self._is_closing @property - def is_closed(self): + def is_closed(self) -> bool: """Return if the cover is closed.""" is_closed = None - if self.has_capability("position"): + if self.has_state("position"): is_closed = self.cover.is_closed() elif self.optimistic: is_closed = self._closed return is_closed @property - def current_cover_tilt_position(self): + def current_cover_tilt_position(self) -> int: """Return current position of cover tilt. None is unknown, 0 is closed, 100 is fully open. """ - orientation = None - if self.has_capability("rotation"): - orientation = 100 - self.cover.orientation - return orientation + if not self.has_state("orientation"): + return None + return 100 - self.cover.orientation def set_cover_tilt_position(self, **kwargs): """Move the cover tilt to a specific position."""