From cfa821c99a1fbb1c09a3f6c2895ae56a6c6b80a3 Mon Sep 17 00:00:00 2001 From: Konstantin Pivnov Date: Thu, 22 Aug 2024 18:47:27 +0300 Subject: [PATCH] fixed old configs migrations --- custom_components/tion/client.py | 27 ++++++++++++--------------- custom_components/tion/config_flow.py | 15 ++++++++------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/custom_components/tion/client.py b/custom_components/tion/client.py index 8e3dce3..44e2391 100644 --- a/custom_components/tion/client.py +++ b/custom_components/tion/client.py @@ -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: @@ -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") @@ -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") @@ -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", [])] @@ -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", @@ -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): @@ -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, ) @@ -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, ) @@ -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, ) diff --git a/custom_components/tion/config_flow.py b/custom_components/tion/config_flow.py index f89c100..559b993 100644 --- a/custom_components/tion/config_flow.py +++ b/custom_components/tion/config_flow.py @@ -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() @@ -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, @@ -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,