Use shorthand attrs for velux (#100294)

* Use shorthand attrs for velux

* Update homeassistant/components/velux/cover.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* black

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Jan Bouwhuis 2023-09-13 16:50:33 +02:00 committed by GitHub
parent f2f45380a9
commit 871800778f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 32 deletions

View File

@ -1,7 +1,7 @@
"""Support for VELUX KLF 200 devices.""" """Support for VELUX KLF 200 devices."""
import logging import logging
from pyvlx import PyVLX, PyVLXException from pyvlx import OpeningDevice, PyVLX, PyVLXException
import voluptuous as vol import voluptuous as vol
from homeassistant.const import ( from homeassistant.const import (
@ -90,9 +90,11 @@ class VeluxEntity(Entity):
_attr_should_poll = False _attr_should_poll = False
def __init__(self, node): def __init__(self, node: OpeningDevice) -> None:
"""Initialize the Velux device.""" """Initialize the Velux device."""
self.node = node self.node = node
self._attr_unique_id = node.serial_number
self._attr_name = node.name if node.name else "#" + str(node.node_id)
@callback @callback
def async_register_callbacks(self): def async_register_callbacks(self):
@ -107,15 +109,3 @@ class VeluxEntity(Entity):
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Store register state change callback.""" """Store register state change callback."""
self.async_register_callbacks() self.async_register_callbacks()
@property
def unique_id(self) -> str:
"""Return the unique id base on the serial_id returned by Velux."""
return self.node.serial_number
@property
def name(self):
"""Return the name of the Velux device."""
if not self.node.name:
return "#" + str(self.node.node_id)
return self.node.name

View File

@ -39,6 +39,26 @@ async def async_setup_platform(
class VeluxCover(VeluxEntity, CoverEntity): class VeluxCover(VeluxEntity, CoverEntity):
"""Representation of a Velux cover.""" """Representation of a Velux cover."""
_is_blind = False
def __init__(self, node: OpeningDevice) -> None:
"""Initialize VeluxCover."""
super().__init__(node)
self._attr_device_class = CoverDeviceClass.WINDOW
if isinstance(node, Awning):
self._attr_device_class = CoverDeviceClass.AWNING
if isinstance(node, Blind):
self._attr_device_class = CoverDeviceClass.BLIND
self._is_blind = True
if isinstance(node, GarageDoor):
self._attr_device_class = CoverDeviceClass.GARAGE
if isinstance(node, Gate):
self._attr_device_class = CoverDeviceClass.GATE
if isinstance(node, RollerShutter):
self._attr_device_class = CoverDeviceClass.SHUTTER
if isinstance(node, Window):
self._attr_device_class = CoverDeviceClass.WINDOW
@property @property
def supported_features(self) -> CoverEntityFeature: def supported_features(self) -> CoverEntityFeature:
"""Flag supported features.""" """Flag supported features."""
@ -65,27 +85,10 @@ class VeluxCover(VeluxEntity, CoverEntity):
@property @property
def current_cover_tilt_position(self) -> int | None: def current_cover_tilt_position(self) -> int | None:
"""Return the current position of the cover.""" """Return the current position of the cover."""
if isinstance(self.node, Blind): if self._is_blind:
return 100 - self.node.orientation.position_percent return 100 - self.node.orientation.position_percent
return None return None
@property
def device_class(self) -> CoverDeviceClass:
"""Define this cover as either awning, blind, garage, gate, shutter or window."""
if isinstance(self.node, Awning):
return CoverDeviceClass.AWNING
if isinstance(self.node, Blind):
return CoverDeviceClass.BLIND
if isinstance(self.node, GarageDoor):
return CoverDeviceClass.GARAGE
if isinstance(self.node, Gate):
return CoverDeviceClass.GATE
if isinstance(self.node, RollerShutter):
return CoverDeviceClass.SHUTTER
if isinstance(self.node, Window):
return CoverDeviceClass.WINDOW
return CoverDeviceClass.WINDOW
@property @property
def is_closed(self) -> bool: def is_closed(self) -> bool:
"""Return if the cover is closed.""" """Return if the cover is closed."""