Skip to content

Commit

Permalink
Add formatted on_time value
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marc Collin committed Sep 30, 2024
1 parent 96ac8b4 commit 407863f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions custom_components/solar_optimizer/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down
5 changes: 5 additions & 0 deletions custom_components/solar_optimizer/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
get_tz,
name_to_unique_id,
DEVICE_MODEL,
seconds_to_hms,
)
from .coordinator import SolarOptimizerCoordinator

Expand Down Expand Up @@ -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",
}
)
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 407863f

Please sign in to comment.