2020-08-28 15:33:34 +00:00
|
|
|
"""Sensor for Shelly."""
|
|
|
|
from homeassistant.components import sensor
|
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,
|
2020-08-30 12:18:35 +00:00
|
|
|
ELECTRICAL_CURRENT_AMPERE,
|
|
|
|
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,
|
2020-10-01 10:08:03 +00:00
|
|
|
VOLT,
|
2020-08-30 12:18:35 +00:00
|
|
|
)
|
2020-08-28 15:33:34 +00:00
|
|
|
|
2020-09-07 12:13:20 +00:00
|
|
|
from .entity import (
|
|
|
|
BlockAttributeDescription,
|
|
|
|
ShellyBlockAttributeEntity,
|
|
|
|
async_setup_entry_attribute_entities,
|
|
|
|
)
|
2020-11-11 08:05:08 +00:00
|
|
|
from .utils import temperature_unit
|
2020-08-28 15:33:34 +00:00
|
|
|
|
|
|
|
SENSORS = {
|
2020-09-07 12:13:20 +00:00
|
|
|
("device", "battery"): BlockAttributeDescription(
|
|
|
|
name="Battery", unit=PERCENTAGE, device_class=sensor.DEVICE_CLASS_BATTERY
|
|
|
|
),
|
|
|
|
("device", "deviceTemp"): BlockAttributeDescription(
|
|
|
|
name="Device Temperature",
|
|
|
|
unit=temperature_unit,
|
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_TEMPERATURE,
|
|
|
|
default_enabled=False,
|
|
|
|
),
|
|
|
|
("emeter", "current"): BlockAttributeDescription(
|
|
|
|
name="Current",
|
|
|
|
unit=ELECTRICAL_CURRENT_AMPERE,
|
|
|
|
value=lambda value: value,
|
|
|
|
device_class=sensor.DEVICE_CLASS_CURRENT,
|
|
|
|
),
|
|
|
|
("light", "power"): BlockAttributeDescription(
|
|
|
|
name="Power",
|
|
|
|
unit=POWER_WATT,
|
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_POWER,
|
|
|
|
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,
|
|
|
|
),
|
|
|
|
("emeter", "power"): BlockAttributeDescription(
|
|
|
|
name="Power",
|
|
|
|
unit=POWER_WATT,
|
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_POWER,
|
|
|
|
),
|
2020-10-01 10:08:03 +00:00
|
|
|
("emeter", "voltage"): BlockAttributeDescription(
|
|
|
|
name="Voltage",
|
|
|
|
unit=VOLT,
|
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_VOLTAGE,
|
|
|
|
),
|
|
|
|
("emeter", "powerFactor"): BlockAttributeDescription(
|
|
|
|
name="Power Factor",
|
|
|
|
unit=PERCENTAGE,
|
|
|
|
value=lambda value: round(value * 100, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_POWER_FACTOR,
|
|
|
|
),
|
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,
|
|
|
|
),
|
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,
|
|
|
|
),
|
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,
|
|
|
|
),
|
|
|
|
("emeter", "energy"): BlockAttributeDescription(
|
|
|
|
name="Energy",
|
|
|
|
unit=ENERGY_KILO_WATT_HOUR,
|
|
|
|
value=lambda value: round(value / 1000, 2),
|
|
|
|
device_class=sensor.DEVICE_CLASS_ENERGY,
|
|
|
|
),
|
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,
|
|
|
|
),
|
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,
|
|
|
|
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,
|
|
|
|
),
|
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,
|
|
|
|
),
|
2020-09-07 12:13:20 +00:00
|
|
|
("sensor", "concentration"): BlockAttributeDescription(
|
|
|
|
name="Gas Concentration",
|
|
|
|
unit=CONCENTRATION_PARTS_PER_MILLION,
|
|
|
|
value=lambda value: value,
|
|
|
|
# "sensorOp" is "normal" when the Shelly Gas is working properly and taking measurements.
|
|
|
|
available=lambda block: block.sensorOp == "normal",
|
|
|
|
),
|
|
|
|
("sensor", "extTemp"): BlockAttributeDescription(
|
|
|
|
name="Temperature",
|
|
|
|
unit=temperature_unit,
|
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_TEMPERATURE,
|
|
|
|
),
|
|
|
|
("sensor", "humidity"): BlockAttributeDescription(
|
|
|
|
name="Humidity",
|
|
|
|
unit=PERCENTAGE,
|
|
|
|
value=lambda value: round(value, 1),
|
|
|
|
device_class=sensor.DEVICE_CLASS_HUMIDITY,
|
|
|
|
),
|
|
|
|
("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,
|
|
|
|
),
|
|
|
|
("sensor", "tilt"): BlockAttributeDescription(name="tilt", unit=DEGREE),
|
2020-08-28 15:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up sensors for device."""
|
2020-09-07 12:13:20 +00:00
|
|
|
await async_setup_entry_attribute_entities(
|
|
|
|
hass, config_entry, async_add_entities, SENSORS, ShellySensor
|
|
|
|
)
|
2020-08-28 15:33:34 +00:00
|
|
|
|
|
|
|
|
2020-09-07 12:13:20 +00:00
|
|
|
class ShellySensor(ShellyBlockAttributeEntity):
|
|
|
|
"""Represent a shelly sensor."""
|
2020-08-28 15:33:34 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2020-09-07 12:13:20 +00:00
|
|
|
"""Return value of sensor."""
|
|
|
|
return self.attribute_value
|