pyhOn/pyhon/hon.py

92 lines
2.7 KiB
Python
Raw Normal View History

2023-04-09 18:50:28 +00:00
import asyncio
2023-04-15 23:36:10 +00:00
from types import TracebackType
from typing import List, Optional, Dict, Any, Type
2023-04-09 18:50:28 +00:00
2023-04-13 21:25:49 +00:00
from aiohttp import ClientSession
2023-04-15 21:02:37 +00:00
from typing_extensions import Self
2023-04-13 21:25:49 +00:00
from pyhon import HonAPI, exceptions
2023-04-09 18:50:28 +00:00
from pyhon.appliance import HonAppliance
class Hon:
2023-05-10 22:43:48 +00:00
def __init__(
self,
email: Optional[str] = "",
password: Optional[str] = "",
session: Optional[ClientSession] = None,
):
self._email: Optional[str] = email
self._password: Optional[str] = password
2023-04-13 21:25:49 +00:00
self._session: ClientSession | None = session
self._appliances: List[HonAppliance] = []
self._api: Optional[HonAPI] = None
2023-04-09 18:50:28 +00:00
2023-04-15 23:36:10 +00:00
async def __aenter__(self) -> Self:
2023-04-10 04:34:19 +00:00
return await self.create()
2023-04-09 18:50:28 +00:00
2023-04-15 23:36:10 +00:00
async def __aexit__(
self,
exc_type: Optional[Type[BaseException]],
exc: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None:
2023-04-10 04:34:19 +00:00
await self.close()
2023-04-13 21:25:49 +00:00
@property
def api(self) -> HonAPI:
if self._api is None:
raise exceptions.NoAuthenticationException
return self._api
2023-05-10 22:43:48 +00: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 21:25:49 +00:00
async def create(self) -> Self:
2023-04-10 04:34:19 +00:00
self._api = await HonAPI(
2023-05-10 22:43:48 +00:00
self.email, self.password, session=self._session
2023-04-10 04:34:19 +00:00
).create()
await self.setup()
return self
2023-04-09 18:50:28 +00:00
@property
def appliances(self) -> List[HonAppliance]:
return self._appliances
2023-05-10 22:43:48 +00:00
@appliances.setter
def appliances(self, appliances) -> None:
self._appliances = appliances
2023-04-15 13:55:22 +00:00
async def _create_appliance(self, appliance_data: Dict[str, Any], zone=0) -> None:
appliance = HonAppliance(self._api, appliance_data, zone=zone)
2023-04-24 02:33:00 +00:00
if appliance.mac_address == "":
2023-04-15 02:12:38 +00:00
return
await asyncio.gather(
*[
appliance.load_attributes(),
appliance.load_commands(),
appliance.load_statistics(),
]
)
self._appliances.append(appliance)
2023-04-15 23:36:10 +00:00
async def setup(self) -> None:
2023-04-15 19:58:20 +00:00
appliance: Dict
2023-04-15 23:36:10 +00:00
for appliance in (await self.api.load_appliances())["payload"]["appliances"]:
if (zones := int(appliance.get("zone", "0"))) > 1:
for zone in range(zones):
await self._create_appliance(appliance.copy(), zone=zone + 1)
2023-04-15 02:12:38 +00:00
await self._create_appliance(appliance)
2023-04-10 04:34:19 +00:00
2023-04-15 23:36:10 +00:00
async def close(self) -> None:
await self.api.close()