Define CoverEntity entity attributes as class variables (#51236)

* Define CoverEntity entity attributes as class variables

* Fix supported features
This commit is contained in:
Franck Nijhof 2021-05-29 14:35:02 +02:00 committed by GitHub
parent d66d7cbd37
commit 255e13930c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 9 deletions

View File

@ -1,4 +1,6 @@
"""Support for Cover devices.""" """Support for Cover devices."""
from __future__ import annotations
from datetime import timedelta from datetime import timedelta
import functools as ft import functools as ft
import logging import logging
@ -167,22 +169,32 @@ async def async_unload_entry(hass, entry):
class CoverEntity(Entity): class CoverEntity(Entity):
"""Base class for cover entities.""" """Base class for cover entities."""
_attr_current_cover_position: int | None = None
_attr_current_cover_tilt_position: int | None = None
_attr_is_closed: bool | None
_attr_is_closing: bool | None = None
_attr_is_opening: bool | None = None
_attr_state: None = None
@property @property
def current_cover_position(self): def current_cover_position(self) -> int | None:
"""Return current position of cover. """Return current position of cover.
None is unknown, 0 is closed, 100 is fully open. None is unknown, 0 is closed, 100 is fully open.
""" """
return self._attr_current_cover_position
@property @property
def current_cover_tilt_position(self): def current_cover_tilt_position(self) -> int | None:
"""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.
""" """
return self._attr_current_cover_tilt_position
@property @property
def state(self): @final
def state(self) -> str | None:
"""Return the state of the cover.""" """Return the state of the cover."""
if self.is_opening: if self.is_opening:
return STATE_OPENING return STATE_OPENING
@ -213,8 +225,11 @@ class CoverEntity(Entity):
return data return data
@property @property
def supported_features(self): def supported_features(self) -> int:
"""Flag supported features.""" """Flag supported features."""
if self._attr_supported_features is not None:
return self._attr_supported_features
supported_features = SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP supported_features = SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP
if self.current_cover_position is not None: if self.current_cover_position is not None:
@ -231,17 +246,19 @@ class CoverEntity(Entity):
return supported_features return supported_features
@property @property
def is_opening(self): def is_opening(self) -> bool | None:
"""Return if the cover is opening or not.""" """Return if the cover is opening or not."""
return self._attr_is_opening
@property @property
def is_closing(self): def is_closing(self) -> bool | None:
"""Return if the cover is closing or not.""" """Return if the cover is closing or not."""
return self._attr_is_closing
@property @property
def is_closed(self): def is_closed(self) -> bool | None:
"""Return if the cover is closed or not.""" """Return if the cover is closed or not."""
raise NotImplementedError() return self._attr_is_closed
def open_cover(self, **kwargs: Any) -> None: def open_cover(self, **kwargs: Any) -> None:
"""Open the cover.""" """Open the cover."""

View File

@ -147,7 +147,7 @@ class ZwaveMotorizedBarrier(ZWaveBaseEntity, CoverEntity):
) )
@property @property
def supported_features(self) -> int | None: def supported_features(self) -> int:
"""Flag supported features.""" """Flag supported features."""
return SUPPORT_OPEN | SUPPORT_CLOSE return SUPPORT_OPEN | SUPPORT_CLOSE