Skip to content

Commit

Permalink
Merge pull request #217 from sockless-coding/energy-1
Browse files Browse the repository at this point in the history
History Time Zones, Async Settings and Cookie Jars
  • Loading branch information
sockless-coding authored Jun 25, 2024
2 parents 702931e + 0fcb54d commit feaa3c0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
2 changes: 1 addition & 1 deletion custom_components/panasonic_cc/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"domain": "panasonic_cc",
"name": "Panasonic Comfort Cloud",
"after_dependencies": ["http"],
"version": "1.0.45",
"version": "1.0.46",
"config_flow": true,
"documentation": "https://github.com/sockless-coding/panasonic_cc/",
"dependencies": [],
Expand Down
58 changes: 38 additions & 20 deletions custom_components/panasonic_cc/pcomfortcloud/apiclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@
import hashlib
import re
import aiohttp
import time
from urllib.parse import quote_plus

from . import constants
from . import panasonicsession

_current_time_zone = None
def get_current_time_zone():
global _current_time_zone
if _current_time_zone is not None:
return _current_time_zone
local_offset_seconds = -time.timezone
if time.daylight:
local_offset_seconds += 3600
hours, remainder = divmod(abs(local_offset_seconds), 3600)
minutes = remainder // 60
_current_time_zone = f"{'+' if local_offset_seconds >= 0 else '-'}{int(hours):02}:{int(minutes):02}"
return _current_time_zone



class ApiClient(panasonicsession.PanasonicSession):
def __init__(self,
Expand Down Expand Up @@ -74,29 +89,32 @@ def dump(self, device_id):
return self.execute_get(self._get_device_status_url(device_guid), "dump", 200)
return None

async def history(self, device_id, mode, date, time_zone="+01:00"):
async def history(self, device_id, mode, date, time_zone=""):
device_guid = self._device_indexer.get(device_id)
if not device_guid:
return None
if not time_zone:
time_zone = get_current_time_zone()
try:
data_mode = constants.DataMode[mode].value
except KeyError:
raise Exception("Wrong mode parameter")

payload = {
"deviceGuid": device_guid,
"dataMode": data_mode,
"date": date,
"osTimezone": time_zone
}

if device_guid:
try:
data_mode = constants.DataMode[mode].value
except KeyError:
raise Exception("Wrong mode parameter")

payload = {
"deviceGuid": device_guid,
"dataMode": data_mode,
"date": date,
"osTimezone": time_zone
}

json_response = await self.execute_post(self._get_device_history_url(), payload, "history", 200)
json_response = await self.execute_post(self._get_device_history_url(), payload, "history", 200)

return {
'id': device_id,
'parameters': self._read_parameters(json_response)
}
return None
return {
'id': device_id,
'parameters': self._read_parameters(json_response)
}



async def get_device(self, device_id):
device_guid = self._device_indexer.get(device_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(self, client: aiohttp.ClientSession, settings: PanasonicSettings, a

async def authenticate(self, username: str, password: str):

self._client.cookie_jar.clear()
# generate initial state and code_challenge
code_verifier = generate_random_string(43)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(self, username, password, client: aiohttp.ClientSession, settingsFi

async def start_session(self):
_LOGGER.debug("Starting Session")
await self._settings.is_ready()
if (not self._settings.has_refresh_token):
await self._authentication.authenticate(self._username, self._password)
if (not self._settings.is_access_token_valid):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ def __init__(self, fileName):
self._refresh_token = None
self._scope = None
self._clientId = ""
asyncio.ensure_future(self._load())
self._loading_task =asyncio.ensure_future(self._load())

async def is_ready(self):
await self._loading_task
return True

async def _load(self):
if not os.path.exists(self._fileName):
Expand Down

0 comments on commit feaa3c0

Please sign in to comment.