2020-08-28 15:33:34 +00:00
|
|
|
"""Sensor for Shelly."""
|
|
|
|
import aioshelly
|
|
|
|
|
|
|
|
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,
|
|
|
|
POWER_WATT,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
UNIT_PERCENTAGE,
|
|
|
|
VOLT,
|
|
|
|
)
|
2020-08-28 15:33:34 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
from . import ShellyBlockEntity, ShellyDeviceWrapper
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
SENSORS = {
|
2020-08-30 12:18:35 +00:00
|
|
|
"battery": [UNIT_PERCENTAGE, sensor.DEVICE_CLASS_BATTERY],
|
2020-09-03 08:54:25 +00:00
|
|
|
"concentration": [CONCENTRATION_PARTS_PER_MILLION, None],
|
2020-08-30 12:18:35 +00:00
|
|
|
"current": [ELECTRICAL_CURRENT_AMPERE, sensor.DEVICE_CLASS_CURRENT],
|
|
|
|
"deviceTemp": [None, sensor.DEVICE_CLASS_TEMPERATURE],
|
|
|
|
"energy": [ENERGY_KILO_WATT_HOUR, sensor.DEVICE_CLASS_ENERGY],
|
|
|
|
"energyReturned": [ENERGY_KILO_WATT_HOUR, sensor.DEVICE_CLASS_ENERGY],
|
2020-08-28 15:33:34 +00:00
|
|
|
"extTemp": [None, sensor.DEVICE_CLASS_TEMPERATURE],
|
|
|
|
"humidity": [UNIT_PERCENTAGE, sensor.DEVICE_CLASS_HUMIDITY],
|
2020-09-04 14:42:57 +00:00
|
|
|
"luminosity": ["lx", sensor.DEVICE_CLASS_ILLUMINANCE],
|
2020-08-30 12:18:35 +00:00
|
|
|
"overpowerValue": [POWER_WATT, sensor.DEVICE_CLASS_POWER],
|
|
|
|
"power": [POWER_WATT, sensor.DEVICE_CLASS_POWER],
|
2020-09-04 14:42:57 +00:00
|
|
|
"powerFactor": [UNIT_PERCENTAGE, sensor.DEVICE_CLASS_POWER_FACTOR],
|
|
|
|
"tilt": [DEGREE, None],
|
2020-08-30 12:18:35 +00:00
|
|
|
"voltage": [VOLT, sensor.DEVICE_CLASS_VOLTAGE],
|
2020-08-28 15:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up sensors for device."""
|
|
|
|
wrapper = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
sensors = []
|
|
|
|
|
|
|
|
for block in wrapper.device.blocks:
|
|
|
|
for attr in SENSORS:
|
2020-09-01 12:18:11 +00:00
|
|
|
# Filter out non-existing sensors and sensors without a value
|
|
|
|
if getattr(block, attr, None) is None:
|
2020-08-28 15:33:34 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
sensors.append(ShellySensor(wrapper, block, attr))
|
|
|
|
|
|
|
|
if sensors:
|
|
|
|
async_add_entities(sensors)
|
|
|
|
|
|
|
|
|
|
|
|
class ShellySensor(ShellyBlockEntity, Entity):
|
|
|
|
"""Switch that controls a relay block on Shelly devices."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
wrapper: ShellyDeviceWrapper,
|
|
|
|
block: aioshelly.Block,
|
|
|
|
attribute: str,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize sensor."""
|
|
|
|
super().__init__(wrapper, block)
|
|
|
|
self.attribute = attribute
|
|
|
|
unit, device_class = SENSORS[attribute]
|
2020-08-30 12:18:35 +00:00
|
|
|
self.info = block.info(attribute)
|
2020-08-28 15:33:34 +00:00
|
|
|
|
2020-08-30 12:18:35 +00:00
|
|
|
if (
|
|
|
|
self.info[aioshelly.BLOCK_VALUE_TYPE]
|
|
|
|
== aioshelly.BLOCK_VALUE_TYPE_TEMPERATURE
|
|
|
|
):
|
|
|
|
if self.info[aioshelly.BLOCK_VALUE_UNIT] == "C":
|
2020-08-28 15:33:34 +00:00
|
|
|
unit = TEMP_CELSIUS
|
|
|
|
else:
|
|
|
|
unit = TEMP_FAHRENHEIT
|
|
|
|
|
|
|
|
self._unit = unit
|
|
|
|
self._device_class = device_class
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique ID of entity."""
|
|
|
|
return f"{super().unique_id}-{self.attribute}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Name of sensor."""
|
|
|
|
return f"{self.wrapper.name} - {self.attribute}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Value of sensor."""
|
2020-09-01 12:04:42 +00:00
|
|
|
value = getattr(self.block, self.attribute)
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
|
2020-09-04 14:42:57 +00:00
|
|
|
if self.attribute in ["luminosity", "tilt"]:
|
|
|
|
return round(value)
|
2020-08-30 12:18:35 +00:00
|
|
|
if self.attribute in [
|
|
|
|
"deviceTemp",
|
|
|
|
"extTemp",
|
|
|
|
"humidity",
|
|
|
|
"overpowerValue",
|
|
|
|
"power",
|
|
|
|
]:
|
2020-09-01 12:04:42 +00:00
|
|
|
return round(value, 1)
|
2020-09-04 14:42:57 +00:00
|
|
|
if self.attribute == "powerFactor":
|
|
|
|
return round(value * 100, 1)
|
2020-08-30 12:18:35 +00:00
|
|
|
# Energy unit change from Wmin or Wh to kWh
|
2020-09-04 09:44:22 +00:00
|
|
|
if self.info.get(aioshelly.BLOCK_VALUE_UNIT) == "Wmin":
|
2020-09-01 12:04:42 +00:00
|
|
|
return round(value / 60 / 1000, 2)
|
2020-09-04 09:44:22 +00:00
|
|
|
if self.info.get(aioshelly.BLOCK_VALUE_UNIT) == "Wh":
|
2020-09-01 12:04:42 +00:00
|
|
|
return round(value / 1000, 2)
|
|
|
|
return value
|
2020-08-28 15:33:34 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return unit of sensor."""
|
|
|
|
return self._unit
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Device class of sensor."""
|
|
|
|
return self._device_class
|
2020-09-03 08:54:25 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Available."""
|
|
|
|
if self.attribute == "concentration":
|
|
|
|
# "sensorOp" is "normal" when the Shelly Gas is working properly and taking
|
|
|
|
# measurements.
|
|
|
|
return super().available and self.block.sensorOp == "normal"
|
|
|
|
return super().available
|