diff --git a/custom_components/ocpp/__init__.py b/custom_components/ocpp/__init__.py index 70614747..cfbce73b 100644 --- a/custom_components/ocpp/__init__.py +++ b/custom_components/ocpp/__init__.py @@ -8,7 +8,7 @@ from homeassistant.helpers import device_registry from .api import CentralSystem -from .const import CONF_CPID, CONF_CSID, DOMAIN, PLATFORMS +from .const import CONF_CPID, CONF_CSID, DEFAULT_CPID, DEFAULT_CSID, DOMAIN, PLATFORMS _LOGGER: logging.Logger = logging.getLogger(__package__) logging.getLogger(DOMAIN).setLevel(logging.DEBUG) @@ -32,16 +32,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """ Create Central System Device """ dr.async_get_or_create( config_entry_id=entry.entry_id, - identifiers={(DOMAIN, entry.data[CONF_CSID])}, - name=entry.data[CONF_CSID], + identifiers={(DOMAIN, entry.data.get(CONF_CSID, DEFAULT_CSID))}, + name=entry.data.get(CONF_CSID, DEFAULT_CSID), model="OCPP Central System", ) """ Create Charge Point Device """ dr.async_get_or_create( config_entry_id=entry.entry_id, - identifiers={(DOMAIN, entry.data[CONF_CPID])}, - name=entry.data[CONF_CPID], + identifiers={(DOMAIN, entry.data.get(CONF_CPID, DEFAULT_CPID))}, + name=entry.data.get(CONF_CPID, DEFAULT_CPID), default_model="OCPP Charge Point", via_device=((DOMAIN), central_sys.id), ) diff --git a/custom_components/ocpp/sensor.py b/custom_components/ocpp/sensor.py index 69e5cdb0..54f355aa 100644 --- a/custom_components/ocpp/sensor.py +++ b/custom_components/ocpp/sensor.py @@ -1,17 +1,29 @@ """Sensor platform for ocpp.""" -from homeassistant.const import CONF_MONITORED_VARIABLES -from homeassistant.helpers.entity import Entity +import datetime + +import homeassistant +from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity +from homeassistant.const import ( + CONF_MONITORED_VARIABLES, + DEVICE_CLASS_CURRENT, + DEVICE_CLASS_ENERGY, + DEVICE_CLASS_POWER, + DEVICE_CLASS_TEMPERATURE, + DEVICE_CLASS_VOLTAGE, +) + +from ocpp.v16.enums import UnitOfMeasure from .api import CentralSystem -from .const import CONF_CPID, DOMAIN, ICON +from .const import CONF_CPID, DEFAULT_CPID, DOMAIN, ICON from .enums import HAChargerDetails, HAChargerSession, HAChargerStatuses async def async_setup_entry(hass, entry, async_add_devices): """Configure the sensor platform.""" central_system = hass.data[DOMAIN][entry.entry_id] - cp_id = entry.data[CONF_CPID] + cp_id = entry.data.get(CONF_CPID, DEFAULT_CPID) entities = [] @@ -36,7 +48,7 @@ async def async_setup_entry(hass, entry, async_add_devices): async_add_devices(entities, False) -class ChargePointMetric(Entity): +class ChargePointMetric(SensorEntity): """Individual sensor for charge point metrics.""" def __init__( @@ -51,6 +63,7 @@ def __init__( self.metric = metric self._state = None self._extra_attr = {} + self._last_reset = homeassistant.util.dt.utc_from_timestamp(0) @property def name(self): @@ -65,7 +78,17 @@ def unique_id(self): @property def state(self): """Return the state of the sensor.""" - return self.central_system.get_metric(self.cp_id, self.metric) + old_state = self._state + new_state = self.central_system.get_metric(self.cp_id, self.metric) + self._state = new_state + if ( + (self.device_class is DEVICE_CLASS_ENERGY) + and (new_state is not None) + and (old_state is not None) + and (new_state < old_state) + ): + self._last_reset = datetime.datetime.now() + return self._state @property def available(self) -> bool: @@ -103,6 +126,41 @@ def extra_state_attributes(self): """Return the state attributes.""" return self.central_system.get_extra_attr(self.cp_id, self.metric) + @property + def state_class(self): + """Return the state class of the sensor.""" + return STATE_CLASS_MEASUREMENT + + @property + def device_class(self): + """Return the device class of the sensor.""" + if self.unit_of_measurement in [ + UnitOfMeasure.wh.value, + UnitOfMeasure.kwh.value, + ]: + return DEVICE_CLASS_ENERGY + elif self.unit_of_measurement in [ + UnitOfMeasure.w.value, + UnitOfMeasure.kw.value, + ]: + return DEVICE_CLASS_POWER + elif self.unit_of_measurement in [ + UnitOfMeasure.celsius.value, + UnitOfMeasure.fahrenheit.value, + ]: + return DEVICE_CLASS_TEMPERATURE + elif self.unit_of_measurement in [UnitOfMeasure.a.value]: + return DEVICE_CLASS_CURRENT + elif self.unit_of_measurement in [UnitOfMeasure.v.value]: + return DEVICE_CLASS_VOLTAGE + else: + return None + + @property + def last_reset(self): + """Return the time when a metered value wwas ;ast reset.""" + return self._last_reset + async def async_update(self): """Get the latest data and update the states.""" pass diff --git a/custom_components/ocpp/switch.py b/custom_components/ocpp/switch.py index dd0f3a51..9fe1d108 100644 --- a/custom_components/ocpp/switch.py +++ b/custom_components/ocpp/switch.py @@ -4,13 +4,13 @@ from homeassistant.components.switch import SwitchEntity from .api import CentralSystem -from .const import CONF_CPID, DOMAIN, ICON, SWITCHES +from .const import CONF_CPID, DEFAULT_CPID, DOMAIN, ICON, SWITCHES async def async_setup_entry(hass, entry, async_add_devices): """Configure the sensor platform.""" central_system = hass.data[DOMAIN][entry.entry_id] - cp_id = entry.data[CONF_CPID] + cp_id = entry.data.get(CONF_CPID, DEFAULT_CPID) entities = []