Skip to content

Commit af0c273

Browse files
committed
Ensure file and folder creation
1 parent c13937c commit af0c273

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

custom_components/bluecon/ConfigFolderNotificationInfoStorage.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
from typing import Any
55
from .const import DOMAIN
66
import json
7+
from pathlib import Path
78

89
class ConfigFolderNotificationInfoStorage(INotificationInfoStorage):
910
def __init__(self, hass: HomeAssistant, entry: ConfigEntry):
10-
self.__credentialsFileName = hass.config.path(f'.{DOMAIN}', entry.entry_id, 'credentials.json')
11-
self.__persistentIdsFileName = hass.config.path(f'.{DOMAIN}', entry.entry_id, 'persistent_ids.txt')
11+
self.__credentialsFileName = Path(hass.config.path(f'.{DOMAIN}', entry.entry_id, 'credentials.json'))
12+
self.__persistentIdsFileName = Path(hass.config.path(f'.{DOMAIN}', entry.entry_id, 'persistent_ids.txt'))
13+
14+
self.__credentialsFileName.mkdir(parents=True, exist_ok=True)
15+
self.__credentialsFileName.touch(exist_ok=True)
16+
self.__persistentIdsFileName.mkdir(parents=True, exist_ok=True)
17+
self.__persistentIdsFileName.touch(exist_ok=True)
1218

1319
def retrieveCredentials(self) -> dict[str, dict[str, Any]] | None:
1420
try:
@@ -18,7 +24,7 @@ def retrieveCredentials(self) -> dict[str, dict[str, Any]] | None:
1824
return None
1925

2026
def storeCredentials(self, credentials: dict[str, dict[str, Any]]):
21-
with open(self.__credentialsFileName, "w+") as f:
27+
with open(self.__credentialsFileName, "w") as f:
2228
json.dump(credentials, f)
2329

2430
def retrievePersistentIds(self) -> list[str] | None:
@@ -29,5 +35,5 @@ def retrievePersistentIds(self) -> list[str] | None:
2935
return None
3036

3137
def storePersistentId(self, persistentId: str):
32-
with open(self.__persistentIdsFileName, "a+") as f:
38+
with open(self.__persistentIdsFileName, "a") as f:
3339
f.write(persistentId + "\n")

custom_components/bluecon/ConfigFolderOAuthTokenStorage.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
from homeassistant.config_entries import ConfigEntry
33
from homeassistant.core import HomeAssistant
44
from .const import DOMAIN
5+
from pathlib import Path
56

67
class ConfigFolderOAuthTokenStorage(IOAuthTokenStorage):
78
def __init__(self, hass: HomeAssistant, entry: ConfigEntry):
8-
self.__oAuthTokenFileName = hass.config.path(f'.{DOMAIN}', entry.entry_id, 'oauth_token.json')
9+
self.__oAuthTokenFileName = Path(hass.config.path(f'.{DOMAIN}', entry.entry_id, 'oauth_token.json'))
10+
11+
self.__oAuthTokenFileName.mkdir(parents=True, exist_ok=True)
12+
self.__oAuthTokenFileName.touch(exist_ok=True)
913

1014
def retrieveOAuthToken(self) -> OAuthToken:
1115
with open(self.__oAuthTokenFileName, "r") as f:
1216
return OAuthToken.fromJson(f)
1317

1418
def storeOAuthToken(self, oAuthToken: OAuthToken):
15-
with open(self.__oAuthTokenFileName, "w+") as f:
19+
with open(self.__oAuthTokenFileName, "w") as f:
1620
f.write(oAuthToken.toJson())

custom_components/bluecon/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"domain": "bluecon",
33
"name": "Fermax Blue",
4-
"version": "0.1.12",
4+
"version": "0.1.13",
55
"documentation": "https://hass-bluecon.afonsogarcia.dev/",
66
"issue_tracker": "https://github.com/AfonsoFGarcia/hass-bluecon/issues",
77
"requirements": ["bluecon==0.1.2", "aiohttp", "oscrypto", "protobuf", "http-ece"],

0 commit comments

Comments
 (0)