Skip to content

Commit a01a6eb

Browse files
author
Mark Hale
committed
Add optional time zone configuration.
1 parent c3a256b commit a01a6eb

3 files changed

Lines changed: 28 additions & 17 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ using
4949

5050
`export_standing_charge`: sensor name or value (default 0).
5151

52+
`time_zone`: if you encounter time zone issues, you can either set this to an explicit time zone, e.g. `time_zone: "Europe/London"` or the name of a sensor that provides the time zone (e.g. https://community.home-assistant.io/t/how-to-obtain-ha-timezone-in-lovelace/393449/2)
53+
5254

5355
### Advanced
5456

src/apps/powerwall/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime as dt
2+
import zoneinfo
23
import re
34
import powerwall_tariff as tariff
45
import teslapy_wrapper as api_wrapper
@@ -197,11 +198,19 @@ def _update_powerwall_tariff():
197198
export_standing_charge = get_sensor_value(EXPORT_RATES.current_tariff, "export_standing_charge", 0)
198199
import_schedule_type = get_tariff_setting(IMPORT_RATES.current_tariff, "schedule_type", "week")
199200
export_schedule_type = get_tariff_setting(EXPORT_RATES.current_tariff, "schedule_type", "week")
201+
tz_config = config.get("time_zone")
202+
if tz_config:
203+
if tz_config.startswith("sensor."):
204+
tz_config = state.get(tz_config)
205+
tz = zoneinfo.ZoneInfo(tz_config)
206+
else:
207+
tz = None
208+
200209
tariff_data = tariff.to_tariff_data(
201210
config["tariff_provider"],
202211
import_plan, import_standing_charge, import_schedule_type,
203212
export_plan, export_standing_charge, export_schedule_type,
204-
WEEK_SCHEDULES, today
213+
WEEK_SCHEDULES, today, tz=tz
205214
)
206215

207216
debug(f"Tariff data:\n{json.dumps(tariff_data)}")

src/modules/powerwall_tariff.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,9 @@ def get_schedules(breaks_config, tariff_pricing_config, plunge_pricing_breaks_co
460460
return schedules
461461

462462

463-
def to_charge_period_json(start_day_of_week, end_day_of_week, period):
464-
start_local = period[0].astimezone()
465-
end_local = period[1].astimezone()
463+
def to_charge_period_json(start_day_of_week, end_day_of_week, period, tz):
464+
start_local = period[0].astimezone(tz)
465+
end_local = period[1].astimezone(tz)
466466
return {
467467
"fromDayOfWeek": start_day_of_week,
468468
"fromHour": start_local.hour,
@@ -473,20 +473,20 @@ def to_charge_period_json(start_day_of_week, end_day_of_week, period):
473473
}
474474

475475

476-
def populate_tou_periods(tou_periods, schedules, start_day_of_week, end_day_of_week):
476+
def populate_tou_periods(tou_periods, schedules, start_day_of_week, end_day_of_week, tz):
477477
for schedule in schedules:
478478
charge_periods = tou_periods[schedule.charge_name]
479479
for period in schedule.get_periods():
480-
charge_periods.append(to_charge_period_json(start_day_of_week, end_day_of_week, period))
480+
charge_periods.append(to_charge_period_json(start_day_of_week, end_day_of_week, period, tz))
481481

482482

483-
def schedules_to_tariff(week_schedules, schedule_type, weekday, export=False):
483+
def schedules_to_tariff(week_schedules, schedule_type, weekday, tz=None, export=False):
484484
tou_periods = defaultdict(list)
485485

486486
if schedule_type == "week":
487487
# week
488488
today_schedules = week_schedules.get_schedules(weekday, export)
489-
populate_tou_periods(tou_periods, today_schedules, 0, 6)
489+
populate_tou_periods(tou_periods, today_schedules, 0, 6, tz)
490490
elif schedule_type == "weekend":
491491
# midweek/weekend when possible
492492
if is_midweek(weekday):
@@ -505,12 +505,12 @@ def schedules_to_tariff(week_schedules, schedule_type, weekday, export=False):
505505
break
506506

507507
if midweek_schedules and weekend_schedules:
508-
populate_tou_periods(tou_periods, midweek_schedules, 0, 4)
509-
populate_tou_periods(tou_periods, weekend_schedules, 5, 6)
508+
populate_tou_periods(tou_periods, midweek_schedules, 0, 4, tz)
509+
populate_tou_periods(tou_periods, weekend_schedules, 5, 6, tz)
510510
elif midweek_schedules:
511-
populate_tou_periods(tou_periods, midweek_schedules, 0, 6)
511+
populate_tou_periods(tou_periods, midweek_schedules, 0, 6, tz)
512512
elif weekend_schedules:
513-
populate_tou_periods(tou_periods, weekend_schedules, 0, 6)
513+
populate_tou_periods(tou_periods, weekend_schedules, 0, 6, tz)
514514
else:
515515
raise ValueError("At least one schedule is required")
516516
elif schedule_type == "multiday":
@@ -520,11 +520,11 @@ def schedules_to_tariff(week_schedules, schedule_type, weekday, export=False):
520520
schedules = week_schedules.get_schedules(i, export)
521521
if schedules:
522522
if last_schedules:
523-
populate_tou_periods(tou_periods, last_schedules, start, i-1)
523+
populate_tou_periods(tou_periods, last_schedules, start, i-1, tz)
524524
start = i
525525
last_schedules = schedules
526526
if last_schedules:
527-
populate_tou_periods(tou_periods, last_schedules, start, 6)
527+
populate_tou_periods(tou_periods, last_schedules, start, 6, tz)
528528
else:
529529
raise ValueError(f"Invalid schedule type: {schedule_type}")
530530

@@ -542,15 +542,15 @@ def get_price_info(schedules):
542542
return price_info
543543

544544

545-
def to_tariff_data(provider_name, import_plan, import_standing_charge, import_schedule_type, export_plan, export_standing_charge, export_schedule_type, week_schedules, day_date):
545+
def to_tariff_data(provider_name, import_plan, import_standing_charge, import_schedule_type, export_plan, export_standing_charge, export_schedule_type, week_schedules, day_date, tz=None):
546546
weekday = day_date.weekday()
547547
current_import_schedules = week_schedules.get_schedules(weekday)
548548
current_export_schedules = week_schedules.get_schedules(weekday, export=True)
549-
import_seasons = schedules_to_tariff(week_schedules, import_schedule_type, weekday)
549+
import_seasons = schedules_to_tariff(week_schedules, import_schedule_type, weekday, tz=tz)
550550
buy_price_info = get_price_info(current_import_schedules);
551551

552552
if current_export_schedules:
553-
export_seasons = schedules_to_tariff(week_schedules, export_schedule_type, weekday, export=True)
553+
export_seasons = schedules_to_tariff(week_schedules, export_schedule_type, weekday, tz=tz, export=True)
554554
sell_price_info = get_price_info(current_export_schedules);
555555
else:
556556
export_seasons = import_seasons

0 commit comments

Comments
 (0)