Use shorthand attributes in Smartthings (#100215)

This commit is contained in:
Joost Lekkerkerker 2023-09-12 19:26:33 +02:00 committed by GitHub
parent e84a4661b0
commit a09372590f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 59 additions and 175 deletions

View file

@ -77,10 +77,8 @@ class SmartThingsCover(SmartThingsEntity, CoverEntity):
def __init__(self, device):
"""Initialize the cover class."""
super().__init__(device)
self._device_class = None
self._current_cover_position = None
self._state = None
self._state_attrs = None
self._attr_supported_features = (
CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
)
@ -90,6 +88,13 @@ class SmartThingsCover(SmartThingsEntity, CoverEntity):
):
self._attr_supported_features |= CoverEntityFeature.SET_POSITION
if Capability.door_control in device.capabilities:
self._attr_device_class = CoverDeviceClass.DOOR
elif Capability.window_shade in device.capabilities:
self._attr_device_class = CoverDeviceClass.SHADE
elif Capability.garage_door_control in device.capabilities:
self._attr_device_class = CoverDeviceClass.GARAGE
async def async_close_cover(self, **kwargs: Any) -> None:
"""Close cover."""
# Same command for all 3 supported capabilities
@ -121,24 +126,21 @@ class SmartThingsCover(SmartThingsEntity, CoverEntity):
async def async_update(self) -> None:
"""Update the attrs of the cover."""
if Capability.door_control in self._device.capabilities:
self._device_class = CoverDeviceClass.DOOR
self._state = VALUE_TO_STATE.get(self._device.status.door)
elif Capability.window_shade in self._device.capabilities:
self._device_class = CoverDeviceClass.SHADE
self._state = VALUE_TO_STATE.get(self._device.status.window_shade)
elif Capability.garage_door_control in self._device.capabilities:
self._device_class = CoverDeviceClass.GARAGE
self._state = VALUE_TO_STATE.get(self._device.status.door)
if Capability.window_shade_level in self._device.capabilities:
self._current_cover_position = self._device.status.shade_level
self._attr_current_cover_position = self._device.status.shade_level
elif Capability.switch_level in self._device.capabilities:
self._current_cover_position = self._device.status.level
self._attr_current_cover_position = self._device.status.level
self._state_attrs = {}
self._attr_extra_state_attributes = {}
battery = self._device.status.attributes[Attribute.battery].value
if battery is not None:
self._state_attrs[ATTR_BATTERY_LEVEL] = battery
self._attr_extra_state_attributes[ATTR_BATTERY_LEVEL] = battery
@property
def is_opening(self) -> bool:
@ -156,18 +158,3 @@ class SmartThingsCover(SmartThingsEntity, CoverEntity):
if self._state == STATE_CLOSED:
return True
return None if self._state is None else False
@property
def current_cover_position(self) -> int | None:
"""Return current position of cover."""
return self._current_cover_position
@property
def device_class(self) -> CoverDeviceClass | None:
"""Define this cover as a garage door."""
return self._device_class
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Get additional state attributes."""
return self._state_attrs