Address feedback to Overkiz integration (#62841)

This commit is contained in:
Mick Vleeshouwer 2021-12-27 09:26:55 -08:00 committed by GitHub
parent 8fd60dbd51
commit 089dcb2b22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 45 deletions

View file

@ -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