Skip to content

Commit

Permalink
feat: handle timestamp sensors (#387)
Browse files Browse the repository at this point in the history
* feat: handle timestamp sensors

* formatting
  • Loading branch information
firstof9 authored Oct 11, 2024
1 parent 11b1732 commit 2a56826
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
7 changes: 7 additions & 0 deletions custom_components/openevse/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,13 @@
state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False,
),
"vehicle_eta_timestamp": SensorEntityDescription(
name="Vehicle Charge Completion Time",
key="vehicle_eta",
icon="mdi:car-electric",
device_class=SensorDeviceClass.TIMESTAMP,
entity_registry_enabled_default=False,
),
"total_day": SensorEntityDescription(
key="total_day",
name="Usage (Today)",
Expand Down
26 changes: 19 additions & 7 deletions custom_components/openevse/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

import logging
from typing import Any
from datetime import timedelta

from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt as dt_util

from .const import CONF_NAME, COORDINATOR, DOMAIN, SENSOR_TYPES

Expand Down Expand Up @@ -71,20 +77,26 @@ def native_value(self) -> Any:
if data is None:
self._state = None
if self._type in data.keys():
value = data[self._type]
if self._type == "charge_time_elapsed":
self._state = data[self._type] / 60
elif self._type == "usage_total" and isinstance(data[self._type], int):
self._state = data[self._type] / 1000
self._state = value / 60
elif self._type == "usage_total" and isinstance(value, int):
self._state = value / 1000
elif self._type in [
"usage_session",
"charging_current",
"charging_power",
]:
self._state = data[self._type] / 1000
self._state = value / 1000
elif self._type == "charging_voltage":
self._state = data[self._type]
self._state = value
elif self.device_class == SensorDeviceClass.TIMESTAMP:
if self._type == "vehicle_eta":
# Timestamp in the future
value = dt_util.utcnow() + timedelta(seconds=value)
self._state = value
else:
self._state = data[self._type]
self._state = value

_LOGGER.debug("Sensor [%s] updated value: %s", self._type, self._state)
self.update_icon()
Expand Down
32 changes: 31 additions & 1 deletion tests/test_sensor.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""Test openevse sensors."""

from datetime import timedelta
from unittest.mock import patch

import pytest
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.select import DOMAIN as SELECT_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.helpers import entity_registry as er
from homeassistant.util import dt as dt_util
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.openevse.const import DOMAIN
Expand All @@ -18,7 +21,9 @@
CHARGER_NAME = "openevse"


async def test_sensors(hass, test_charger, mock_ws_start):
async def test_sensors(
hass, test_charger, mock_ws_start, entity_registry: er.EntityRegistry
):
"""Test setup_entry."""
entry = MockConfigEntry(
domain=DOMAIN,
Expand Down Expand Up @@ -51,3 +56,28 @@ async def test_sensors(hass, test_charger, mock_ws_start):
state = hass.states.get("sensor.openevse_max_current")
assert state
assert state.state == "48"

# enable disabled sensor
entity_id = "sensor.openevse_vehicle_charge_completion_time"
entity_entry = entity_registry.async_get(entity_id)

assert entity_entry
assert entity_entry.disabled
assert entity_entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION

updated_entry = entity_registry.async_update_entity(
entity_entry.entity_id, disabled_by=None
)
assert updated_entry != entity_entry
assert updated_entry.disabled is False

# reload the integration
assert await hass.config_entries.async_forward_entry_unload(entry, "sensor")
await hass.config_entries.async_forward_entry_setups(entry, ["sensor"])
await hass.async_block_till_done()

state = hass.states.get("sensor.openevse_vehicle_charge_completion_time")
assert state
assert state.state == (dt_util.utcnow() + timedelta(seconds=18000)).isoformat(
timespec="seconds"
)

0 comments on commit 2a56826

Please sign in to comment.