Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #277 API using a blocking function - Warning #279

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions custom_components/loxone/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,22 @@ async def get_json(self):
return self.responsecode


async def async_open_file(filename: str) -> dict:
def sync_open_file(f):
with open(f) as f:
return json.load(f)

return await asyncio.to_thread(sync_open_file, filename)


async def async_write_file(filename: str, data: dict) -> None:
def sync_write_file(f, d):
with open(f, "w") as write_file:
json.dump(d, write_file)

return await asyncio.to_thread(sync_write_file, filename, data)


class LoxWs:
def __init__(
self,
Expand Down Expand Up @@ -737,27 +753,25 @@ async def acquire_token(self):
key_and_salt.hash_alg,
)

if self.save_token() == ERROR_VALUE:
if await self.save_token() == ERROR_VALUE:
return ERROR_VALUE
return True

def load_token(self):
async def load_token(self):
try:
persist_token = os.path.join(
get_default_config_dir(), self._token_persist_filename
)
try:
with open(persist_token) as f:
try:
dict_token = json.load(f)
except ValueError:
return ERROR_VALUE
try:
dict_token = await async_open_file(persist_token)
except ValueError:
return ERROR_VALUE
except FileNotFoundError:
with open(self._token_persist_filename) as f:
try:
dict_token = json.load(f)
except ValueError:
return ERROR_VALUE
try:
dict_token = await async_open_file(self._token_persist_filename)
except ValueError:
return ERROR_VALUE
try:
self._token.set_token(dict_token["_token"])
self._token.set_valid_until(dict_token["_valid_until"])
Expand Down Expand Up @@ -787,7 +801,7 @@ def delete_token(self):
_LOGGER.debug("error deleting token...")
return ERROR_VALUE

def save_token(self):
async def save_token(self):
persist_token = ""
try:
persist_token = os.path.join(
Expand All @@ -800,11 +814,9 @@ def save_token(self):
"_hash_alg": self._token.hash_alg,
}
try:
with open(persist_token, "w") as write_file:
json.dump(dict_token, write_file)
await async_write_file(persist_token, dict_token)
except FileNotFoundError:
with open(self._token_persist_filename, "w") as write_file:
json.dump(dict_token, write_file)
await async_write_file(self._token_persist_filename, dict_token)

_LOGGER.debug("save_token successfully...")
return True
Expand Down Expand Up @@ -979,7 +991,8 @@ async def get_token_from_file(self):
get_default_config_dir(), self._token_persist_filename
)
if os.path.exists(persist_token):
if self.load_token():
res = await self.load_token()
if res:
_LOGGER.debug(
"token successfully loaded from file: {}".format(persist_token)
)
Expand Down
Loading