pyh0n/pyhon/hon.py

114 lines
3.5 KiB
Python
Raw Normal View History

2023-04-10 00:20:28 +05:30
import asyncio
import logging
2023-06-25 20:59:04 +05:30
from pathlib import Path
2023-04-16 05:06:10 +05:30
from types import TracebackType
from typing import List, Optional, Dict, Any, Type
2023-04-10 00:20:28 +05:30
2023-04-14 02:55:49 +05:30
from aiohttp import ClientSession
2023-04-16 02:32:37 +05:30
from typing_extensions import Self
2023-04-14 02:55:49 +05:30
2023-04-10 00:20:28 +05:30
from pyhon.appliance import HonAppliance
2023-07-16 09:23:23 +05:30
from pyhon.connection.api import HonAPI
2023-06-25 20:59:04 +05:30
from pyhon.connection.api import TestAPI
2023-07-16 09:23:23 +05:30
from pyhon.exceptions import NoAuthenticationException
2023-04-10 00:20:28 +05:30
_LOGGER = logging.getLogger(__name__)
2023-04-10 00:20:28 +05:30
class Hon:
2023-05-11 04:13:48 +05:30
def __init__(
self,
email: Optional[str] = "",
password: Optional[str] = "",
session: Optional[ClientSession] = None,
2023-06-25 20:59:04 +05:30
test_data_path: Optional[Path] = None,
2023-05-11 04:13:48 +05:30
):
self._email: Optional[str] = email
self._password: Optional[str] = password
2023-04-14 02:55:49 +05:30
self._session: ClientSession | None = session
self._appliances: List[HonAppliance] = []
self._api: Optional[HonAPI] = None
2023-06-25 20:59:04 +05:30
self._test_data_path: Path = test_data_path or Path().cwd()
2023-04-10 00:20:28 +05:30
2023-04-16 05:06:10 +05:30
async def __aenter__(self) -> Self:
2023-04-10 10:04:19 +05:30
return await self.create()
2023-04-10 00:20:28 +05:30
2023-04-16 05:06:10 +05:30
async def __aexit__(
self,
exc_type: Optional[Type[BaseException]],
exc: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None:
2023-04-10 10:04:19 +05:30
await self.close()
2023-04-14 02:55:49 +05:30
@property
def api(self) -> HonAPI:
if self._api is None:
2023-07-16 09:23:23 +05:30
raise NoAuthenticationException
2023-04-14 02:55:49 +05:30
return self._api
2023-05-11 04:13:48 +05:30
@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-14 02:55:49 +05:30
async def create(self) -> Self:
2023-04-10 10:04:19 +05:30
self._api = await HonAPI(
2023-05-11 04:13:48 +05:30
self.email, self.password, session=self._session
2023-04-10 10:04:19 +05:30
).create()
await self.setup()
return self
2023-04-10 00:20:28 +05:30
@property
def appliances(self) -> List[HonAppliance]:
return self._appliances
2023-05-11 04:13:48 +05:30
@appliances.setter
2023-06-28 22:32:11 +05:30
def appliances(self, appliances: List[HonAppliance]) -> None:
2023-05-11 04:13:48 +05:30
self._appliances = appliances
2023-06-25 20:59:04 +05:30
async def _create_appliance(
2023-06-28 22:32:11 +05:30
self, appliance_data: Dict[str, Any], api: HonAPI, zone: int = 0
2023-06-25 20:59:04 +05:30
) -> None:
appliance = HonAppliance(api, appliance_data, zone=zone)
2023-04-24 08:03:00 +05:30
if appliance.mac_address == "":
2023-04-15 07:42:38 +05:30
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 04:18:08 +05:30
_LOGGER.error("Device data - %s", appliance_data)
2023-04-15 07:42:38 +05:30
self._appliances.append(appliance)
2023-04-16 05:06:10 +05:30
async def setup(self) -> None:
2023-06-25 20:59:04 +05:30
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 20:59:04 +05:30
await self._create_appliance(
appliance.copy(), self.api, zone=zone + 1
)
await self._create_appliance(appliance, self.api)
if (
test_data := self._test_data_path / "hon-test-data" / "test_data"
).exists() or (test_data := test_data / "test_data").exists():
api = TestAPI(test_data)
for appliance in await api.load_appliances():
await self._create_appliance(appliance, api)
2023-04-10 10:04:19 +05:30
2023-04-16 05:06:10 +05:30
async def close(self) -> None:
await self.api.close()