Use enums in shelly (#62035)

This commit is contained in:
Robert Hillis 2021-12-16 08:12:23 -05:00 committed by GitHub
parent db6b472e7a
commit 0cf0104662
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 106 additions and 113 deletions

View File

@ -4,22 +4,13 @@ from __future__ import annotations
from typing import Final, cast from typing import Final, cast
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
DEVICE_CLASS_CONNECTIVITY,
DEVICE_CLASS_GAS,
DEVICE_CLASS_MOISTURE,
DEVICE_CLASS_MOTION,
DEVICE_CLASS_OPENING,
DEVICE_CLASS_POWER,
DEVICE_CLASS_PROBLEM,
DEVICE_CLASS_SMOKE,
DEVICE_CLASS_UPDATE,
DEVICE_CLASS_VIBRATION,
STATE_ON, STATE_ON,
BinarySensorDeviceClass,
BinarySensorEntity, BinarySensorEntity,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ENTITY_CATEGORY_DIAGNOSTIC
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import CONF_SLEEP_PERIOD from .const import CONF_SLEEP_PERIOD
@ -44,69 +35,69 @@ from .utils import (
SENSORS: Final = { SENSORS: Final = {
("device", "overtemp"): BlockAttributeDescription( ("device", "overtemp"): BlockAttributeDescription(
name="Overheating", name="Overheating",
device_class=DEVICE_CLASS_PROBLEM, device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
("device", "overpower"): BlockAttributeDescription( ("device", "overpower"): BlockAttributeDescription(
name="Overpowering", name="Overpowering",
device_class=DEVICE_CLASS_PROBLEM, device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
("light", "overpower"): BlockAttributeDescription( ("light", "overpower"): BlockAttributeDescription(
name="Overpowering", name="Overpowering",
device_class=DEVICE_CLASS_PROBLEM, device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
("relay", "overpower"): BlockAttributeDescription( ("relay", "overpower"): BlockAttributeDescription(
name="Overpowering", name="Overpowering",
device_class=DEVICE_CLASS_PROBLEM, device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
("sensor", "dwIsOpened"): BlockAttributeDescription( ("sensor", "dwIsOpened"): BlockAttributeDescription(
name="Door", name="Door",
device_class=DEVICE_CLASS_OPENING, device_class=BinarySensorDeviceClass.OPENING,
available=lambda block: cast(int, block.dwIsOpened) != -1, available=lambda block: cast(int, block.dwIsOpened) != -1,
), ),
("sensor", "flood"): BlockAttributeDescription( ("sensor", "flood"): BlockAttributeDescription(
name="Flood", device_class=DEVICE_CLASS_MOISTURE name="Flood", device_class=BinarySensorDeviceClass.MOISTURE
), ),
("sensor", "gas"): BlockAttributeDescription( ("sensor", "gas"): BlockAttributeDescription(
name="Gas", name="Gas",
device_class=DEVICE_CLASS_GAS, device_class=BinarySensorDeviceClass.GAS,
value=lambda value: value in ["mild", "heavy"], value=lambda value: value in ["mild", "heavy"],
extra_state_attributes=lambda block: {"detected": block.gas}, extra_state_attributes=lambda block: {"detected": block.gas},
), ),
("sensor", "smoke"): BlockAttributeDescription( ("sensor", "smoke"): BlockAttributeDescription(
name="Smoke", device_class=DEVICE_CLASS_SMOKE name="Smoke", device_class=BinarySensorDeviceClass.SMOKE
), ),
("sensor", "vibration"): BlockAttributeDescription( ("sensor", "vibration"): BlockAttributeDescription(
name="Vibration", device_class=DEVICE_CLASS_VIBRATION name="Vibration", device_class=BinarySensorDeviceClass.VIBRATION
), ),
("input", "input"): BlockAttributeDescription( ("input", "input"): BlockAttributeDescription(
name="Input", name="Input",
device_class=DEVICE_CLASS_POWER, device_class=BinarySensorDeviceClass.POWER,
default_enabled=False, default_enabled=False,
removal_condition=is_block_momentary_input, removal_condition=is_block_momentary_input,
), ),
("relay", "input"): BlockAttributeDescription( ("relay", "input"): BlockAttributeDescription(
name="Input", name="Input",
device_class=DEVICE_CLASS_POWER, device_class=BinarySensorDeviceClass.POWER,
default_enabled=False, default_enabled=False,
removal_condition=is_block_momentary_input, removal_condition=is_block_momentary_input,
), ),
("device", "input"): BlockAttributeDescription( ("device", "input"): BlockAttributeDescription(
name="Input", name="Input",
device_class=DEVICE_CLASS_POWER, device_class=BinarySensorDeviceClass.POWER,
default_enabled=False, default_enabled=False,
removal_condition=is_block_momentary_input, removal_condition=is_block_momentary_input,
), ),
("sensor", "extInput"): BlockAttributeDescription( ("sensor", "extInput"): BlockAttributeDescription(
name="External Input", name="External Input",
device_class=DEVICE_CLASS_POWER, device_class=BinarySensorDeviceClass.POWER,
default_enabled=False, default_enabled=False,
), ),
("sensor", "motion"): BlockAttributeDescription( ("sensor", "motion"): BlockAttributeDescription(
name="Motion", device_class=DEVICE_CLASS_MOTION name="Motion", device_class=BinarySensorDeviceClass.MOTION
), ),
} }
@ -114,13 +105,13 @@ REST_SENSORS: Final = {
"cloud": RestAttributeDescription( "cloud": RestAttributeDescription(
name="Cloud", name="Cloud",
value=lambda status, _: status["cloud"]["connected"], value=lambda status, _: status["cloud"]["connected"],
device_class=DEVICE_CLASS_CONNECTIVITY, device_class=BinarySensorDeviceClass.CONNECTIVITY,
default_enabled=False, default_enabled=False,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"fwupdate": RestAttributeDescription( "fwupdate": RestAttributeDescription(
name="Firmware Update", name="Firmware Update",
device_class=DEVICE_CLASS_UPDATE, device_class=BinarySensorDeviceClass.UPDATE,
value=lambda status, _: status["update"]["has_update"], value=lambda status, _: status["update"]["has_update"],
default_enabled=False, default_enabled=False,
extra_state_attributes=lambda status: { extra_state_attributes=lambda status: {
@ -128,7 +119,7 @@ REST_SENSORS: Final = {
"installed_version": status["update"]["old_version"], "installed_version": status["update"]["old_version"],
"beta_version": status["update"].get("beta_version", ""), "beta_version": status["update"].get("beta_version", ""),
}, },
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
} }
@ -137,7 +128,7 @@ RPC_SENSORS: Final = {
key="input", key="input",
sub_key="state", sub_key="state",
name="Input", name="Input",
device_class=DEVICE_CLASS_POWER, device_class=BinarySensorDeviceClass.POWER,
default_enabled=False, default_enabled=False,
removal_condition=is_rpc_momentary_input, removal_condition=is_rpc_momentary_input,
), ),
@ -145,22 +136,22 @@ RPC_SENSORS: Final = {
key="cloud", key="cloud",
sub_key="connected", sub_key="connected",
name="Cloud", name="Cloud",
device_class=DEVICE_CLASS_CONNECTIVITY, device_class=BinarySensorDeviceClass.CONNECTIVITY,
default_enabled=False, default_enabled=False,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"fwupdate": RpcAttributeDescription( "fwupdate": RpcAttributeDescription(
key="sys", key="sys",
sub_key="available_updates", sub_key="available_updates",
name="Firmware Update", name="Firmware Update",
device_class=DEVICE_CLASS_UPDATE, device_class=BinarySensorDeviceClass.UPDATE,
default_enabled=False, default_enabled=False,
extra_state_attributes=lambda status, shelly: { extra_state_attributes=lambda status, shelly: {
"latest_stable_version": status.get("stable", {"version": ""})["version"], "latest_stable_version": status.get("stable", {"version": ""})["version"],
"installed_version": shelly["ver"], "installed_version": shelly["ver"],
"beta_version": status.get("beta", {"version": ""})["version"], "beta_version": status.get("beta", {"version": ""})["version"],
}, },
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
} }

View File

@ -11,10 +11,9 @@ from homeassistant.components.button import (
ButtonEntityDescription, ButtonEntityDescription,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ENTITY_CATEGORY_CONFIG
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo, EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import slugify from homeassistant.util import slugify
@ -40,7 +39,7 @@ BUTTONS: Final = [
key="ota_update", key="ota_update",
name="OTA Update", name="OTA Update",
device_class=ButtonDeviceClass.UPDATE, device_class=ButtonDeviceClass.UPDATE,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=EntityCategory.CONFIG,
press_action=lambda wrapper: wrapper.async_trigger_ota_update(), press_action=lambda wrapper: wrapper.async_trigger_ota_update(),
), ),
ShellyButtonDescription( ShellyButtonDescription(
@ -48,14 +47,14 @@ BUTTONS: Final = [
name="OTA Update Beta", name="OTA Update Beta",
device_class=ButtonDeviceClass.UPDATE, device_class=ButtonDeviceClass.UPDATE,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=EntityCategory.CONFIG,
press_action=lambda wrapper: wrapper.async_trigger_ota_update(beta=True), press_action=lambda wrapper: wrapper.async_trigger_ota_update(beta=True),
), ),
ShellyButtonDescription( ShellyButtonDescription(
key="reboot", key="reboot",
name="Reboot", name="Reboot",
device_class=ButtonDeviceClass.RESTART, device_class=ButtonDeviceClass.RESTART,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=EntityCategory.CONFIG,
press_action=lambda wrapper: wrapper.device.trigger_reboot(), press_action=lambda wrapper: wrapper.device.trigger_reboot(),
), ),
] ]

View File

@ -7,11 +7,11 @@ from aioshelly.block_device import Block
from homeassistant.components.cover import ( from homeassistant.components.cover import (
ATTR_POSITION, ATTR_POSITION,
DEVICE_CLASS_SHUTTER,
SUPPORT_CLOSE, SUPPORT_CLOSE,
SUPPORT_OPEN, SUPPORT_OPEN,
SUPPORT_SET_POSITION, SUPPORT_SET_POSITION,
SUPPORT_STOP, SUPPORT_STOP,
CoverDeviceClass,
CoverEntity, CoverEntity,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -41,7 +41,7 @@ async def async_setup_entry(
class ShellyCover(ShellyBlockEntity, CoverEntity): class ShellyCover(ShellyBlockEntity, CoverEntity):
"""Switch that controls a cover block on Shelly devices.""" """Switch that controls a cover block on Shelly devices."""
_attr_device_class = DEVICE_CLASS_SHUTTER _attr_device_class = CoverDeviceClass.SHUTTER
def __init__(self, wrapper: BlockDeviceWrapper, block: Block) -> None: def __init__(self, wrapper: BlockDeviceWrapper, block: Block) -> None:
"""Initialize light.""" """Initialize light."""

View File

@ -3,8 +3,11 @@ from __future__ import annotations
from typing import Final, cast from typing import Final, cast
from homeassistant.components import sensor from homeassistant.components.sensor import (
from homeassistant.components.sensor import SensorEntity SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION, CONCENTRATION_PARTS_PER_MILLION,
@ -12,7 +15,6 @@ from homeassistant.const import (
ELECTRIC_CURRENT_AMPERE, ELECTRIC_CURRENT_AMPERE,
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
ENTITY_CATEGORY_DIAGNOSTIC,
LIGHT_LUX, LIGHT_LUX,
PERCENTAGE, PERCENTAGE,
POWER_WATT, POWER_WATT,
@ -20,6 +22,7 @@ from homeassistant.const import (
TEMP_CELSIUS, TEMP_CELSIUS,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
@ -42,163 +45,163 @@ SENSORS: Final = {
("device", "battery"): BlockAttributeDescription( ("device", "battery"): BlockAttributeDescription(
name="Battery", name="Battery",
unit=PERCENTAGE, unit=PERCENTAGE,
device_class=sensor.DEVICE_CLASS_BATTERY, device_class=SensorDeviceClass.BATTERY,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
removal_condition=lambda settings, _: settings.get("external_power") == 1, removal_condition=lambda settings, _: settings.get("external_power") == 1,
available=lambda block: cast(int, block.battery) != -1, available=lambda block: cast(int, block.battery) != -1,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
("device", "deviceTemp"): BlockAttributeDescription( ("device", "deviceTemp"): BlockAttributeDescription(
name="Device Temperature", name="Device Temperature",
unit=temperature_unit, unit=temperature_unit,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
default_enabled=False, default_enabled=False,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
("emeter", "current"): BlockAttributeDescription( ("emeter", "current"): BlockAttributeDescription(
name="Current", name="Current",
unit=ELECTRIC_CURRENT_AMPERE, unit=ELECTRIC_CURRENT_AMPERE,
value=lambda value: value, value=lambda value: value,
device_class=sensor.DEVICE_CLASS_CURRENT, device_class=SensorDeviceClass.CURRENT,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
("light", "power"): BlockAttributeDescription( ("light", "power"): BlockAttributeDescription(
name="Power", name="Power",
unit=POWER_WATT, unit=POWER_WATT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_POWER, device_class=SensorDeviceClass.POWER,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
default_enabled=False, default_enabled=False,
), ),
("device", "power"): BlockAttributeDescription( ("device", "power"): BlockAttributeDescription(
name="Power", name="Power",
unit=POWER_WATT, unit=POWER_WATT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_POWER, device_class=SensorDeviceClass.POWER,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
("emeter", "power"): BlockAttributeDescription( ("emeter", "power"): BlockAttributeDescription(
name="Power", name="Power",
unit=POWER_WATT, unit=POWER_WATT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_POWER, device_class=SensorDeviceClass.POWER,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
("device", "voltage"): BlockAttributeDescription( ("device", "voltage"): BlockAttributeDescription(
name="Voltage", name="Voltage",
unit=ELECTRIC_POTENTIAL_VOLT, unit=ELECTRIC_POTENTIAL_VOLT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_VOLTAGE, device_class=SensorDeviceClass.VOLTAGE,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
default_enabled=False, default_enabled=False,
), ),
("emeter", "voltage"): BlockAttributeDescription( ("emeter", "voltage"): BlockAttributeDescription(
name="Voltage", name="Voltage",
unit=ELECTRIC_POTENTIAL_VOLT, unit=ELECTRIC_POTENTIAL_VOLT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_VOLTAGE, device_class=SensorDeviceClass.VOLTAGE,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
("emeter", "powerFactor"): BlockAttributeDescription( ("emeter", "powerFactor"): BlockAttributeDescription(
name="Power Factor", name="Power Factor",
unit=PERCENTAGE, unit=PERCENTAGE,
value=lambda value: round(value * 100, 1), value=lambda value: round(value * 100, 1),
device_class=sensor.DEVICE_CLASS_POWER_FACTOR, device_class=SensorDeviceClass.POWER_FACTOR,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
("relay", "power"): BlockAttributeDescription( ("relay", "power"): BlockAttributeDescription(
name="Power", name="Power",
unit=POWER_WATT, unit=POWER_WATT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_POWER, device_class=SensorDeviceClass.POWER,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
("roller", "rollerPower"): BlockAttributeDescription( ("roller", "rollerPower"): BlockAttributeDescription(
name="Power", name="Power",
unit=POWER_WATT, unit=POWER_WATT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_POWER, device_class=SensorDeviceClass.POWER,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
("device", "energy"): BlockAttributeDescription( ("device", "energy"): BlockAttributeDescription(
name="Energy", name="Energy",
unit=ENERGY_KILO_WATT_HOUR, unit=ENERGY_KILO_WATT_HOUR,
value=lambda value: round(value / 60 / 1000, 2), value=lambda value: round(value / 60 / 1000, 2),
device_class=sensor.DEVICE_CLASS_ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=sensor.STATE_CLASS_TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
("emeter", "energy"): BlockAttributeDescription( ("emeter", "energy"): BlockAttributeDescription(
name="Energy", name="Energy",
unit=ENERGY_KILO_WATT_HOUR, unit=ENERGY_KILO_WATT_HOUR,
value=lambda value: round(value / 1000, 2), value=lambda value: round(value / 1000, 2),
device_class=sensor.DEVICE_CLASS_ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=sensor.STATE_CLASS_TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
("emeter", "energyReturned"): BlockAttributeDescription( ("emeter", "energyReturned"): BlockAttributeDescription(
name="Energy Returned", name="Energy Returned",
unit=ENERGY_KILO_WATT_HOUR, unit=ENERGY_KILO_WATT_HOUR,
value=lambda value: round(value / 1000, 2), value=lambda value: round(value / 1000, 2),
device_class=sensor.DEVICE_CLASS_ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=sensor.STATE_CLASS_TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
("light", "energy"): BlockAttributeDescription( ("light", "energy"): BlockAttributeDescription(
name="Energy", name="Energy",
unit=ENERGY_KILO_WATT_HOUR, unit=ENERGY_KILO_WATT_HOUR,
value=lambda value: round(value / 60 / 1000, 2), value=lambda value: round(value / 60 / 1000, 2),
device_class=sensor.DEVICE_CLASS_ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=sensor.STATE_CLASS_TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
default_enabled=False, default_enabled=False,
), ),
("relay", "energy"): BlockAttributeDescription( ("relay", "energy"): BlockAttributeDescription(
name="Energy", name="Energy",
unit=ENERGY_KILO_WATT_HOUR, unit=ENERGY_KILO_WATT_HOUR,
value=lambda value: round(value / 60 / 1000, 2), value=lambda value: round(value / 60 / 1000, 2),
device_class=sensor.DEVICE_CLASS_ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=sensor.STATE_CLASS_TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
("roller", "rollerEnergy"): BlockAttributeDescription( ("roller", "rollerEnergy"): BlockAttributeDescription(
name="Energy", name="Energy",
unit=ENERGY_KILO_WATT_HOUR, unit=ENERGY_KILO_WATT_HOUR,
value=lambda value: round(value / 60 / 1000, 2), value=lambda value: round(value / 60 / 1000, 2),
device_class=sensor.DEVICE_CLASS_ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=sensor.STATE_CLASS_TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
("sensor", "concentration"): BlockAttributeDescription( ("sensor", "concentration"): BlockAttributeDescription(
name="Gas Concentration", name="Gas Concentration",
unit=CONCENTRATION_PARTS_PER_MILLION, unit=CONCENTRATION_PARTS_PER_MILLION,
icon="mdi:gauge", icon="mdi:gauge",
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
("sensor", "extTemp"): BlockAttributeDescription( ("sensor", "extTemp"): BlockAttributeDescription(
name="Temperature", name="Temperature",
unit=temperature_unit, unit=temperature_unit,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
available=lambda block: cast(int, block.extTemp) != 999, available=lambda block: cast(int, block.extTemp) != 999,
), ),
("sensor", "humidity"): BlockAttributeDescription( ("sensor", "humidity"): BlockAttributeDescription(
name="Humidity", name="Humidity",
unit=PERCENTAGE, unit=PERCENTAGE,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_HUMIDITY, device_class=SensorDeviceClass.HUMIDITY,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
available=lambda block: cast(int, block.humidity) != 999, available=lambda block: cast(int, block.humidity) != 999,
), ),
("sensor", "luminosity"): BlockAttributeDescription( ("sensor", "luminosity"): BlockAttributeDescription(
name="Luminosity", name="Luminosity",
unit=LIGHT_LUX, unit=LIGHT_LUX,
device_class=sensor.DEVICE_CLASS_ILLUMINANCE, device_class=SensorDeviceClass.ILLUMINANCE,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
available=lambda block: cast(int, block.luminosity) != -1, available=lambda block: cast(int, block.luminosity) != -1,
), ),
("sensor", "tilt"): BlockAttributeDescription( ("sensor", "tilt"): BlockAttributeDescription(
name="Tilt", name="Tilt",
unit=DEGREE, unit=DEGREE,
icon="mdi:angle-acute", icon="mdi:angle-acute",
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
("relay", "totalWorkTime"): BlockAttributeDescription( ("relay", "totalWorkTime"): BlockAttributeDescription(
name="Lamp Life", name="Lamp Life",
@ -208,14 +211,14 @@ SENSORS: Final = {
extra_state_attributes=lambda block: { extra_state_attributes=lambda block: {
"Operational hours": round(cast(int, block.totalWorkTime) / 3600, 1) "Operational hours": round(cast(int, block.totalWorkTime) / 3600, 1)
}, },
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
("adc", "adc"): BlockAttributeDescription( ("adc", "adc"): BlockAttributeDescription(
name="ADC", name="ADC",
unit=ELECTRIC_POTENTIAL_VOLT, unit=ELECTRIC_POTENTIAL_VOLT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_VOLTAGE, device_class=SensorDeviceClass.VOLTAGE,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
("sensor", "sensorOp"): BlockAttributeDescription( ("sensor", "sensorOp"): BlockAttributeDescription(
name="Operation", name="Operation",
@ -230,17 +233,17 @@ REST_SENSORS: Final = {
name="RSSI", name="RSSI",
unit=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, unit=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
value=lambda status, _: status["wifi_sta"]["rssi"], value=lambda status, _: status["wifi_sta"]["rssi"],
device_class=sensor.DEVICE_CLASS_SIGNAL_STRENGTH, device_class=SensorDeviceClass.SIGNAL_STRENGTH,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
default_enabled=False, default_enabled=False,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"uptime": RestAttributeDescription( "uptime": RestAttributeDescription(
name="Uptime", name="Uptime",
value=lambda status, last: get_device_uptime(status["uptime"], last), value=lambda status, last: get_device_uptime(status["uptime"], last),
device_class=sensor.DEVICE_CLASS_TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
default_enabled=False, default_enabled=False,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
} }
@ -252,8 +255,8 @@ RPC_SENSORS: Final = {
name="Power", name="Power",
unit=POWER_WATT, unit=POWER_WATT,
value=lambda status, _: round(float(status), 1), value=lambda status, _: round(float(status), 1),
device_class=sensor.DEVICE_CLASS_POWER, device_class=SensorDeviceClass.POWER,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"voltage": RpcAttributeDescription( "voltage": RpcAttributeDescription(
key="switch", key="switch",
@ -261,8 +264,8 @@ RPC_SENSORS: Final = {
name="Voltage", name="Voltage",
unit=ELECTRIC_POTENTIAL_VOLT, unit=ELECTRIC_POTENTIAL_VOLT,
value=lambda status, _: round(float(status), 1), value=lambda status, _: round(float(status), 1),
device_class=sensor.DEVICE_CLASS_VOLTAGE, device_class=SensorDeviceClass.VOLTAGE,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
default_enabled=False, default_enabled=False,
), ),
"energy": RpcAttributeDescription( "energy": RpcAttributeDescription(
@ -271,8 +274,8 @@ RPC_SENSORS: Final = {
name="Energy", name="Energy",
unit=ENERGY_KILO_WATT_HOUR, unit=ENERGY_KILO_WATT_HOUR,
value=lambda status, _: round(status["total"] / 1000, 2), value=lambda status, _: round(status["total"] / 1000, 2),
device_class=sensor.DEVICE_CLASS_ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=sensor.STATE_CLASS_TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
"temperature": RpcAttributeDescription( "temperature": RpcAttributeDescription(
key="switch", key="switch",
@ -280,8 +283,8 @@ RPC_SENSORS: Final = {
name="Temperature", name="Temperature",
unit=TEMP_CELSIUS, unit=TEMP_CELSIUS,
value=lambda status, _: round(status["tC"], 1), value=lambda status, _: round(status["tC"], 1),
device_class=sensor.DEVICE_CLASS_TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
default_enabled=False, default_enabled=False,
), ),
"rssi": RpcAttributeDescription( "rssi": RpcAttributeDescription(
@ -289,19 +292,19 @@ RPC_SENSORS: Final = {
sub_key="rssi", sub_key="rssi",
name="RSSI", name="RSSI",
unit=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, unit=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
device_class=sensor.DEVICE_CLASS_SIGNAL_STRENGTH, device_class=SensorDeviceClass.SIGNAL_STRENGTH,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
default_enabled=False, default_enabled=False,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"uptime": RpcAttributeDescription( "uptime": RpcAttributeDescription(
key="sys", key="sys",
sub_key="uptime", sub_key="uptime",
name="Uptime", name="Uptime",
value=get_device_uptime, value=get_device_uptime,
device_class=sensor.DEVICE_CLASS_TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
default_enabled=False, default_enabled=False,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
} }