2020-08-28 15:33:34 +00:00
|
|
|
"""Sensor for Shelly."""
|
2021-07-21 17:11:44 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Final, cast
|
|
|
|
|
2020-08-28 15:33:34 +00:00
|
|
|
from homeassistant.components import sensor
|
2021-03-22 18:54:14 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2021-07-21 17:11:44 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-08-30 12:18:35 +00:00
|
|
|
from homeassistant.const import (
|
2020-09-03 08:54:25 +00:00
|
|
|
CONCENTRATION_PARTS_PER_MILLION,
|
2020-09-04 14:42:57 +00:00
|
|
|
DEGREE,
|
2021-07-20 18:06:23 +00:00
|
|
|
ELECTRIC_CURRENT_AMPERE,
|
|
|
|
ELECTRIC_POTENTIAL_VOLT,
|
2020-08-30 12:18:35 +00:00
|
|
|
ENERGY_KILO_WATT_HOUR,
|
2020-09-23 18:48:01 +00:00
|
|
|
LIGHT_LUX,
|
2020-09-05 19:09:14 +00:00
|
|
|
PERCENTAGE,
|
2020-08-30 12:18:35 +00:00
|
|
|
POWER_WATT,
|
2021-04-15 17:52:06 +00:00
|
|
|
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
2020-08-30 12:18:35 +00:00
|
|
|
)
|
2021-07-21 17:11:44 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import StateType
|
2020-08-28 15:33:34 +00:00
|
|
|
|
2021-08-18 11:13:35 +00:00
|
|
|
from .const import SHAIR_MAX_WORK_HOURS
|
2020-09-07 12:13:20 +00:00
|
|
|
from .entity import (
|
|
|
|
BlockAttributeDescription,
|
2020-11-11 19:13:14 +00:00
|
|
|
RestAttributeDescription,
|
2020-09-07 12:13:20 +00:00
|
|
|
ShellyBlockAttributeEntity,
|
2020-11-11 19:13:14 +00:00
|
|
|
ShellyRestAttributeEntity,
|
2021-02-03 16:03:22 +00:00
|
|
|
ShellySleepingBlockAttributeEntity,
|
2020-09-07 12:13:20 +00:00
|
|
|
async_setup_entry_attribute_entities,
|
2020-11-11 19:13:14 +00:00
|
|
|
async_setup_entry_rest,
|
2020-09-07 12:13:20 +00:00
|
|
|
)
|
2020-11-27 08:40:06 +00:00
|
|
|
from .utils import get_device_uptime, temperature_unit
|
2020-11-16 10:49:23 +00:00
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
SENSORS: Final = {
|
2020-09-07 12:13:20 +00:00
|
|
|
("device", "battery"): BlockAttributeDescription(
|
2020-11-19 10:42:24 +00:00
|
|
|
name="Battery",
|
|
|
|
unit=PERCENTAGE,
|
|
|
|
device_class=sensor.DEVICE_CLASS_BATTERY,
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2020-11-19 10:42:24 +00:00
|
|
|
removal_condition=lambda settings, _: settings.get("external_power") == 1,
|
2021-08-29 12:52:12 +00:00
|
|
|
available=lambda block: cast(bool, block.battery != -1),
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
|
|
|
("device", "deviceTemp"): BlockAttributeDescription(
|
|
|
|
name="Device Temperature",
|
|
|
|
unit=temperature_unit,
|
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_TEMPERATURE,
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2020-09-07 12:13:20 +00:00
|
|
|
default_enabled=False,
|
|
|
|
),
|
|
|
|
("emeter", "current"): BlockAttributeDescription(
|
|
|
|
name="Current",
|
2021-07-20 18:06:23 +00:00
|
|
|
unit=ELECTRIC_CURRENT_AMPERE,
|
2020-09-07 12:13:20 +00:00
|
|
|
value=lambda value: value,
|
|
|
|
device_class=sensor.DEVICE_CLASS_CURRENT,
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
|
|
|
("light", "power"): BlockAttributeDescription(
|
|
|
|
name="Power",
|
|
|
|
unit=POWER_WATT,
|
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_POWER,
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2020-09-07 12:13:20 +00:00
|
|
|
default_enabled=False,
|
|
|
|
),
|
2020-09-08 21:22:44 +00:00
|
|
|
("device", "power"): BlockAttributeDescription(
|
|
|
|
name="Power",
|
|
|
|
unit=POWER_WATT,
|
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_POWER,
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2020-09-08 21:22:44 +00:00
|
|
|
),
|
|
|
|
("emeter", "power"): BlockAttributeDescription(
|
|
|
|
name="Power",
|
|
|
|
unit=POWER_WATT,
|
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_POWER,
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2020-09-08 21:22:44 +00:00
|
|
|
),
|
2020-10-01 10:08:03 +00:00
|
|
|
("emeter", "voltage"): BlockAttributeDescription(
|
|
|
|
name="Voltage",
|
2021-07-20 18:06:23 +00:00
|
|
|
unit=ELECTRIC_POTENTIAL_VOLT,
|
2020-10-01 10:08:03 +00:00
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_VOLTAGE,
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2020-10-01 10:08:03 +00:00
|
|
|
),
|
|
|
|
("emeter", "powerFactor"): BlockAttributeDescription(
|
|
|
|
name="Power Factor",
|
|
|
|
unit=PERCENTAGE,
|
|
|
|
value=lambda value: round(value * 100, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_POWER_FACTOR,
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2020-10-01 10:08:03 +00:00
|
|
|
),
|
2020-09-08 21:22:44 +00:00
|
|
|
("relay", "power"): BlockAttributeDescription(
|
|
|
|
name="Power",
|
|
|
|
unit=POWER_WATT,
|
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_POWER,
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2020-09-08 21:22:44 +00:00
|
|
|
),
|
2020-09-12 20:31:01 +00:00
|
|
|
("roller", "rollerPower"): BlockAttributeDescription(
|
|
|
|
name="Power",
|
|
|
|
unit=POWER_WATT,
|
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_POWER,
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2020-09-12 20:31:01 +00:00
|
|
|
),
|
2020-09-08 21:22:44 +00:00
|
|
|
("device", "energy"): BlockAttributeDescription(
|
|
|
|
name="Energy",
|
|
|
|
unit=ENERGY_KILO_WATT_HOUR,
|
|
|
|
value=lambda value: round(value / 60 / 1000, 2),
|
|
|
|
device_class=sensor.DEVICE_CLASS_ENERGY,
|
2021-08-18 11:13:35 +00:00
|
|
|
state_class=sensor.STATE_CLASS_TOTAL_INCREASING,
|
2020-09-08 21:22:44 +00:00
|
|
|
),
|
|
|
|
("emeter", "energy"): BlockAttributeDescription(
|
|
|
|
name="Energy",
|
|
|
|
unit=ENERGY_KILO_WATT_HOUR,
|
|
|
|
value=lambda value: round(value / 1000, 2),
|
|
|
|
device_class=sensor.DEVICE_CLASS_ENERGY,
|
2021-08-18 11:13:35 +00:00
|
|
|
state_class=sensor.STATE_CLASS_TOTAL_INCREASING,
|
2020-09-08 21:22:44 +00:00
|
|
|
),
|
2020-10-01 10:08:03 +00:00
|
|
|
("emeter", "energyReturned"): BlockAttributeDescription(
|
|
|
|
name="Energy Returned",
|
|
|
|
unit=ENERGY_KILO_WATT_HOUR,
|
|
|
|
value=lambda value: round(value / 1000, 2),
|
|
|
|
device_class=sensor.DEVICE_CLASS_ENERGY,
|
2021-08-18 11:13:35 +00:00
|
|
|
state_class=sensor.STATE_CLASS_TOTAL_INCREASING,
|
2020-10-01 10:08:03 +00:00
|
|
|
),
|
2020-09-08 21:22:44 +00:00
|
|
|
("light", "energy"): BlockAttributeDescription(
|
|
|
|
name="Energy",
|
|
|
|
unit=ENERGY_KILO_WATT_HOUR,
|
|
|
|
value=lambda value: round(value / 60 / 1000, 2),
|
|
|
|
device_class=sensor.DEVICE_CLASS_ENERGY,
|
2021-08-18 11:13:35 +00:00
|
|
|
state_class=sensor.STATE_CLASS_TOTAL_INCREASING,
|
2020-09-08 21:22:44 +00:00
|
|
|
default_enabled=False,
|
|
|
|
),
|
2020-09-07 12:13:20 +00:00
|
|
|
("relay", "energy"): BlockAttributeDescription(
|
|
|
|
name="Energy",
|
|
|
|
unit=ENERGY_KILO_WATT_HOUR,
|
|
|
|
value=lambda value: round(value / 60 / 1000, 2),
|
|
|
|
device_class=sensor.DEVICE_CLASS_ENERGY,
|
2021-08-18 11:13:35 +00:00
|
|
|
state_class=sensor.STATE_CLASS_TOTAL_INCREASING,
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
2020-09-12 20:31:01 +00:00
|
|
|
("roller", "rollerEnergy"): BlockAttributeDescription(
|
|
|
|
name="Energy",
|
|
|
|
unit=ENERGY_KILO_WATT_HOUR,
|
|
|
|
value=lambda value: round(value / 60 / 1000, 2),
|
|
|
|
device_class=sensor.DEVICE_CLASS_ENERGY,
|
2021-08-18 11:13:35 +00:00
|
|
|
state_class=sensor.STATE_CLASS_TOTAL_INCREASING,
|
2020-09-12 20:31:01 +00:00
|
|
|
),
|
2020-09-07 12:13:20 +00:00
|
|
|
("sensor", "concentration"): BlockAttributeDescription(
|
|
|
|
name="Gas Concentration",
|
|
|
|
unit=CONCENTRATION_PARTS_PER_MILLION,
|
2020-11-12 18:49:06 +00:00
|
|
|
icon="mdi:gauge",
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
|
|
|
("sensor", "extTemp"): BlockAttributeDescription(
|
2021-03-09 12:45:58 +00:00
|
|
|
name="Temperature",
|
2020-09-07 12:13:20 +00:00
|
|
|
unit=temperature_unit,
|
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_TEMPERATURE,
|
2021-05-21 09:44:34 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2021-07-21 17:11:44 +00:00
|
|
|
available=lambda block: cast(bool, block.extTemp != 999),
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
|
|
|
("sensor", "humidity"): BlockAttributeDescription(
|
|
|
|
name="Humidity",
|
|
|
|
unit=PERCENTAGE,
|
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_HUMIDITY,
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2021-07-21 17:11:44 +00:00
|
|
|
available=lambda block: cast(bool, block.extTemp != 999),
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
|
|
|
("sensor", "luminosity"): BlockAttributeDescription(
|
|
|
|
name="Luminosity",
|
2020-09-23 18:48:01 +00:00
|
|
|
unit=LIGHT_LUX,
|
2020-09-07 12:13:20 +00:00
|
|
|
device_class=sensor.DEVICE_CLASS_ILLUMINANCE,
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2021-08-29 12:52:12 +00:00
|
|
|
available=lambda block: cast(bool, block.luminosity != -1),
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
2021-02-24 20:18:14 +00:00
|
|
|
("sensor", "tilt"): BlockAttributeDescription(
|
|
|
|
name="Tilt",
|
|
|
|
unit=DEGREE,
|
|
|
|
icon="mdi:angle-acute",
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2021-02-24 20:18:14 +00:00
|
|
|
),
|
2020-11-12 09:38:53 +00:00
|
|
|
("relay", "totalWorkTime"): BlockAttributeDescription(
|
2021-02-25 08:51:18 +00:00
|
|
|
name="Lamp Life",
|
2020-11-12 09:38:53 +00:00
|
|
|
unit=PERCENTAGE,
|
|
|
|
icon="mdi:progress-wrench",
|
|
|
|
value=lambda value: round(100 - (value / 3600 / SHAIR_MAX_WORK_HOURS), 1),
|
2021-03-11 20:23:20 +00:00
|
|
|
extra_state_attributes=lambda block: {
|
2020-11-12 09:38:53 +00:00
|
|
|
"Operational hours": round(block.totalWorkTime / 3600, 1)
|
|
|
|
},
|
|
|
|
),
|
2020-11-25 14:37:07 +00:00
|
|
|
("adc", "adc"): BlockAttributeDescription(
|
|
|
|
name="ADC",
|
2021-07-20 18:06:23 +00:00
|
|
|
unit=ELECTRIC_POTENTIAL_VOLT,
|
2020-11-25 14:37:07 +00:00
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_VOLTAGE,
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2020-11-25 14:37:07 +00:00
|
|
|
),
|
2021-03-31 16:46:39 +00:00
|
|
|
("sensor", "sensorOp"): BlockAttributeDescription(
|
|
|
|
name="Operation",
|
|
|
|
icon="mdi:cog-transfer",
|
|
|
|
value=lambda value: value,
|
|
|
|
extra_state_attributes=lambda block: {"self_test": block.selfTest},
|
|
|
|
),
|
2020-08-28 15:33:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
REST_SENSORS: Final = {
|
2020-11-11 19:13:14 +00:00
|
|
|
"rssi": RestAttributeDescription(
|
|
|
|
name="RSSI",
|
2021-04-15 17:52:06 +00:00
|
|
|
unit=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
2020-11-27 08:40:06 +00:00
|
|
|
value=lambda status, _: status["wifi_sta"]["rssi"],
|
2020-11-11 19:13:14 +00:00
|
|
|
device_class=sensor.DEVICE_CLASS_SIGNAL_STRENGTH,
|
2021-05-23 20:10:22 +00:00
|
|
|
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
2020-11-11 19:13:14 +00:00
|
|
|
default_enabled=False,
|
|
|
|
),
|
|
|
|
"uptime": RestAttributeDescription(
|
|
|
|
name="Uptime",
|
2020-11-27 08:40:06 +00:00
|
|
|
value=get_device_uptime,
|
2020-11-11 19:13:14 +00:00
|
|
|
device_class=sensor.DEVICE_CLASS_TIMESTAMP,
|
2020-11-19 10:42:24 +00:00
|
|
|
default_enabled=False,
|
2020-11-11 19:13:14 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2020-08-28 15:33:34 +00:00
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-08-28 15:33:34 +00:00
|
|
|
"""Set up sensors for device."""
|
2021-02-03 16:03:22 +00:00
|
|
|
if config_entry.data["sleep_period"]:
|
|
|
|
await async_setup_entry_attribute_entities(
|
|
|
|
hass, config_entry, async_add_entities, SENSORS, ShellySleepingSensor
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
await async_setup_entry_attribute_entities(
|
|
|
|
hass, config_entry, async_add_entities, SENSORS, ShellySensor
|
|
|
|
)
|
|
|
|
await async_setup_entry_rest(
|
|
|
|
hass, config_entry, async_add_entities, REST_SENSORS, ShellyRestSensor
|
|
|
|
)
|
2020-08-28 15:33:34 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class ShellySensor(ShellyBlockAttributeEntity, SensorEntity):
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Represent a shelly sensor."""
|
2020-08-28 15:33:34 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-12 15:40:55 +00:00
|
|
|
def native_value(self) -> StateType:
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Return value of sensor."""
|
|
|
|
return self.attribute_value
|
2020-11-11 19:13:14 +00:00
|
|
|
|
2021-05-23 20:10:22 +00:00
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def state_class(self) -> str | None:
|
2021-05-23 20:10:22 +00:00
|
|
|
"""State class of sensor."""
|
|
|
|
return self.description.state_class
|
|
|
|
|
2021-03-23 14:56:33 +00:00
|
|
|
@property
|
2021-08-12 15:40:55 +00:00
|
|
|
def native_unit_of_measurement(self) -> str | None:
|
2021-03-23 14:56:33 +00:00
|
|
|
"""Return unit of sensor."""
|
2021-07-21 17:11:44 +00:00
|
|
|
return cast(str, self._unit)
|
2021-03-23 14:56:33 +00:00
|
|
|
|
2020-11-11 19:13:14 +00:00
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class ShellyRestSensor(ShellyRestAttributeEntity, SensorEntity):
|
2020-11-11 19:13:14 +00:00
|
|
|
"""Represent a shelly REST sensor."""
|
|
|
|
|
|
|
|
@property
|
2021-08-12 15:40:55 +00:00
|
|
|
def native_value(self) -> StateType:
|
2020-11-11 19:13:14 +00:00
|
|
|
"""Return value of sensor."""
|
|
|
|
return self.attribute_value
|
2021-02-03 16:03:22 +00:00
|
|
|
|
2021-05-21 09:44:34 +00:00
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def state_class(self) -> str | None:
|
2021-05-21 09:44:34 +00:00
|
|
|
"""State class of sensor."""
|
|
|
|
return self.description.state_class
|
|
|
|
|
2021-03-23 14:56:33 +00:00
|
|
|
@property
|
2021-08-12 15:40:55 +00:00
|
|
|
def native_unit_of_measurement(self) -> str | None:
|
2021-03-23 14:56:33 +00:00
|
|
|
"""Return unit of sensor."""
|
|
|
|
return self.description.unit
|
|
|
|
|
2021-02-03 16:03:22 +00:00
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class ShellySleepingSensor(ShellySleepingBlockAttributeEntity, SensorEntity):
|
2021-02-03 16:03:22 +00:00
|
|
|
"""Represent a shelly sleeping sensor."""
|
|
|
|
|
|
|
|
@property
|
2021-08-12 15:40:55 +00:00
|
|
|
def native_value(self) -> StateType:
|
2021-02-03 16:03:22 +00:00
|
|
|
"""Return value of sensor."""
|
|
|
|
if self.block is not None:
|
|
|
|
return self.attribute_value
|
|
|
|
|
|
|
|
return self.last_state
|
2021-03-23 14:56:33 +00:00
|
|
|
|
2021-05-23 20:10:22 +00:00
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def state_class(self) -> str | None:
|
2021-05-23 20:10:22 +00:00
|
|
|
"""State class of sensor."""
|
|
|
|
return self.description.state_class
|
|
|
|
|
2021-03-23 14:56:33 +00:00
|
|
|
@property
|
2021-08-12 15:40:55 +00:00
|
|
|
def native_unit_of_measurement(self) -> str | None:
|
2021-03-23 14:56:33 +00:00
|
|
|
"""Return unit of sensor."""
|
2021-07-21 17:11:44 +00:00
|
|
|
return cast(str, self._unit)
|