From 407863fb62cefb9ecd6b3af93be946255ba4bfd3 Mon Sep 17 00:00:00 2001 From: Jean-Marc Collin Date: Mon, 30 Sep 2024 16:06:23 +0000 Subject: [PATCH] Add formatted on_time value --- custom_components/solar_optimizer/const.py | 11 +++++++++++ custom_components/solar_optimizer/sensor.py | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/custom_components/solar_optimizer/const.py b/custom_components/solar_optimizer/const.py index e064dee..6d38a06 100644 --- a/custom_components/solar_optimizer/const.py +++ b/custom_components/solar_optimizer/const.py @@ -38,6 +38,17 @@ def name_to_unique_id(name: str) -> str: return slugify(name).replace("-", "_") +def seconds_to_hms(seconds): + """Convert seconds to a formatted string of hours:minutes:seconds.""" + hours = seconds // 3600 + minutes = (seconds % 3600) // 60 + secs = seconds % 60 + if hours > 0: + return f"{hours}:{minutes:02d}:{secs:02d}" + else: + return f"{minutes}:{secs:02d}" + + class ConfigurationError(Exception): """An error in configuration""" diff --git a/custom_components/solar_optimizer/sensor.py b/custom_components/solar_optimizer/sensor.py index de6b197..8775f78 100644 --- a/custom_components/solar_optimizer/sensor.py +++ b/custom_components/solar_optimizer/sensor.py @@ -36,6 +36,7 @@ get_tz, name_to_unique_id, DEVICE_MODEL, + seconds_to_hms, ) from .coordinator import SolarOptimizerCoordinator @@ -155,6 +156,8 @@ class TodayOnTimeSensor(SensorEntity, RestoreEntity): { "max_on_time_per_day_sec", "max_on_time_per_day_min", + "max_on_time_hms", + "on_time_hms", } ) ) @@ -293,6 +296,8 @@ def update_custom_attributes(self): "last_datetime_on": self._last_datetime_on, "max_on_time_per_day_min": round(self._device.max_on_time_per_day_sec / 60), "max_on_time_per_day_sec": self._device.max_on_time_per_day_sec, + "on_time_hms": seconds_to_hms(self._attr_native_value), + "max_on_time_hms": seconds_to_hms(self._device.max_on_time_per_day_sec), } @property