Add Z-Wave.Me SensorMultilevels types (#68585)

* Added SensorMultilevels types

* Fixed sort and spaces

* co, co2

* PF -> PERCENTAGE

* POWER_FACTOR to PERCENTAGE

* Fix import sorting

* Whitespaces for flake8

* Whitespaces

* Whitespaces

* Fixed entity_description type

Co-authored-by: aivs <aivs@yandex.ru>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
pull/70658/head
Poltorak Serguei 2022-04-25 12:00:33 +03:00 committed by GitHub
parent 1af7c26741
commit 5b9e3a5e7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 68 additions and 28 deletions

View File

@ -1,6 +1,9 @@
"""Representation of a sensorMultilevel.""" """Representation of a sensorMultilevel."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from zwave_me_ws import ZWaveMeData from zwave_me_ws import ZWaveMeData
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
@ -11,11 +14,13 @@ from homeassistant.components.sensor import (
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
ELECTRIC_CURRENT_AMPERE,
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
LIGHT_LUX, LIGHT_LUX,
PERCENTAGE,
POWER_WATT, POWER_WATT,
SIGNAL_STRENGTH_DECIBELS, PRESSURE_KPA,
TEMP_CELSIUS, TEMP_CELSIUS,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@ -25,50 +30,83 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ZWaveMeController, ZWaveMeEntity from . import ZWaveMeController, ZWaveMeEntity
from .const import DOMAIN, ZWaveMePlatform from .const import DOMAIN, ZWaveMePlatform
SENSORS_MAP: dict[str, SensorEntityDescription] = {
"meterElectric_watt": SensorEntityDescription( @dataclass
key="meterElectric_watt", class ZWaveMeSensorEntityDescription(SensorEntityDescription):
device_class=SensorDeviceClass.POWER, """Class describing ZWaveMeSensor sensor entities."""
native_unit_of_measurement=POWER_WATT,
value: Callable = lambda value: value
SENSORS_MAP: dict[str, ZWaveMeSensorEntityDescription] = {
"barometer": ZWaveMeSensorEntityDescription(
key="barometer",
device_class=SensorDeviceClass.PRESSURE,
native_unit_of_measurement=PRESSURE_KPA,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"meterElectric_kilowatt_hour": SensorEntityDescription( "co": ZWaveMeSensorEntityDescription(
key="co",
device_class=SensorDeviceClass.CO,
native_unit_of_measurement="ppm",
state_class=SensorStateClass.MEASUREMENT,
),
"co2": ZWaveMeSensorEntityDescription(
key="co2",
device_class=SensorDeviceClass.CO2,
native_unit_of_measurement="ppm",
state_class=SensorStateClass.MEASUREMENT,
),
"humidity": ZWaveMeSensorEntityDescription(
key="humidity",
device_class=SensorDeviceClass.HUMIDITY,
native_unit_of_measurement=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
"luminosity": ZWaveMeSensorEntityDescription(
key="luminosity",
device_class=SensorDeviceClass.ILLUMINANCE,
native_unit_of_measurement=LIGHT_LUX,
state_class=SensorStateClass.MEASUREMENT,
),
"meterElectric_ampere": ZWaveMeSensorEntityDescription(
key="meterElectric_ampere",
device_class=SensorDeviceClass.CURRENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
state_class=SensorStateClass.MEASUREMENT,
),
"meterElectric_kilowatt_hour": ZWaveMeSensorEntityDescription(
key="meterElectric_kilowatt_hour", key="meterElectric_kilowatt_hour",
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
"meterElectric_voltage": SensorEntityDescription( "meterElectric_power_factor": ZWaveMeSensorEntityDescription(
key="meterElectric_power_factor",
device_class=SensorDeviceClass.POWER_FACTOR,
native_unit_of_measurement=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
value=lambda value: float(value) * 100,
),
"meterElectric_voltage": ZWaveMeSensorEntityDescription(
key="meterElectric_voltage", key="meterElectric_voltage",
device_class=SensorDeviceClass.VOLTAGE, device_class=SensorDeviceClass.VOLTAGE,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"light": SensorEntityDescription( "meterElectric_watt": ZWaveMeSensorEntityDescription(
key="light", key="meterElectric_watt",
device_class=SensorDeviceClass.ILLUMINANCE, device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=LIGHT_LUX, native_unit_of_measurement=POWER_WATT,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"noise": SensorEntityDescription( "temperature": ZWaveMeSensorEntityDescription(
key="noise",
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
state_class=SensorStateClass.MEASUREMENT,
),
"currentTemperature": SensorEntityDescription(
key="currentTemperature",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
),
"temperature": SensorEntityDescription(
key="temperature", key="temperature",
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"generic": SensorEntityDescription( "generic": ZWaveMeSensorEntityDescription(
key="generic", key="generic",
), ),
} }
@ -104,11 +142,13 @@ async def async_setup_entry(
class ZWaveMeSensor(ZWaveMeEntity, SensorEntity): class ZWaveMeSensor(ZWaveMeEntity, SensorEntity):
"""Representation of a ZWaveMe sensor.""" """Representation of a ZWaveMe sensor."""
entity_description: ZWaveMeSensorEntityDescription
def __init__( def __init__(
self, self,
controller: ZWaveMeController, controller: ZWaveMeController,
device: ZWaveMeData, device: ZWaveMeData,
description: SensorEntityDescription, description: ZWaveMeSensorEntityDescription,
) -> None: ) -> None:
"""Initialize the device.""" """Initialize the device."""
super().__init__(controller=controller, device=device) super().__init__(controller=controller, device=device)
@ -117,4 +157,4 @@ class ZWaveMeSensor(ZWaveMeEntity, SensorEntity):
@property @property
def native_value(self) -> str: def native_value(self) -> str:
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self.device.level return self.entity_description.value(self.device.level)