Skip to content

Commit

Permalink
handling for heatpump timezone being different than client timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
hashtagKnorke committed Dec 19, 2024
1 parent 3df5d21 commit 83c7876
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ThermiaOnlineAPI/model/HeatPump.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,10 @@ def is_online(self):
@property
def last_online(self):
return get_dict_value_or_none(self.__info, "lastOnline")

@property
def installation_timezone(self) -> str:
return get_dict_value_or_none(self.__info, "timeZoneId")

@property
def model(self):
Expand Down
171 changes: 171 additions & 0 deletions ThermiaOnlineAPI/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,184 @@
import random
import string
from typing import Any, TypeVar
import pytz
from datetime import datetime

T = TypeVar("T")


_LOGGER = logging.getLogger(__name__)



# Mapping of Windows time zone names to pytz time zones
windows_to_pytz = {
"Dateline Standard Time": "Etc/GMT+12",
"UTC-11": "Etc/GMT+11",
"Aleutian Standard Time": "America/Adak",
"Hawaiian Standard Time": "Pacific/Honolulu",
"Marquesas Standard Time": "Pacific/Marquesas",
"Alaskan Standard Time": "America/Anchorage",
"UTC-09": "Etc/GMT+9",
"Pacific Standard Time (Mexico)": "America/Tijuana",
"UTC-08": "Etc/GMT+8",
"Pacific Standard Time": "America/Los_Angeles",
"US Mountain Standard Time": "America/Phoenix",
"Mountain Standard Time (Mexico)": "America/Chihuahua",
"Mountain Standard Time": "America/Denver",
"Yukon Standard Time": "America/Whitehorse",
"Central America Standard Time": "America/Guatemala",
"Central Standard Time": "America/Chicago",
"Easter Island Standard Time": "Pacific/Easter",
"Central Standard Time (Mexico)": "America/Mexico_City",
"Canada Central Standard Time": "America/Regina",
"SA Pacific Standard Time": "America/Bogota",
"Eastern Standard Time (Mexico)": "America/Cancun",
"Eastern Standard Time": "America/New_York",
"Haiti Standard Time": "America/Port-au-Prince",
"Cuba Standard Time": "America/Havana",
"US Eastern Standard Time": "America/Indiana/Indianapolis",
"Turks And Caicos Standard Time": "America/Grand_Turk",
"Paraguay Standard Time": "America/Asuncion",
"Atlantic Standard Time": "America/Halifax",
"Venezuela Standard Time": "America/Caracas",
"Central Brazilian Standard Time": "America/Cuiaba",
"SA Western Standard Time": "America/La_Paz",
"Pacific SA Standard Time": "America/Santiago",
"Newfoundland Standard Time": "America/St_Johns",
"Tocantins Standard Time": "America/Araguaina",
"E. South America Standard Time": "America/Sao_Paulo",
"SA Eastern Standard Time": "America/Cayenne",
"Argentina Standard Time": "America/Argentina/Buenos_Aires",
"Montevideo Standard Time": "America/Montevideo",
"Magallanes Standard Time": "America/Punta_Arenas",
"Saint Pierre Standard Time": "America/Miquelon",
"Bahia Standard Time": "America/Bahia",
"UTC-02": "Etc/GMT+2",
"Greenland Standard Time": "America/Godthab",
"Mid-Atlantic Standard Time": "Etc/GMT+2",
"Azores Standard Time": "Atlantic/Azores",
"Cape Verde Standard Time": "Atlantic/Cape_Verde",
"UTC": "Etc/UTC",
"GMT Standard Time": "Europe/London",
"Greenwich Standard Time": "Atlantic/Reykjavik",
"Sao Tome Standard Time": "Africa/Sao_Tome",
"Morocco Standard Time": "Africa/Casablanca",
"W. Europe Standard Time": "Europe/Berlin",
"Central Europe Standard Time": "Europe/Budapest",
"Romance Standard Time": "Europe/Paris",
"Central European Standard Time": "Europe/Warsaw",
"W. Central Africa Standard Time": "Africa/Lagos",
"GTB Standard Time": "Europe/Bucharest",
"Middle East Standard Time": "Asia/Beirut",
"Egypt Standard Time": "Africa/Cairo",
"E. Europe Standard Time": "Europe/Chisinau",
"West Bank Standard Time": "Asia/Gaza",
"South Africa Standard Time": "Africa/Johannesburg",
"FLE Standard Time": "Europe/Kiev",
"Israel Standard Time": "Asia/Jerusalem",
"South Sudan Standard Time": "Africa/Juba",
"Kaliningrad Standard Time": "Europe/Kaliningrad",
"Sudan Standard Time": "Africa/Khartoum",
"Libya Standard Time": "Africa/Tripoli",
"Namibia Standard Time": "Africa/Windhoek",
"Jordan Standard Time": "Asia/Amman",
"Arabic Standard Time": "Asia/Baghdad",
"Syria Standard Time": "Asia/Damascus",
"Turkey Standard Time": "Europe/Istanbul",
"Arab Standard Time": "Asia/Kuwait",
"Belarus Standard Time": "Europe/Minsk",
"Russian Standard Time": "Europe/Moscow",
"E. Africa Standard Time": "Africa/Nairobi",
"Volgograd Standard Time": "Europe/Volgograd",
"Iran Standard Time": "Asia/Tehran",
"Arabian Standard Time": "Asia/Dubai",
"Astrakhan Standard Time": "Europe/Astrakhan",
"Azerbaijan Standard Time": "Asia/Baku",
"Russia Time Zone 3": "Europe/Samara",
"Mauritius Standard Time": "Indian/Mauritius",
"Saratov Standard Time": "Europe/Saratov",
"Georgian Standard Time": "Asia/Tbilisi",
"Caucasus Standard Time": "Asia/Yerevan",
"Afghanistan Standard Time": "Asia/Kabul",
"West Asia Standard Time": "Asia/Tashkent",
"Qyzylorda Standard Time": "Asia/Qyzylorda",
"Ekaterinburg Standard Time": "Asia/Yekaterinburg",
"Pakistan Standard Time": "Asia/Karachi",
"India Standard Time": "Asia/Kolkata",
"Sri Lanka Standard Time": "Asia/Colombo",
"Nepal Standard Time": "Asia/Kathmandu",
"Central Asia Standard Time": "Asia/Almaty",
"Bangladesh Standard Time": "Asia/Dhaka",
"Omsk Standard Time": "Asia/Omsk",
"Myanmar Standard Time": "Asia/Yangon",
"SE Asia Standard Time": "Asia/Bangkok",
"Altai Standard Time": "Asia/Barnaul",
"W. Mongolia Standard Time": "Asia/Hovd",
"North Asia Standard Time": "Asia/Krasnoyarsk",
"N. Central Asia Standard Time": "Asia/Novosibirsk",
"Tomsk Standard Time": "Asia/Tomsk",
"China Standard Time": "Asia/Shanghai",
"North Asia East Standard Time": "Asia/Irkutsk",
"Singapore Standard Time": "Asia/Singapore",
"W. Australia Standard Time": "Australia/Perth",
"Taipei Standard Time": "Asia/Taipei",
"Ulaanbaatar Standard Time": "Asia/Ulaanbaatar",
"Aus Central W. Standard Time": "Australia/Eucla",
"Transbaikal Standard Time": "Asia/Chita",
"Tokyo Standard Time": "Asia/Tokyo",
"North Korea Standard Time": "Asia/Pyongyang",
"Korea Standard Time": "Asia/Seoul",
"Yakutsk Standard Time": "Asia/Yakutsk",
"Cen. Australia Standard Time": "Australia/Adelaide",
"AUS Central Standard Time": "Australia/Darwin",
"E. Australia Standard Time": "Australia/Brisbane",
"AUS Eastern Standard Time": "Australia/Sydney",
"West Pacific Standard Time": "Pacific/Port_Moresby",
"Tasmania Standard Time": "Australia/Hobart",
"Vladivostok Standard Time": "Asia/Vladivostok",
"Lord Howe Standard Time": "Australia/Lord_Howe",
"Bougainville Standard Time": "Pacific/Bougainville",
"Russia Time Zone 10": "Asia/Srednekolymsk",
"Magadan Standard Time": "Asia/Magadan",
"Norfolk Standard Time": "Pacific/Norfolk",
"Sakhalin Standard Time": "Asia/Sakhalin",
"Central Pacific Standard Time": "Pacific/Guadalcanal",
"Russia Time Zone 11": "Asia/Kamchatka",
"New Zealand Standard Time": "Pacific/Auckland",
"UTC+12": "Etc/GMT-12",
"Fiji Standard Time": "Pacific/Fiji",
"Kamchatka Standard Time": "Asia/Kamchatka",
"Chatham Islands Standard Time": "Pacific/Chatham",
"UTC+13": "Etc/GMT-13",
"Tonga Standard Time": "Pacific/Tongatapu",
"Samoa Standard Time": "Pacific/Apia",
"Line Islands Standard Time": "Pacific/Kiritimati"
}

def get_pytz_timezone(windows_tz_name):
"""
Get the corresponding pytz time zone for a given Windows time zone name.
Args:
windows_tz_name (str): The Windows time zone name.
Returns:
str: The corresponding pytz time zone name.
"""
return windows_to_pytz.get(windows_tz_name)

def adjust_times_for_timezone(timestamp: datetime, time_zone_name: str) -> datetime:

pytz_tz_name = get_pytz_timezone(time_zone_name)
if pytz_tz_name:
heatpump_timezone = pytz.timezone(pytz_tz_name)
timestamp = timestamp.astimezone(heatpump_timezone)
return timestamp
else:
raise ValueError(f"No corresponding pytz time zone found for '{time_zone_name}'")


def get_dict_value_or_none(dictionary, key) -> Any:
if dictionary is None or key not in dictionary:
return None
Expand Down

0 comments on commit 83c7876

Please sign in to comment.