Fix RTS cover with set position available (#43555)

pull/41267/head^2
Thibaut 2020-11-23 19:13:52 +01:00 committed by GitHub
parent 0c64873c10
commit b272e16b44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 16 deletions

View File

@ -164,12 +164,12 @@ class SomfyEntity(CoordinatorEntity, Entity):
return self.coordinator.data[self._id] return self.coordinator.data[self._id]
@property @property
def unique_id(self): def unique_id(self) -> str:
"""Return the unique id base on the id returned by Somfy.""" """Return the unique id base on the id returned by Somfy."""
return self._id return self._id
@property @property
def name(self): def name(self) -> str:
"""Return the name of the device.""" """Return the name of the device."""
return self.device.name return self.device.name
@ -188,13 +188,18 @@ class SomfyEntity(CoordinatorEntity, Entity):
"manufacturer": "Somfy", "manufacturer": "Somfy",
} }
def has_capability(self, capability): def has_capability(self, capability: str) -> bool:
"""Test if device has a capability.""" """Test if device has a capability."""
capabilities = self.device.capabilities capabilities = self.device.capabilities
return bool([c for c in capabilities if c.name == capability]) 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 @property
def assumed_state(self): def assumed_state(self) -> bool:
"""Return if the device has an assumed state.""" """Return if the device has an assumed state."""
return not bool(self.device.states) return not bool(self.device.states)

View File

@ -8,6 +8,14 @@ from homeassistant.components.cover import (
ATTR_TILT_POSITION, ATTR_TILT_POSITION,
DEVICE_CLASS_BLIND, DEVICE_CLASS_BLIND,
DEVICE_CLASS_SHUTTER, 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, CoverEntity,
) )
from homeassistant.const import STATE_CLOSED, STATE_OPEN from homeassistant.const import STATE_CLOSED, STATE_OPEN
@ -57,10 +65,32 @@ class SomfyCover(SomfyEntity, RestoreEntity, CoverEntity):
self.cover = None self.cover = None
self._create_device() self._create_device()
def _create_device(self): def _create_device(self) -> Blind:
"""Update the device with the latest data.""" """Update the device with the latest data."""
self.cover = Blind(self.device, self.api) 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): async def async_close_cover(self, **kwargs):
"""Close the cover.""" """Close the cover."""
self._is_closing = True self._is_closing = True
@ -105,10 +135,9 @@ class SomfyCover(SomfyEntity, RestoreEntity, CoverEntity):
@property @property
def current_cover_position(self): def current_cover_position(self):
"""Return the current position of cover shutter.""" """Return the current position of cover shutter."""
position = None if not self.has_state("position"):
if self.has_capability("position"): return None
position = 100 - self.cover.get_position() return 100 - self.cover.get_position()
return position
@property @property
def is_opening(self): def is_opening(self):
@ -125,25 +154,24 @@ class SomfyCover(SomfyEntity, RestoreEntity, CoverEntity):
return self._is_closing return self._is_closing
@property @property
def is_closed(self): def is_closed(self) -> bool:
"""Return if the cover is closed.""" """Return if the cover is closed."""
is_closed = None is_closed = None
if self.has_capability("position"): if self.has_state("position"):
is_closed = self.cover.is_closed() is_closed = self.cover.is_closed()
elif self.optimistic: elif self.optimistic:
is_closed = self._closed is_closed = self._closed
return is_closed return is_closed
@property @property
def current_cover_tilt_position(self): def current_cover_tilt_position(self) -> int:
"""Return current position of cover tilt. """Return current position of cover tilt.
None is unknown, 0 is closed, 100 is fully open. None is unknown, 0 is closed, 100 is fully open.
""" """
orientation = None if not self.has_state("orientation"):
if self.has_capability("rotation"): return None
orientation = 100 - self.cover.orientation return 100 - self.cover.orientation
return orientation
def set_cover_tilt_position(self, **kwargs): def set_cover_tilt_position(self, **kwargs):
"""Move the cover tilt to a specific position.""" """Move the cover tilt to a specific position."""