Skip to content

Commit

Permalink
fixed old configs migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
vaproloff committed Aug 22, 2024
1 parent 8339e88 commit cfa821c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
27 changes: 12 additions & 15 deletions custom_components/tion/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ class TionZoneModeAutoSet:
def __init__(self, data: dict[str, Any]) -> None:
"""Tion zone mode auto set initialization."""
self.co2 = data.get("co2")
# self.temperature = data.get("temperature")
# self.humidity = data.get("humidity")
# self.noise = data.get("noise")
# self.pm25 = data.get("pm25")
# self.pm10 = data.get("pm10")


class TionZoneMode:
Expand All @@ -43,8 +38,6 @@ def __init__(self, data: dict[str, Any]) -> None:
self.temperature = data.get("temperature")
self.humidity = data.get("humidity")
self.pm25 = data.get("pm25")
# self.pm10 = data.get("pm10")
# self.pm1 = data.get("pm1")
self.backlight = data.get("backlight")
self.sound_is_on = data.get("sound_is_on")
self.is_on = data.get("is_on")
Expand Down Expand Up @@ -75,7 +68,6 @@ def __init__(self, data: dict[str, Any]) -> None:
self.name = data.get("name")
self.type = data.get("type")
self.mac = data.get("mac")
# self.is_online = data.get("is_online")
self.data = TionZoneDeviceData(data.get("data", {}))
self.firmware = data.get("firmware")
self.hardware = data.get("hardware")
Expand Down Expand Up @@ -117,7 +109,6 @@ def __init__(self, data: dict[str, Any]) -> None:
"""Tion location data initialization."""
self.guid = data.get("guid")
self.name = data.get("name")
# self.unique_key = data.get("unique_key")
self.zones = [TionZone(zone) for zone in data.get("zones", [])]


Expand Down Expand Up @@ -154,13 +145,13 @@ def __init__(
self._temp_lock = asyncio.Lock()

@property
def _headers(self):
async def _headers(self):
"""Return headers for API request."""
return {
"Accept": "application/json, text/plain, */*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "ru-RU",
"Authorization": self._authorization,
"Authorization": await self.authorization,
"Connection": "Keep-Alive",
"Content-Type": "application/json",
"Host": "api2.magicair.tion.ru",
Expand All @@ -170,8 +161,14 @@ def _headers(self):
}

@property
def authorization(self) -> str:
async def authorization(self) -> str:
"""Return authorization data."""
if self._authorization is None:
if await self._get_authorization():
return self._authorization

return None

return self._authorization

def add_update_listener(self, coro):
Expand Down Expand Up @@ -225,7 +222,7 @@ async def get_location_data(self, force=False) -> bool:

response = await self._session.get(
url=f"{self._API_ENDPOINT}{self._LOCATION_URL}",
headers=self._headers,
headers=await self._headers,
timeout=10,
)

Expand Down Expand Up @@ -340,7 +337,7 @@ async def _send(self, url: str, data: dict[str, Any]):
response = await self._session.post(
url=url,
json=data,
headers=self._headers,
headers=await self._headers,
timeout=10,
)

Expand Down Expand Up @@ -371,7 +368,7 @@ async def _wait_for_task(self, task_id: str, max_time: int = 5) -> bool:

response = await self._session.get(
url=f"{self._API_ENDPOINT}{self._TASK_URL}/{task_id}",
headers=self._headers,
headers=await self._headers,
timeout=10,
)

Expand Down
15 changes: 8 additions & 7 deletions custom_components/tion/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,24 @@ class TionConfigFlow(ConfigFlow, domain=DOMAIN):

VERSION = 1

def _check_auth(self, user, password, interval, auth_data=None) -> bool:
async def _get_auth_data(
self, user, password, interval, auth_data=None
) -> str | None:
session = async_create_clientsession(self.hass)
api = TionClient(
session, user, password, min_update_interval_sec=interval, auth=auth_data
)
return api.authorization
return await api.authorization

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Step user."""
self._async_abort_entries_match({CONF_USERNAME: user_input[CONF_USERNAME]})

errors: dict[str, str] = {}
if user_input is not None:
self._async_abort_entries_match({CONF_USERNAME: user_input[CONF_USERNAME]})

sha256_hash = hashlib.new("sha256")
sha256_hash.update(user_input[CONF_USERNAME].encode())
sha256_hex = sha256_hash.hexdigest()
Expand All @@ -50,8 +53,7 @@ async def async_step_user(
except TypeError:
interval = DEFAULT_SCAN_INTERVAL

auth_data = await self.hass.async_add_executor_job(
self._check_auth,
auth_data = await self._get_auth_data(
user_input[CONF_USERNAME],
user_input[CONF_PASSWORD],
interval,
Expand Down Expand Up @@ -110,8 +112,7 @@ async def async_step_import(
except TypeError:
interval = DEFAULT_SCAN_INTERVAL

auth_data = await self.hass.async_add_executor_job(
self._check_auth,
auth_data = await self._get_auth_data(
import_config[CONF_USERNAME],
import_config[CONF_PASSWORD],
interval,
Expand Down

0 comments on commit cfa821c

Please sign in to comment.