mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Use EntityDescription - openevse (#55084)
This commit is contained in:
parent
6637ed4868
commit
791ccca042
@ -1,11 +1,17 @@
|
|||||||
"""Support for monitoring an OpenEVSE Charger."""
|
"""Support for monitoring an OpenEVSE Charger."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import openevsewifi
|
import openevsewifi
|
||||||
from requests import RequestException
|
from requests import RequestException
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
from homeassistant.components.sensor import (
|
||||||
|
PLATFORM_SCHEMA,
|
||||||
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_MONITORED_VARIABLES,
|
CONF_MONITORED_VARIABLES,
|
||||||
@ -18,21 +24,53 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
"status": ["Charging Status", None, None],
|
SensorEntityDescription(
|
||||||
"charge_time": ["Charge Time Elapsed", TIME_MINUTES, None],
|
key="status",
|
||||||
"ambient_temp": ["Ambient Temperature", TEMP_CELSIUS, DEVICE_CLASS_TEMPERATURE],
|
name="Charging Status",
|
||||||
"ir_temp": ["IR Temperature", TEMP_CELSIUS, DEVICE_CLASS_TEMPERATURE],
|
),
|
||||||
"rtc_temp": ["RTC Temperature", TEMP_CELSIUS, DEVICE_CLASS_TEMPERATURE],
|
SensorEntityDescription(
|
||||||
"usage_session": ["Usage this Session", ENERGY_KILO_WATT_HOUR, None],
|
key="charge_time",
|
||||||
"usage_total": ["Total Usage", ENERGY_KILO_WATT_HOUR, None],
|
name="Charge Time Elapsed",
|
||||||
}
|
native_unit_of_measurement=TIME_MINUTES,
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="ambient_temp",
|
||||||
|
name="Ambient Temperature",
|
||||||
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="ir_temp",
|
||||||
|
name="IR Temperature",
|
||||||
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="rtc_temp",
|
||||||
|
name="RTC Temperature",
|
||||||
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="usage_session",
|
||||||
|
name="Usage this Session",
|
||||||
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="usage_total",
|
||||||
|
name="Total Usage",
|
||||||
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_HOST): cv.string,
|
vol.Required(CONF_HOST): cv.string,
|
||||||
vol.Optional(CONF_MONITORED_VARIABLES, default=["status"]): vol.All(
|
vol.Optional(CONF_MONITORED_VARIABLES, default=["status"]): vol.All(
|
||||||
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
cv.ensure_list, [vol.In(SENSOR_KEYS)]
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -40,63 +78,47 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the OpenEVSE sensor."""
|
"""Set up the OpenEVSE sensor."""
|
||||||
host = config.get(CONF_HOST)
|
host = config[CONF_HOST]
|
||||||
monitored_variables = config.get(CONF_MONITORED_VARIABLES)
|
monitored_variables = config[CONF_MONITORED_VARIABLES]
|
||||||
|
|
||||||
charger = openevsewifi.Charger(host)
|
charger = openevsewifi.Charger(host)
|
||||||
|
|
||||||
dev = []
|
entities = [
|
||||||
for variable in monitored_variables:
|
OpenEVSESensor(charger, description)
|
||||||
dev.append(OpenEVSESensor(variable, charger))
|
for description in SENSOR_TYPES
|
||||||
|
if description.key in monitored_variables
|
||||||
|
]
|
||||||
|
|
||||||
add_entities(dev, True)
|
add_entities(entities, True)
|
||||||
|
|
||||||
|
|
||||||
class OpenEVSESensor(SensorEntity):
|
class OpenEVSESensor(SensorEntity):
|
||||||
"""Implementation of an OpenEVSE sensor."""
|
"""Implementation of an OpenEVSE sensor."""
|
||||||
|
|
||||||
def __init__(self, sensor_type, charger):
|
def __init__(self, charger, description: SensorEntityDescription):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._name = SENSOR_TYPES[sensor_type][0]
|
self.entity_description = description
|
||||||
self.type = sensor_type
|
|
||||||
self._state = None
|
|
||||||
self.charger = charger
|
self.charger = charger
|
||||||
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
|
||||||
self._attr_device_class = SENSOR_TYPES[sensor_type][2]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement of this sensor."""
|
|
||||||
return self._unit_of_measurement
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the monitored data from the charger."""
|
"""Get the monitored data from the charger."""
|
||||||
try:
|
try:
|
||||||
if self.type == "status":
|
sensor_type = self.entity_description.key
|
||||||
self._state = self.charger.getStatus()
|
if sensor_type == "status":
|
||||||
elif self.type == "charge_time":
|
self._attr_native_value = self.charger.getStatus()
|
||||||
self._state = self.charger.getChargeTimeElapsed() / 60
|
elif sensor_type == "charge_time":
|
||||||
elif self.type == "ambient_temp":
|
self._attr_native_value = self.charger.getChargeTimeElapsed() / 60
|
||||||
self._state = self.charger.getAmbientTemperature()
|
elif sensor_type == "ambient_temp":
|
||||||
elif self.type == "ir_temp":
|
self._attr_native_value = self.charger.getAmbientTemperature()
|
||||||
self._state = self.charger.getIRTemperature()
|
elif sensor_type == "ir_temp":
|
||||||
elif self.type == "rtc_temp":
|
self._attr_native_value = self.charger.getIRTemperature()
|
||||||
self._state = self.charger.getRTCTemperature()
|
elif sensor_type == "rtc_temp":
|
||||||
elif self.type == "usage_session":
|
self._attr_native_value = self.charger.getRTCTemperature()
|
||||||
self._state = float(self.charger.getUsageSession()) / 1000
|
elif sensor_type == "usage_session":
|
||||||
elif self.type == "usage_total":
|
self._attr_native_value = float(self.charger.getUsageSession()) / 1000
|
||||||
self._state = float(self.charger.getUsageTotal()) / 1000
|
elif sensor_type == "usage_total":
|
||||||
|
self._attr_native_value = float(self.charger.getUsageTotal()) / 1000
|
||||||
else:
|
else:
|
||||||
self._state = "Unknown"
|
self._attr_native_value = "Unknown"
|
||||||
except (RequestException, ValueError, KeyError):
|
except (RequestException, ValueError, KeyError):
|
||||||
_LOGGER.warning("Could not update status for %s", self.name)
|
_LOGGER.warning("Could not update status for %s", self.name)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user