2019-04-03 15:40:03 +00:00
|
|
|
"""Support for monitoring an OpenEVSE Charger."""
|
2021-08-23 19:29:22 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-01-30 20:29:56 +00:00
|
|
|
import logging
|
|
|
|
|
2019-10-21 07:55:29 +00:00
|
|
|
import openevsewifi
|
2017-01-30 20:29:56 +00:00
|
|
|
from requests import RequestException
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-08-23 19:29:22 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
2021-12-16 16:12:18 +00:00
|
|
|
SensorDeviceClass,
|
2021-08-23 19:29:22 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2019-03-13 01:46:41 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_MONITORED_VARIABLES,
|
2019-10-21 07:55:29 +00:00
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
TEMP_CELSIUS,
|
2020-02-23 20:09:24 +00:00
|
|
|
TIME_MINUTES,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-01-03 18:10:57 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-10-21 07:55:29 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-03 18:10:57 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2017-01-30 20:29:56 +00:00
|
|
|
|
2017-06-13 09:10:32 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-08-23 19:29:22 +00:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="status",
|
|
|
|
name="Charging Status",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="charge_time",
|
|
|
|
name="Charge Time Elapsed",
|
|
|
|
native_unit_of_measurement=TIME_MINUTES,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="ambient_temp",
|
|
|
|
name="Ambient Temperature",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
2021-12-16 16:12:18 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2021-08-23 19:29:22 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="ir_temp",
|
|
|
|
name="IR Temperature",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
2021-12-16 16:12:18 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2021-08-23 19:29:22 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="rtc_temp",
|
|
|
|
name="RTC Temperature",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
2021-12-16 16:12:18 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2021-08-23 19:29:22 +00:00
|
|
|
),
|
|
|
|
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]
|
2017-01-30 20:29:56 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_MONITORED_VARIABLES, default=["status"]): vol.All(
|
2021-08-23 19:29:22 +00:00
|
|
|
cv.ensure_list, [vol.In(SENSOR_KEYS)]
|
2019-07-31 19:25:30 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2017-01-30 20:29:56 +00:00
|
|
|
|
|
|
|
|
2022-01-03 18:10:57 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-02-11 19:29:37 +00:00
|
|
|
"""Set up the OpenEVSE sensor."""
|
2021-08-23 19:29:22 +00:00
|
|
|
host = config[CONF_HOST]
|
|
|
|
monitored_variables = config[CONF_MONITORED_VARIABLES]
|
2017-01-30 20:29:56 +00:00
|
|
|
|
|
|
|
charger = openevsewifi.Charger(host)
|
|
|
|
|
2021-08-23 19:29:22 +00:00
|
|
|
entities = [
|
|
|
|
OpenEVSESensor(charger, description)
|
|
|
|
for description in SENSOR_TYPES
|
|
|
|
if description.key in monitored_variables
|
|
|
|
]
|
2017-01-30 20:29:56 +00:00
|
|
|
|
2021-08-23 19:29:22 +00:00
|
|
|
add_entities(entities, True)
|
2017-01-30 20:29:56 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
class OpenEVSESensor(SensorEntity):
|
2017-01-30 20:29:56 +00:00
|
|
|
"""Implementation of an OpenEVSE sensor."""
|
|
|
|
|
2021-08-23 19:29:22 +00:00
|
|
|
def __init__(self, charger, description: SensorEntityDescription):
|
2017-01-30 20:29:56 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-08-23 19:29:22 +00:00
|
|
|
self.entity_description = description
|
2017-01-30 20:29:56 +00:00
|
|
|
self.charger = charger
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the monitored data from the charger."""
|
|
|
|
try:
|
2021-08-23 19:29:22 +00:00
|
|
|
sensor_type = self.entity_description.key
|
|
|
|
if sensor_type == "status":
|
|
|
|
self._attr_native_value = self.charger.getStatus()
|
|
|
|
elif sensor_type == "charge_time":
|
|
|
|
self._attr_native_value = self.charger.getChargeTimeElapsed() / 60
|
|
|
|
elif sensor_type == "ambient_temp":
|
|
|
|
self._attr_native_value = self.charger.getAmbientTemperature()
|
|
|
|
elif sensor_type == "ir_temp":
|
|
|
|
self._attr_native_value = self.charger.getIRTemperature()
|
|
|
|
elif sensor_type == "rtc_temp":
|
|
|
|
self._attr_native_value = self.charger.getRTCTemperature()
|
|
|
|
elif sensor_type == "usage_session":
|
|
|
|
self._attr_native_value = float(self.charger.getUsageSession()) / 1000
|
|
|
|
elif sensor_type == "usage_total":
|
|
|
|
self._attr_native_value = float(self.charger.getUsageTotal()) / 1000
|
2017-01-30 20:29:56 +00:00
|
|
|
else:
|
2021-08-23 19:29:22 +00:00
|
|
|
self._attr_native_value = "Unknown"
|
2017-01-30 20:29:56 +00:00
|
|
|
except (RequestException, ValueError, KeyError):
|
2017-02-11 19:29:37 +00:00
|
|
|
_LOGGER.warning("Could not update status for %s", self.name)
|