Add type hints in fibaro device (#106874)

* Add typings in fibaro device

* Fix type hints

* Fix type hints

* Remove unused method parameter

* Improve log message

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
rappenze 2024-04-22 09:29:10 +02:00 committed by GitHub
parent 70d4b4d20d
commit aeaa1f84c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 19 deletions

View File

@ -454,37 +454,38 @@ class FibaroDevice(Entity):
if not fibaro_device.visible: if not fibaro_device.visible:
self._attr_entity_registry_visible_default = False self._attr_entity_registry_visible_default = False
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Call when entity is added to hass.""" """Call when entity is added to hass."""
self.controller.register(self.fibaro_device.fibaro_id, self._update_callback) self.controller.register(self.fibaro_device.fibaro_id, self._update_callback)
def _update_callback(self): def _update_callback(self) -> None:
"""Update the state.""" """Update the state."""
self.schedule_update_ha_state(True) self.schedule_update_ha_state(True)
@property @property
def level(self): def level(self) -> int | None:
"""Get the level of Fibaro device.""" """Get the level of Fibaro device."""
if self.fibaro_device.value.has_value: if self.fibaro_device.value.has_value:
return self.fibaro_device.value.int_value() return self.fibaro_device.value.int_value()
return None return None
@property @property
def level2(self): def level2(self) -> int | None:
"""Get the tilt level of Fibaro device.""" """Get the tilt level of Fibaro device."""
if self.fibaro_device.value_2.has_value: if self.fibaro_device.value_2.has_value:
return self.fibaro_device.value_2.int_value() return self.fibaro_device.value_2.int_value()
return None return None
def dont_know_message(self, action): def dont_know_message(self, cmd: str) -> None:
"""Make a warning in case we don't know how to perform an action.""" """Make a warning in case we don't know how to perform an action."""
_LOGGER.warning( _LOGGER.warning(
"Not sure how to setValue: %s (available actions: %s)", "Not sure how to %s: %s (available actions: %s)",
cmd,
str(self.ha_id), str(self.ha_id),
str(self.fibaro_device.actions), str(self.fibaro_device.actions),
) )
def set_level(self, level): def set_level(self, level: int) -> None:
"""Set the level of Fibaro device.""" """Set the level of Fibaro device."""
self.action("setValue", level) self.action("setValue", level)
if self.fibaro_device.value.has_value: if self.fibaro_device.value.has_value:
@ -492,21 +493,21 @@ class FibaroDevice(Entity):
if self.fibaro_device.has_brightness: if self.fibaro_device.has_brightness:
self.fibaro_device.properties["brightness"] = level self.fibaro_device.properties["brightness"] = level
def set_level2(self, level): def set_level2(self, level: int) -> None:
"""Set the level2 of Fibaro device.""" """Set the level2 of Fibaro device."""
self.action("setValue2", level) self.action("setValue2", level)
if self.fibaro_device.value_2.has_value: if self.fibaro_device.value_2.has_value:
self.fibaro_device.properties["value2"] = level self.fibaro_device.properties["value2"] = level
def call_turn_on(self): def call_turn_on(self) -> None:
"""Turn on the Fibaro device.""" """Turn on the Fibaro device."""
self.action("turnOn") self.action("turnOn")
def call_turn_off(self): def call_turn_off(self) -> None:
"""Turn off the Fibaro device.""" """Turn off the Fibaro device."""
self.action("turnOff") self.action("turnOff")
def call_set_color(self, red, green, blue, white): def call_set_color(self, red: int, green: int, blue: int, white: int) -> None:
"""Set the color of Fibaro device.""" """Set the color of Fibaro device."""
red = int(max(0, min(255, red))) red = int(max(0, min(255, red)))
green = int(max(0, min(255, green))) green = int(max(0, min(255, green)))
@ -516,7 +517,7 @@ class FibaroDevice(Entity):
self.fibaro_device.properties["color"] = color_str self.fibaro_device.properties["color"] = color_str
self.action("setColor", str(red), str(green), str(blue), str(white)) self.action("setColor", str(red), str(green), str(blue), str(white))
def action(self, cmd, *args): def action(self, cmd: str, *args: Any) -> None:
"""Perform an action on the Fibaro HC.""" """Perform an action on the Fibaro HC."""
if cmd in self.fibaro_device.actions: if cmd in self.fibaro_device.actions:
self.fibaro_device.execute_action(cmd, args) self.fibaro_device.execute_action(cmd, args)
@ -525,12 +526,12 @@ class FibaroDevice(Entity):
self.dont_know_message(cmd) self.dont_know_message(cmd)
@property @property
def current_binary_state(self): def current_binary_state(self) -> bool:
"""Return the current binary state.""" """Return the current binary state."""
return self.fibaro_device.value.bool_value(False) return self.fibaro_device.value.bool_value(False)
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> Mapping[str, Any]:
"""Return the state attributes of the device.""" """Return the state attributes of the device."""
attr = {"fibaro_id": self.fibaro_device.fibaro_id} attr = {"fibaro_id": self.fibaro_device.fibaro_id}

View File

@ -76,9 +76,9 @@ class FibaroBinarySensor(FibaroDevice, BinarySensorEntity):
self._attr_icon = SENSOR_TYPES[self._fibaro_sensor_type][1] self._attr_icon = SENSOR_TYPES[self._fibaro_sensor_type][1]
@property @property
def extra_state_attributes(self) -> Mapping[str, Any] | None: def extra_state_attributes(self) -> Mapping[str, Any]:
"""Return the extra state attributes of the device.""" """Return the extra state attributes of the device."""
return super().extra_state_attributes | self._own_extra_state_attributes return {**super().extra_state_attributes, **self._own_extra_state_attributes}
def update(self) -> None: def update(self) -> None:
"""Get the latest data and update the state.""" """Get the latest data and update the state."""

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Any from typing import Any, cast
from pyfibaro.fibaro_device import DeviceModel from pyfibaro.fibaro_device import DeviceModel
@ -80,11 +80,11 @@ class FibaroCover(FibaroDevice, CoverEntity):
def set_cover_position(self, **kwargs: Any) -> None: def set_cover_position(self, **kwargs: Any) -> None:
"""Move the cover to a specific position.""" """Move the cover to a specific position."""
self.set_level(kwargs.get(ATTR_POSITION)) self.set_level(cast(int, kwargs.get(ATTR_POSITION)))
def set_cover_tilt_position(self, **kwargs: Any) -> None: def set_cover_tilt_position(self, **kwargs: Any) -> None:
"""Move the cover to a specific position.""" """Move the cover to a specific position."""
self.set_level2(kwargs.get(ATTR_TILT_POSITION)) self.set_level2(cast(int, kwargs.get(ATTR_TILT_POSITION)))
@property @property
def is_closed(self) -> bool | None: def is_closed(self) -> bool | None: