Skip to content

Commit

Permalink
Use timestamp device class
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Brown <[email protected]>
  • Loading branch information
zestysoft committed Nov 29, 2024
1 parent 39a49d1 commit bd8741c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion custom_components/sensus_analytics/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "sensus_analytics",
"name": "Sensus Analytics Integration",
"version": "1.4.3",
"version": "1.4.4",
"documentation": "https://github.com/zestysoft/sensus_analytics_integration",
"dependencies": [],
"codeowners": ["@zestysoft"],
Expand Down
35 changes: 27 additions & 8 deletions custom_components/sensus_analytics/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DEVICE_CLASS_TIMESTAMP
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Expand Down Expand Up @@ -138,23 +139,32 @@ def native_value(self):
return self.coordinator.data.get("meterAddress1")


class SensusAnalyticsLastReadSensor(StaticUnitSensorBase):
class SensusAnalyticsLastReadSensor(CoordinatorEntity, SensorEntity):
"""Representation of the last read timestamp sensor."""

def __init__(self, coordinator, entry):
"""Initialize the last read sensor."""
super().__init__(coordinator, entry, unit="UTC")
super().__init__(coordinator)
self.coordinator = coordinator
self.entry = entry
self._unique_id = f"{DOMAIN}_{entry.entry_id}_last_read"
self._attr_name = f"{DEFAULT_NAME} Last Read"
self._attr_unique_id = f"{self._unique_id}_last_read"
self._attr_icon = "mdi:clock-time-nine"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, entry.entry_id)},
name=DEFAULT_NAME,
manufacturer="Unknown",
model="Water Meter",
)
self._attr_device_class = DEVICE_CLASS_TIMESTAMP

@property
def native_value(self):
"""Return the state of the sensor."""
last_read_ts = self.coordinator.data.get("lastRead")
if last_read_ts:
# Convert milliseconds to seconds for timestamp
return dt_util.utc_from_timestamp(last_read_ts / 1000).strftime("%Y-%m-%d %H:%M:%S")
return dt_util.utc_from_timestamp(last_read_ts / 1000)
return None


Expand Down Expand Up @@ -223,23 +233,32 @@ def native_value(self):
return self._convert_usage(latest_read_usage)


class SensusAnalyticsLatestReadTimeSensor(StaticUnitSensorBase):
class SensusAnalyticsLatestReadTimeSensor(CoordinatorEntity, SensorEntity):
"""Representation of the latest read time sensor."""

def __init__(self, coordinator, entry):
"""Initialize the latest read time sensor."""
super().__init__(coordinator, entry, unit="UTC")
super().__init__(coordinator)
self.coordinator = coordinator
self.entry = entry
self._unique_id = f"{DOMAIN}_{entry.entry_id}_latest_read_time"
self._attr_name = f"{DEFAULT_NAME} Latest Read Time"
self._attr_unique_id = f"{self._unique_id}_latest_read_time"
self._attr_icon = "mdi:clock-time-nine"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, entry.entry_id)},
name=DEFAULT_NAME,
manufacturer="Unknown",
model="Water Meter",
)
self._attr_device_class = DEVICE_CLASS_TIMESTAMP

@property
def native_value(self):
"""Return the state of the sensor."""
latest_read_time_ts = self.coordinator.data.get("latestReadTime")
if latest_read_time_ts:
# Convert milliseconds to seconds for timestamp
return dt_util.utc_from_timestamp(latest_read_time_ts / 1000).strftime("%Y-%m-%d %H:%M:%S")
return dt_util.utc_from_timestamp(latest_read_time_ts / 1000)
return None


Expand Down

0 comments on commit bd8741c

Please sign in to comment.