Skip to content

Commit

Permalink
More pesky timezone fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan authored and nathanmarlor committed Mar 26, 2023
1 parent c4279e7 commit 21e1ac6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
28 changes: 18 additions & 10 deletions custom_components/foxess_em/average/average_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,26 @@ def average_peak_house_load(self) -> float:
"""House load peak"""
days = self._tracked_sensors["house_load_7d"].primary.period.days

eco_start = datetime.now().replace(
hour=self._eco_start_time.hour,
minute=self._eco_start_time.minute,
second=0,
microsecond=0,
eco_start = (
datetime.now()
.astimezone()
.replace(
hour=self._eco_start_time.hour,
minute=self._eco_start_time.minute,
second=0,
microsecond=0,
)
)
eco_start = eco_start.astimezone(pytz.utc).time()
eco_end = datetime.now().replace(
hour=self._eco_end_time.hour,
minute=self._eco_end_time.minute,
second=0,
microsecond=0,
eco_end = (
datetime.now()
.astimezone()
.replace(
hour=self._eco_end_time.hour,
minute=self._eco_end_time.minute,
second=0,
microsecond=0,
)
)
eco_end = eco_end.astimezone(pytz.utc).time()

Expand Down
19 changes: 12 additions & 7 deletions custom_components/foxess_em/battery/battery_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from custom_components.foxess_em.battery.battery_util import BatteryUtils
from custom_components.foxess_em.battery.schedule import Schedule
from custom_components.foxess_em.util.peak_period_util import PeakPeriodUtils
from dateutil import tz
from homeassistant.core import HomeAssistant

from ..util.exceptions import NoDataError
Expand Down Expand Up @@ -98,7 +99,11 @@ def refresh_battery_model(self, forecast: pd.DataFrame, load: pd.DataFrame) -> N
_, min_soc = self._charge_totals(load_forecast, now, battery)

for index, _ in future.iterrows():
period = load_forecast.iloc[index]["period_start"].to_pydatetime()
period = (
load_forecast.iloc[index]["period_start"]
.to_pydatetime()
.astimezone(tz.tzlocal())
)
if period.time() == self._eco_start_time:
# landed on the start of the eco period
boost = self._get_total_additional_charge(period)
Expand All @@ -122,7 +127,11 @@ def refresh_battery_model(self, forecast: pd.DataFrame, load: pd.DataFrame) -> N
load_forecast.at[index, "battery"] = battery

for index, _ in future.iterrows():
period = load_forecast.iloc[index]["period_start"].to_pydatetime()
period = (
load_forecast.iloc[index]["period_start"]
.to_pydatetime()
.astimezone(tz.tzlocal())
)
if period.time() == self._eco_start_time:
self._add_metadata(load_forecast, period)

Expand All @@ -138,11 +147,7 @@ def _charge_totals(
):
"""Return charge totals for dawn/day"""
# calculate start/end of the next peak period
now = datetime.now().astimezone()
eco_start = now.replace(
year=period.year,
month=period.month,
day=period.day,
eco_start = period.replace(
hour=self._eco_start_time.hour,
minute=self._eco_start_time.minute,
second=0,
Expand Down
5 changes: 4 additions & 1 deletion custom_components/foxess_em/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ async def async_get_events(
summary += f"Export: {str(round(values['export'], 2))} / "
calendar_events.append(
CalendarEvent(
key, values["eco_end"], f"FoxESS Charge: {charge}", summary
values["eco_start"],
values["eco_end"],
f"FoxESS Charge: {charge}",
summary,
)
)

Expand Down

0 comments on commit 21e1ac6

Please sign in to comment.