1 """Support gathering ted5000 information."""
3 from __future__
import annotations
5 from contextlib
import suppress
6 from datetime
import timedelta
10 import voluptuous
as vol
14 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
17 SensorEntityDescription,
24 UnitOfElectricPotential,
33 _LOGGER = logging.getLogger(__name__)
40 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
42 vol.Required(CONF_HOST): cv.string,
43 vol.Optional(CONF_PORT, default=80): cv.port,
44 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
51 native_unit_of_measurement=UnitOfPower.WATT,
52 device_class=SensorDeviceClass.POWER,
53 state_class=SensorStateClass.MEASUREMENT,
57 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
58 device_class=SensorDeviceClass.VOLTAGE,
59 state_class=SensorStateClass.MEASUREMENT,
67 add_entities: AddEntitiesCallback,
68 discovery_info: DiscoveryInfoType |
None =
None,
70 """Set up the Ted5000 sensor."""
71 host: str = config[CONF_HOST]
72 port: int = config[CONF_PORT]
73 name: str = config[CONF_NAME]
74 url = f
"http://{host}:{port}/api/LiveData.xml"
83 for mtu
in gateway.data
84 for description
in SENSORS
89 """Implementation of a Ted5000 sensor."""
93 gateway: Ted5000Gateway,
96 description: SensorEntityDescription,
98 """Initialize the sensor."""
100 self.
_attr_name_attr_name = f
"{name} mtu{mtu} {description.key}"
107 """Return the state of the resources."""
109 with suppress(KeyError):
114 """Get the latest data from REST API."""
119 """The class for handling the data retrieval."""
122 """Initialize the data object."""
124 self.data: dict[int, dict[str, int | float]] = {}
126 @Throttle(MIN_TIME_BETWEEN_UPDATES)
128 """Get the latest data from the Ted5000 XML API."""
131 request = requests.get(self.
urlurl, timeout=10)
132 except requests.exceptions.RequestException
as err:
133 _LOGGER.error(
"No connection to endpoint: %s", err)
135 doc = xmltodict.parse(request.text)
136 mtus =
int(doc[
"LiveData"][
"System"][
"NumberMTU"])
138 for mtu
in range(1, mtus + 1):
139 power =
int(doc[
"LiveData"][
"Power"][f
"MTU{mtu}"][
"PowerNow"])
140 voltage =
int(doc[
"LiveData"][
"Voltage"][f
"MTU{mtu}"][
"VoltageNow"])
143 UnitOfPower.WATT: power,
144 UnitOfElectricPotential.VOLT: voltage / 10,
None __init__(self, str url)
None __init__(self, Ted5000Gateway gateway, str name, int mtu, SensorEntityDescription description)
int|float|None native_value(self)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)