Refactor ted5000 to use entity descriptions (#83820)
* Refactor ted5000 to use entity descriptions * Apply suggestions from code review Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Refactor native_value Co-authored-by: Martin Hjelmare <marhje52@gmail.com>pull/83842/head
parent
583b4aef07
commit
b8a5869f76
|
@ -11,15 +11,17 @@ import xmltodict
|
|||
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_HOST,
|
||||
CONF_NAME,
|
||||
CONF_PORT,
|
||||
ELECTRIC_POTENTIAL_VOLT,
|
||||
POWER_WATT,
|
||||
UnitOfElectricPotential,
|
||||
UnitOfPower,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
@ -42,6 +44,21 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
}
|
||||
)
|
||||
|
||||
SENSORS = [
|
||||
SensorEntityDescription(
|
||||
key="power",
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="voltage",
|
||||
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
|
||||
device_class=SensorDeviceClass.VOLTAGE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def setup_platform(
|
||||
hass: HomeAssistant,
|
||||
|
@ -50,9 +67,9 @@ def setup_platform(
|
|||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the Ted5000 sensor."""
|
||||
host = config.get(CONF_HOST)
|
||||
port = config.get(CONF_PORT)
|
||||
name = config.get(CONF_NAME)
|
||||
host: str = config[CONF_HOST]
|
||||
port: int = config[CONF_PORT]
|
||||
name: str = config[CONF_NAME]
|
||||
url = f"http://{host}:{port}/api/LiveData.xml"
|
||||
|
||||
gateway = Ted5000Gateway(url)
|
||||
|
@ -60,43 +77,38 @@ def setup_platform(
|
|||
# Get MUT information to create the sensors.
|
||||
gateway.update()
|
||||
|
||||
dev = []
|
||||
entities = []
|
||||
for mtu in gateway.data:
|
||||
dev.append(Ted5000Sensor(gateway, name, mtu, POWER_WATT))
|
||||
dev.append(Ted5000Sensor(gateway, name, mtu, ELECTRIC_POTENTIAL_VOLT))
|
||||
for description in SENSORS:
|
||||
entities.append(Ted5000Sensor(gateway, name, mtu, description))
|
||||
|
||||
add_entities(dev)
|
||||
add_entities(entities)
|
||||
|
||||
|
||||
class Ted5000Sensor(SensorEntity):
|
||||
"""Implementation of a Ted5000 sensor."""
|
||||
|
||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||
|
||||
def __init__(self, gateway, name, mtu, unit):
|
||||
def __init__(
|
||||
self,
|
||||
gateway: Ted5000Gateway,
|
||||
name: str,
|
||||
mtu: int,
|
||||
description: SensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
units = {POWER_WATT: "power", ELECTRIC_POTENTIAL_VOLT: "voltage"}
|
||||
self._gateway = gateway
|
||||
self._name = f"{name} mtu{mtu} {units[unit]}"
|
||||
self._attr_name = f"{name} mtu{mtu} {description.key}"
|
||||
self._mtu = mtu
|
||||
self._unit = unit
|
||||
self.entity_description = description
|
||||
self.update()
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def native_unit_of_measurement(self):
|
||||
"""Return the unit the value is expressed in."""
|
||||
return self._unit
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
def native_value(self) -> int | float | None:
|
||||
"""Return the state of the resources."""
|
||||
with suppress(KeyError):
|
||||
return self._gateway.data[self._mtu][self._unit]
|
||||
if unit := self.entity_description.native_unit_of_measurement:
|
||||
with suppress(KeyError):
|
||||
return self._gateway.data[self._mtu][unit]
|
||||
return None
|
||||
|
||||
def update(self) -> None:
|
||||
"""Get the latest data from REST API."""
|
||||
|
@ -106,13 +118,13 @@ class Ted5000Sensor(SensorEntity):
|
|||
class Ted5000Gateway:
|
||||
"""The class for handling the data retrieval."""
|
||||
|
||||
def __init__(self, url):
|
||||
def __init__(self, url: str) -> None:
|
||||
"""Initialize the data object."""
|
||||
self.url = url
|
||||
self.data = {}
|
||||
self.data: dict[int, dict[str, int | float]] = {}
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
def update(self) -> None:
|
||||
"""Get the latest data from the Ted5000 XML API."""
|
||||
|
||||
try:
|
||||
|
@ -128,6 +140,6 @@ class Ted5000Gateway:
|
|||
voltage = int(doc["LiveData"]["Voltage"]["MTU%d" % mtu]["VoltageNow"])
|
||||
|
||||
self.data[mtu] = {
|
||||
POWER_WATT: power,
|
||||
ELECTRIC_POTENTIAL_VOLT: voltage / 10,
|
||||
UnitOfPower.WATT: power,
|
||||
UnitOfElectricPotential.VOLT: voltage / 10,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue