mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
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>
This commit is contained in:
parent
583b4aef07
commit
b8a5869f76
@ -11,15 +11,17 @@ import xmltodict
|
|||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
|
SensorDeviceClass,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
CONF_PORT,
|
CONF_PORT,
|
||||||
ELECTRIC_POTENTIAL_VOLT,
|
UnitOfElectricPotential,
|
||||||
POWER_WATT,
|
UnitOfPower,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import config_validation as cv
|
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(
|
def setup_platform(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
@ -50,9 +67,9 @@ def setup_platform(
|
|||||||
discovery_info: DiscoveryInfoType | None = None,
|
discovery_info: DiscoveryInfoType | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Ted5000 sensor."""
|
"""Set up the Ted5000 sensor."""
|
||||||
host = config.get(CONF_HOST)
|
host: str = config[CONF_HOST]
|
||||||
port = config.get(CONF_PORT)
|
port: int = config[CONF_PORT]
|
||||||
name = config.get(CONF_NAME)
|
name: str = config[CONF_NAME]
|
||||||
url = f"http://{host}:{port}/api/LiveData.xml"
|
url = f"http://{host}:{port}/api/LiveData.xml"
|
||||||
|
|
||||||
gateway = Ted5000Gateway(url)
|
gateway = Ted5000Gateway(url)
|
||||||
@ -60,43 +77,38 @@ def setup_platform(
|
|||||||
# Get MUT information to create the sensors.
|
# Get MUT information to create the sensors.
|
||||||
gateway.update()
|
gateway.update()
|
||||||
|
|
||||||
dev = []
|
entities = []
|
||||||
for mtu in gateway.data:
|
for mtu in gateway.data:
|
||||||
dev.append(Ted5000Sensor(gateway, name, mtu, POWER_WATT))
|
for description in SENSORS:
|
||||||
dev.append(Ted5000Sensor(gateway, name, mtu, ELECTRIC_POTENTIAL_VOLT))
|
entities.append(Ted5000Sensor(gateway, name, mtu, description))
|
||||||
|
|
||||||
add_entities(dev)
|
add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class Ted5000Sensor(SensorEntity):
|
class Ted5000Sensor(SensorEntity):
|
||||||
"""Implementation of a Ted5000 sensor."""
|
"""Implementation of a Ted5000 sensor."""
|
||||||
|
|
||||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
def __init__(
|
||||||
|
self,
|
||||||
def __init__(self, gateway, name, mtu, unit):
|
gateway: Ted5000Gateway,
|
||||||
|
name: str,
|
||||||
|
mtu: int,
|
||||||
|
description: SensorEntityDescription,
|
||||||
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
units = {POWER_WATT: "power", ELECTRIC_POTENTIAL_VOLT: "voltage"}
|
|
||||||
self._gateway = gateway
|
self._gateway = gateway
|
||||||
self._name = f"{name} mtu{mtu} {units[unit]}"
|
self._attr_name = f"{name} mtu{mtu} {description.key}"
|
||||||
self._mtu = mtu
|
self._mtu = mtu
|
||||||
self._unit = unit
|
self.entity_description = description
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def native_value(self) -> int | float | None:
|
||||||
"""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):
|
|
||||||
"""Return the state of the resources."""
|
"""Return the state of the resources."""
|
||||||
with suppress(KeyError):
|
if unit := self.entity_description.native_unit_of_measurement:
|
||||||
return self._gateway.data[self._mtu][self._unit]
|
with suppress(KeyError):
|
||||||
|
return self._gateway.data[self._mtu][unit]
|
||||||
|
return None
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""Get the latest data from REST API."""
|
"""Get the latest data from REST API."""
|
||||||
@ -106,13 +118,13 @@ class Ted5000Sensor(SensorEntity):
|
|||||||
class Ted5000Gateway:
|
class Ted5000Gateway:
|
||||||
"""The class for handling the data retrieval."""
|
"""The class for handling the data retrieval."""
|
||||||
|
|
||||||
def __init__(self, url):
|
def __init__(self, url: str) -> None:
|
||||||
"""Initialize the data object."""
|
"""Initialize the data object."""
|
||||||
self.url = url
|
self.url = url
|
||||||
self.data = {}
|
self.data: dict[int, dict[str, int | float]] = {}
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||||
def update(self):
|
def update(self) -> None:
|
||||||
"""Get the latest data from the Ted5000 XML API."""
|
"""Get the latest data from the Ted5000 XML API."""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -128,6 +140,6 @@ class Ted5000Gateway:
|
|||||||
voltage = int(doc["LiveData"]["Voltage"]["MTU%d" % mtu]["VoltageNow"])
|
voltage = int(doc["LiveData"]["Voltage"]["MTU%d" % mtu]["VoltageNow"])
|
||||||
|
|
||||||
self.data[mtu] = {
|
self.data[mtu] = {
|
||||||
POWER_WATT: power,
|
UnitOfPower.WATT: power,
|
||||||
ELECTRIC_POTENTIAL_VOLT: voltage / 10,
|
UnitOfElectricPotential.VOLT: voltage / 10,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user