2023-04-10 00:20:28 +05:30
|
|
|
import asyncio
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from pyhon import HonAPI
|
|
|
|
from pyhon.appliance import HonAppliance
|
|
|
|
|
|
|
|
|
|
|
|
class Hon:
|
2023-04-10 10:04:19 +05:30
|
|
|
def __init__(self, email, password, session=None):
|
2023-04-10 00:20:28 +05:30
|
|
|
self._email = email
|
|
|
|
self._password = password
|
2023-04-10 10:04:19 +05:30
|
|
|
self._session = session
|
2023-04-10 00:20:28 +05:30
|
|
|
self._appliances = []
|
|
|
|
self._api = None
|
|
|
|
|
|
|
|
async def __aenter__(self):
|
2023-04-10 10:04:19 +05:30
|
|
|
return await self.create()
|
2023-04-10 00:20:28 +05:30
|
|
|
|
|
|
|
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
2023-04-10 10:04:19 +05:30
|
|
|
await self.close()
|
|
|
|
|
|
|
|
async def create(self):
|
|
|
|
self._api = await HonAPI(
|
|
|
|
self._email, self._password, session=self._session
|
|
|
|
).create()
|
|
|
|
await self.setup()
|
|
|
|
return self
|
2023-04-10 00:20:28 +05:30
|
|
|
|
|
|
|
@property
|
|
|
|
def appliances(self) -> List[HonAppliance]:
|
|
|
|
return self._appliances
|
|
|
|
|
|
|
|
async def setup(self):
|
|
|
|
for appliance in (await self._api.load_appliances())["payload"]["appliances"]:
|
|
|
|
appliance = HonAppliance(self._api, appliance)
|
|
|
|
if appliance.mac_address is None:
|
|
|
|
continue
|
2023-04-10 00:25:36 +05:30
|
|
|
await asyncio.gather(
|
|
|
|
*[
|
|
|
|
appliance.load_attributes(),
|
|
|
|
appliance.load_commands(),
|
|
|
|
appliance.load_statistics(),
|
|
|
|
]
|
|
|
|
)
|
2023-04-10 00:20:28 +05:30
|
|
|
self._appliances.append(appliance)
|
2023-04-10 10:04:19 +05:30
|
|
|
|
|
|
|
async def close(self):
|
|
|
|
await self._api.close()
|