Rework Shelly sensors (#39747)
* Rework Shelly sensors * Lint * Add shelly/entity to coveragerc
This commit is contained in:
parent
90c6e1c449
commit
b07628ae57
7 changed files with 309 additions and 249 deletions
|
@ -1,6 +1,4 @@
|
|||
"""Sensor for Shelly."""
|
||||
import aioshelly
|
||||
|
||||
from homeassistant.components import sensor
|
||||
from homeassistant.const import (
|
||||
CONCENTRATION_PARTS_PER_MILLION,
|
||||
|
@ -9,128 +7,84 @@ from homeassistant.const import (
|
|||
ENERGY_KILO_WATT_HOUR,
|
||||
PERCENTAGE,
|
||||
POWER_WATT,
|
||||
TEMP_CELSIUS,
|
||||
TEMP_FAHRENHEIT,
|
||||
VOLT,
|
||||
)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
from . import ShellyBlockEntity, ShellyDeviceWrapper
|
||||
from .const import DOMAIN
|
||||
from .entity import (
|
||||
BlockAttributeDescription,
|
||||
ShellyBlockAttributeEntity,
|
||||
async_setup_entry_attribute_entities,
|
||||
temperature_unit,
|
||||
)
|
||||
|
||||
SENSORS = {
|
||||
"battery": [PERCENTAGE, sensor.DEVICE_CLASS_BATTERY],
|
||||
"concentration": [CONCENTRATION_PARTS_PER_MILLION, None],
|
||||
"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],
|
||||
"extTemp": [None, sensor.DEVICE_CLASS_TEMPERATURE],
|
||||
"humidity": [PERCENTAGE, sensor.DEVICE_CLASS_HUMIDITY],
|
||||
"luminosity": ["lx", sensor.DEVICE_CLASS_ILLUMINANCE],
|
||||
"overpowerValue": [POWER_WATT, sensor.DEVICE_CLASS_POWER],
|
||||
"power": [POWER_WATT, sensor.DEVICE_CLASS_POWER],
|
||||
"powerFactor": [PERCENTAGE, sensor.DEVICE_CLASS_POWER_FACTOR],
|
||||
"tilt": [DEGREE, None],
|
||||
"voltage": [VOLT, sensor.DEVICE_CLASS_VOLTAGE],
|
||||
("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,
|
||||
),
|
||||
("relay", "energy"): BlockAttributeDescription(
|
||||
name="Energy",
|
||||
unit=ENERGY_KILO_WATT_HOUR,
|
||||
value=lambda value: round(value / 60 / 1000, 2),
|
||||
device_class=sensor.DEVICE_CLASS_ENERGY,
|
||||
),
|
||||
("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",
|
||||
unit="lx",
|
||||
device_class=sensor.DEVICE_CLASS_ILLUMINANCE,
|
||||
),
|
||||
("sensor", "tilt"): BlockAttributeDescription(name="tilt", unit=DEGREE),
|
||||
}
|
||||
|
||||
|
||||
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:
|
||||
# Filter out non-existing sensors and sensors without a value
|
||||
if getattr(block, attr, None) is None:
|
||||
continue
|
||||
|
||||
sensors.append(ShellySensor(wrapper, block, attr))
|
||||
|
||||
if sensors:
|
||||
async_add_entities(sensors)
|
||||
await async_setup_entry_attribute_entities(
|
||||
hass, config_entry, async_add_entities, SENSORS, ShellySensor
|
||||
)
|
||||
|
||||
|
||||
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]
|
||||
self.info = block.info(attribute)
|
||||
|
||||
if (
|
||||
self.info[aioshelly.BLOCK_VALUE_TYPE]
|
||||
== aioshelly.BLOCK_VALUE_TYPE_TEMPERATURE
|
||||
):
|
||||
if self.info[aioshelly.BLOCK_VALUE_UNIT] == "C":
|
||||
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}"
|
||||
class ShellySensor(ShellyBlockAttributeEntity):
|
||||
"""Represent a shelly sensor."""
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Value of sensor."""
|
||||
value = getattr(self.block, self.attribute)
|
||||
if value is None:
|
||||
return None
|
||||
|
||||
if self.attribute in ["luminosity", "tilt"]:
|
||||
return round(value)
|
||||
if self.attribute in [
|
||||
"deviceTemp",
|
||||
"extTemp",
|
||||
"humidity",
|
||||
"overpowerValue",
|
||||
"power",
|
||||
]:
|
||||
return round(value, 1)
|
||||
if self.attribute == "powerFactor":
|
||||
return round(value * 100, 1)
|
||||
# Energy unit change from Wmin or Wh to kWh
|
||||
if self.info.get(aioshelly.BLOCK_VALUE_UNIT) == "Wmin":
|
||||
return round(value / 60 / 1000, 2)
|
||||
if self.info.get(aioshelly.BLOCK_VALUE_UNIT) == "Wh":
|
||||
return round(value / 1000, 2)
|
||||
return value
|
||||
|
||||
@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
|
||||
|
||||
@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
|
||||
"""Return value of sensor."""
|
||||
return self.attribute_value
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue