From 089dcb2b222f6a653b1bcb31d62504dcc56d916b Mon Sep 17 00:00:00 2001 From: Mick Vleeshouwer Date: Mon, 27 Dec 2021 09:26:55 -0800 Subject: [PATCH] Address feedback to Overkiz integration (#62841) --- homeassistant/components/overkiz/button.py | 22 +++++----- homeassistant/components/overkiz/entity.py | 13 ------ homeassistant/components/overkiz/lock.py | 10 ++--- homeassistant/components/overkiz/sensor.py | 48 ++++++++++++++-------- 4 files changed, 48 insertions(+), 45 deletions(-) diff --git a/homeassistant/components/overkiz/button.py b/homeassistant/components/overkiz/button.py index 85e7ea8c613..7a2c0b0448c 100644 --- a/homeassistant/components/overkiz/button.py +++ b/homeassistant/components/overkiz/button.py @@ -57,18 +57,20 @@ async def async_setup_entry( for device in data.coordinator.data.values(): if ( - device.widget not in IGNORED_OVERKIZ_DEVICES - and device.ui_class not in IGNORED_OVERKIZ_DEVICES + device.widget in IGNORED_OVERKIZ_DEVICES + or device.ui_class in IGNORED_OVERKIZ_DEVICES ): - for command in device.definition.commands: - if description := supported_commands.get(command.command_name): - entities.append( - OverkizButton( - device.device_url, - data.coordinator, - description, - ) + continue + + for command in device.definition.commands: + if description := supported_commands.get(command.command_name): + entities.append( + OverkizButton( + device.device_url, + data.coordinator, + description, ) + ) async_add_entities(entities) diff --git a/homeassistant/components/overkiz/entity.py b/homeassistant/components/overkiz/entity.py index b88417b0d2b..13f9fc781a7 100644 --- a/homeassistant/components/overkiz/entity.py +++ b/homeassistant/components/overkiz/entity.py @@ -1,13 +1,9 @@ """Parent class for every Overkiz device.""" from __future__ import annotations -from collections.abc import Callable -from dataclasses import dataclass - from pyoverkiz.enums import OverkizAttribute, OverkizState from pyoverkiz.models import Device -from homeassistant.components.sensor import SensorEntityDescription from homeassistant.helpers.entity import DeviceInfo, EntityDescription from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -83,15 +79,6 @@ class OverkizEntity(CoordinatorEntity): ) -@dataclass -class OverkizSensorDescription(SensorEntityDescription): - """Class to describe an Overkiz sensor.""" - - native_value: Callable[ - [str | int | float], str | int | float - ] | None = lambda val: val - - class OverkizDescriptiveEntity(OverkizEntity): """Representation of a Overkiz device entity based on a description.""" diff --git a/homeassistant/components/overkiz/lock.py b/homeassistant/components/overkiz/lock.py index 89a091751d9..c900b2faab8 100644 --- a/homeassistant/components/overkiz/lock.py +++ b/homeassistant/components/overkiz/lock.py @@ -24,22 +24,20 @@ async def async_setup_entry( """Set up the Overkiz locks from a config entry.""" data: HomeAssistantOverkizData = hass.data[DOMAIN][entry.entry_id] - entities: list[OverkizLock] = [ + async_add_entities( OverkizLock(device.device_url, data.coordinator) for device in data.platforms[Platform.LOCK] - ] - - async_add_entities(entities) + ) class OverkizLock(OverkizEntity, LockEntity): """Representation of an Overkiz Lock.""" - async def async_lock(self, **_: Any) -> None: + async def async_lock(self, **kwargs: Any) -> None: """Lock method.""" await self.executor.async_execute_command(OverkizCommand.LOCK) - async def async_unlock(self, **_: Any) -> None: + async def async_unlock(self, **kwargs: Any) -> None: """Unlock method.""" await self.executor.async_execute_command(OverkizCommand.UNLOCK) diff --git a/homeassistant/components/overkiz/sensor.py b/homeassistant/components/overkiz/sensor.py index a00587328b8..eb35e8b8208 100644 --- a/homeassistant/components/overkiz/sensor.py +++ b/homeassistant/components/overkiz/sensor.py @@ -1,11 +1,15 @@ """Support for Overkiz sensors.""" from __future__ import annotations +from collections.abc import Callable +from dataclasses import dataclass + from pyoverkiz.enums import OverkizAttribute, OverkizState, UIWidget from homeassistant.components.sensor import ( SensorDeviceClass, SensorEntity, + SensorEntityDescription, SensorStateClass, ) from homeassistant.config_entries import ConfigEntry @@ -28,7 +32,15 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import HomeAssistantOverkizData from .const import DOMAIN, IGNORED_OVERKIZ_DEVICES from .coordinator import OverkizDataUpdateCoordinator -from .entity import OverkizDescriptiveEntity, OverkizEntity, OverkizSensorDescription +from .entity import OverkizDescriptiveEntity, OverkizEntity + + +@dataclass +class OverkizSensorDescription(SensorEntityDescription): + """Class to describe an Overkiz sensor.""" + + native_value: Callable[[str | int | float], str | int | float] | None = None + SENSOR_DESCRIPTIONS: list[OverkizSensorDescription] = [ OverkizSensorDescription( @@ -347,20 +359,6 @@ async def async_setup_entry( } for device in data.coordinator.data.values(): - if ( - device.widget not in IGNORED_OVERKIZ_DEVICES - and device.ui_class not in IGNORED_OVERKIZ_DEVICES - ): - for state in device.definition.states: - if description := key_supported_states.get(state.qualified_name): - entities.append( - OverkizStateSensor( - device.device_url, - data.coordinator, - description, - ) - ) - if device.widget == UIWidget.HOMEKIT_STACK: entities.append( OverkizHomeKitSetupCodeSensor( @@ -369,12 +367,30 @@ async def async_setup_entry( ) ) + if ( + device.widget in IGNORED_OVERKIZ_DEVICES + or device.ui_class in IGNORED_OVERKIZ_DEVICES + ): + continue + + for state in device.definition.states: + if description := key_supported_states.get(state.qualified_name): + entities.append( + OverkizStateSensor( + device.device_url, + data.coordinator, + description, + ) + ) + async_add_entities(entities) class OverkizStateSensor(OverkizDescriptiveEntity, SensorEntity): """Representation of an Overkiz Sensor.""" + entity_description: OverkizSensorDescription + @property def native_value(self): """Return the value of the sensor.""" @@ -384,7 +400,7 @@ class OverkizStateSensor(OverkizDescriptiveEntity, SensorEntity): return None # Transform the value with a lambda function - if hasattr(self.entity_description, "native_value"): + if self.entity_description.native_value: return self.entity_description.native_value(state.value) return state.value