From aeaa1f84c06f5ff027fe51439e973195e469a0aa Mon Sep 17 00:00:00 2001 From: rappenze Date: Mon, 22 Apr 2024 09:29:10 +0200 Subject: [PATCH] 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 --- homeassistant/components/fibaro/__init__.py | 29 ++++++++++--------- .../components/fibaro/binary_sensor.py | 4 +-- homeassistant/components/fibaro/cover.py | 6 ++-- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/fibaro/__init__.py b/homeassistant/components/fibaro/__init__.py index 2c1405130b4..4c1feb27629 100644 --- a/homeassistant/components/fibaro/__init__.py +++ b/homeassistant/components/fibaro/__init__.py @@ -454,37 +454,38 @@ class FibaroDevice(Entity): if not fibaro_device.visible: 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.""" self.controller.register(self.fibaro_device.fibaro_id, self._update_callback) - def _update_callback(self): + def _update_callback(self) -> None: """Update the state.""" self.schedule_update_ha_state(True) @property - def level(self): + def level(self) -> int | None: """Get the level of Fibaro device.""" if self.fibaro_device.value.has_value: return self.fibaro_device.value.int_value() return None @property - def level2(self): + def level2(self) -> int | None: """Get the tilt level of Fibaro device.""" if self.fibaro_device.value_2.has_value: return self.fibaro_device.value_2.int_value() 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.""" _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.fibaro_device.actions), ) - def set_level(self, level): + def set_level(self, level: int) -> None: """Set the level of Fibaro device.""" self.action("setValue", level) if self.fibaro_device.value.has_value: @@ -492,21 +493,21 @@ class FibaroDevice(Entity): if self.fibaro_device.has_brightness: self.fibaro_device.properties["brightness"] = level - def set_level2(self, level): + def set_level2(self, level: int) -> None: """Set the level2 of Fibaro device.""" self.action("setValue2", level) if self.fibaro_device.value_2.has_value: self.fibaro_device.properties["value2"] = level - def call_turn_on(self): + def call_turn_on(self) -> None: """Turn on the Fibaro device.""" self.action("turnOn") - def call_turn_off(self): + def call_turn_off(self) -> None: """Turn off the Fibaro device.""" 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.""" red = int(max(0, min(255, red))) green = int(max(0, min(255, green))) @@ -516,7 +517,7 @@ class FibaroDevice(Entity): self.fibaro_device.properties["color"] = color_str 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.""" if cmd in self.fibaro_device.actions: self.fibaro_device.execute_action(cmd, args) @@ -525,12 +526,12 @@ class FibaroDevice(Entity): self.dont_know_message(cmd) @property - def current_binary_state(self): + def current_binary_state(self) -> bool: """Return the current binary state.""" return self.fibaro_device.value.bool_value(False) @property - def extra_state_attributes(self): + def extra_state_attributes(self) -> Mapping[str, Any]: """Return the state attributes of the device.""" attr = {"fibaro_id": self.fibaro_device.fibaro_id} diff --git a/homeassistant/components/fibaro/binary_sensor.py b/homeassistant/components/fibaro/binary_sensor.py index c0980025555..3c965c11b34 100644 --- a/homeassistant/components/fibaro/binary_sensor.py +++ b/homeassistant/components/fibaro/binary_sensor.py @@ -76,9 +76,9 @@ class FibaroBinarySensor(FibaroDevice, BinarySensorEntity): self._attr_icon = SENSOR_TYPES[self._fibaro_sensor_type][1] @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 super().extra_state_attributes | self._own_extra_state_attributes + return {**super().extra_state_attributes, **self._own_extra_state_attributes} def update(self) -> None: """Get the latest data and update the state.""" diff --git a/homeassistant/components/fibaro/cover.py b/homeassistant/components/fibaro/cover.py index 16be6e98ae1..e71ae8982e7 100644 --- a/homeassistant/components/fibaro/cover.py +++ b/homeassistant/components/fibaro/cover.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import Any +from typing import Any, cast from pyfibaro.fibaro_device import DeviceModel @@ -80,11 +80,11 @@ class FibaroCover(FibaroDevice, CoverEntity): def set_cover_position(self, **kwargs: Any) -> None: """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: """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 def is_closed(self) -> bool | None: