pyhOn/pyhon/hon.py

139 lines
4.4 KiB
Python
Raw Normal View History

2023-04-09 20:50:28 +02:00
import asyncio
import logging
2023-06-25 17:29:04 +02:00
from pathlib import Path
2023-04-16 01:36:10 +02:00
from types import TracebackType
2024-03-29 01:10:27 +01:00
from typing import List, Optional, Dict, Any, Type, Callable
2023-04-09 20:50:28 +02:00
2023-04-13 23:25:49 +02:00
from aiohttp import ClientSession
2023-04-15 23:02:37 +02:00
from typing_extensions import Self
2023-04-13 23:25:49 +02:00
2023-04-09 20:50:28 +02:00
from pyhon.appliance import HonAppliance
2023-07-16 05:53:23 +02:00
from pyhon.connection.api import HonAPI
2023-06-25 17:29:04 +02:00
from pyhon.connection.api import TestAPI
2024-03-29 01:10:27 +01:00
from pyhon.connection.mqtt import MQTTClient
2023-07-16 05:53:23 +02:00
from pyhon.exceptions import NoAuthenticationException
2023-04-09 20:50:28 +02:00
_LOGGER = logging.getLogger(__name__)
2023-04-09 20:50:28 +02:00
2024-03-29 13:21:49 +01:00
# pylint: disable=too-many-instance-attributes
2023-04-09 20:50:28 +02:00
class Hon:
2023-05-11 00:43:48 +02:00
def __init__(
self,
email: Optional[str] = "",
password: Optional[str] = "",
session: Optional[ClientSession] = None,
2024-02-09 20:37:53 +01:00
mobile_id: str = "",
2024-02-10 00:59:14 +01:00
refresh_token: str = "",
2023-06-25 17:29:04 +02:00
test_data_path: Optional[Path] = None,
2023-05-11 00:43:48 +02:00
):
self._email: Optional[str] = email
self._password: Optional[str] = password
2023-04-13 23:25:49 +02:00
self._session: ClientSession | None = session
self._appliances: List[HonAppliance] = []
self._api: Optional[HonAPI] = None
2023-06-25 17:29:04 +02:00
self._test_data_path: Path = test_data_path or Path().cwd()
2024-02-09 20:37:53 +01:00
self._mobile_id: str = mobile_id
2024-02-10 00:59:14 +01:00
self._refresh_token: str = refresh_token
2024-03-29 01:10:27 +01:00
self._mqtt_client: MQTTClient | None = None
self._notify_function: Optional[Callable[[Any], None]] = None
2023-04-09 20:50:28 +02:00
2023-04-16 01:36:10 +02:00
async def __aenter__(self) -> Self:
2023-04-10 06:34:19 +02:00
return await self.create()
2023-04-09 20:50:28 +02:00
2023-04-16 01:36:10 +02:00
async def __aexit__(
self,
exc_type: Optional[Type[BaseException]],
exc: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None:
2023-04-10 06:34:19 +02:00
await self.close()
2023-04-13 23:25:49 +02:00
@property
def api(self) -> HonAPI:
if self._api is None:
2023-07-16 05:53:23 +02:00
raise NoAuthenticationException
2023-04-13 23:25:49 +02:00
return self._api
2023-05-11 00:43:48 +02:00
@property
def email(self) -> str:
if not self._email:
raise ValueError("Missing email")
return self._email
@property
def password(self) -> str:
if not self._password:
raise ValueError("Missing password")
return self._password
2023-04-13 23:25:49 +02:00
async def create(self) -> Self:
2023-04-10 06:34:19 +02:00
self._api = await HonAPI(
2024-02-10 00:59:14 +01:00
self.email,
self.password,
session=self._session,
mobile_id=self._mobile_id,
refresh_token=self._refresh_token,
2023-04-10 06:34:19 +02:00
).create()
await self.setup()
return self
2023-04-09 20:50:28 +02:00
@property
def appliances(self) -> List[HonAppliance]:
return self._appliances
2023-05-11 00:43:48 +02:00
@appliances.setter
2023-06-28 19:02:11 +02:00
def appliances(self, appliances: List[HonAppliance]) -> None:
2023-05-11 00:43:48 +02:00
self._appliances = appliances
2023-06-25 17:29:04 +02:00
async def _create_appliance(
2023-06-28 19:02:11 +02:00
self, appliance_data: Dict[str, Any], api: HonAPI, zone: int = 0
2023-06-25 17:29:04 +02:00
) -> None:
appliance = HonAppliance(api, appliance_data, zone=zone)
2023-04-24 04:33:00 +02:00
if appliance.mac_address == "":
2023-04-15 04:12:38 +02:00
return
try:
await asyncio.gather(
*[
appliance.load_attributes(),
appliance.load_commands(),
appliance.load_statistics(),
]
)
except (KeyError, ValueError, IndexError) as error:
_LOGGER.exception(error)
2023-05-19 00:48:08 +02:00
_LOGGER.error("Device data - %s", appliance_data)
2023-04-15 04:12:38 +02:00
self._appliances.append(appliance)
2023-04-16 01:36:10 +02:00
async def setup(self) -> None:
2023-06-25 17:29:04 +02:00
appliances = await self.api.load_appliances()
for appliance in appliances:
if (zones := int(appliance.get("zone", "0"))) > 1:
for zone in range(zones):
2023-06-25 17:29:04 +02:00
await self._create_appliance(
appliance.copy(), self.api, zone=zone + 1
)
await self._create_appliance(appliance, self.api)
if (
2024-02-04 04:21:27 +01:00
self._test_data_path
2024-02-09 20:33:50 +01:00
and (
test_data := self._test_data_path / "hon-test-data" / "test_data"
).exists()
2024-02-04 04:21:27 +01:00
or (test_data := test_data / "..").exists()
):
2023-06-25 17:29:04 +02:00
api = TestAPI(test_data)
for appliance in await api.load_appliances():
await self._create_appliance(appliance, api)
2024-03-29 01:10:27 +01:00
if not self._mqtt_client:
2024-03-29 13:21:49 +01:00
self._mqtt_client = await MQTTClient(self, self._mobile_id).create()
2024-03-29 01:10:27 +01:00
def subscribe_updates(self, notify_function: Callable[[Any], None]) -> None:
self._notify_function = notify_function
def notify(self) -> None:
if self._notify_function:
self._notify_function(None)
2023-04-10 06:34:19 +02:00
2023-04-16 01:36:10 +02:00
async def close(self) -> None:
await self.api.close()