Skip to content

Commit

Permalink
Using local time for attributes instead of UTC time.
Browse files Browse the repository at this point in the history
This will resolve issue #5
  • Loading branch information
Erling Paulsen committed Feb 17, 2023
1 parent 0d374cc commit 471ae05
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
20 changes: 16 additions & 4 deletions custom_components/energytariff/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,14 @@ def __init__(self, hass, config, rx_coord: GridCapacityCoordinator):
self._effect_sensor_id = config.get(CONF_EFFECT_ENTITY)
self._coordinator = rx_coord
self._state = None
self._last_update = dt.now()
# self._last_update = dt.as_local(dt.now)
self._attr_unique_id = (
f"{DOMAIN}_{self._effect_sensor_id}_grid_effect_threshold_kwh".replace(
"sensor.", ""
)
)

self.attr = {"top_three": [], "level_name": "Unknown", "price": "Unknown"}
self.attr = {"top_three": [], "month": dt.as_local(dt.now()).month}

self._levels = config.get(GRID_LEVELS)

Expand All @@ -313,6 +313,8 @@ async def async_added_to_hass(self) -> None:
if savedstate:
if savedstate.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
self._state = float(savedstate.state)
if "month" in savedstate.attributes:
self.attr["month"] = int(savedstate.attributes["month"])
if "top_three" in savedstate.attributes:
for item in savedstate.attributes["top_three"]:
self.attr["top_three"].append(
Expand All @@ -325,9 +327,12 @@ async def async_added_to_hass(self) -> None:
self.calculate_level()

def _state_change(self, state: EnergyData) -> None:

if state is None:
return
if int(self.attr["month"]) != dt.as_local(state.timestamp).month:
self.attr["top_three"].clear()

self.attr["month"] = dt.as_local(dt.now()).month
self.attr["top_three"] = calculate_top_three(state, self.attr["top_three"])
self.calculate_level()

Expand Down Expand Up @@ -419,7 +424,7 @@ def __init__(self, hass, config, rx_coord: GridCapacityCoordinator):
)
)

self.attr = {"top_three": []}
self.attr = {"top_three": [], "month": dt.as_local(dt.now()).month}

self._levels = config.get(GRID_LEVELS)

Expand All @@ -432,6 +437,8 @@ async def async_added_to_hass(self) -> None:
if savedstate:
if savedstate.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
self._state = float(savedstate.state)
if "month" in savedstate.attributes:
self.attr["month"] = int(savedstate.attributes["month"])
if "top_three" in savedstate.attributes:
for item in savedstate.attributes["top_three"]:
self.attr["top_three"].append(
Expand All @@ -445,6 +452,11 @@ async def async_added_to_hass(self) -> None:
def _state_change(self, state: EnergyData) -> None:
if state is None:
return

if int(self.attr["month"]) != dt.as_local(state.timestamp).month:
self.attr["top_three"].clear()

self.attr["month"] = dt.as_local(dt.now()).month
self.attr["top_three"] = calculate_top_three(state, self.attr["top_three"])

totalSum = float(0)
Expand Down
8 changes: 6 additions & 2 deletions custom_components/energytariff/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from datetime import datetime, timedelta
from typing import Any

from homeassistant.util import dt

from homeassistant.const import (
STATE_UNAVAILABLE,
STATE_UNKNOWN,
Expand Down Expand Up @@ -75,9 +77,11 @@ def calculate_top_three(state: EnergyData, top_three: Any) -> Any:
if state is None:
return top_three

localtime = dt.as_local(state.timestamp)

consumption = {
"day": state.timestamp.day,
"hour": state.timestamp.hour,
"day": localtime.day,
"hour": localtime.hour,
"energy": state.energy_consumed,
}

Expand Down

0 comments on commit 471ae05

Please sign in to comment.