mirror of
https://github.com/Andre0512/pyhOn.git
synced 2024-11-12 01:10:47 +01:00
Fix translation command
This commit is contained in:
parent
e8531f3faf
commit
3e3fc7ad66
@ -90,7 +90,7 @@ def create_command(commands, concat=False):
|
||||
|
||||
|
||||
async def translate(language, json_output=False):
|
||||
async with HonAPI() as hon:
|
||||
async with HonAPI(anonymous=True) as hon:
|
||||
keys = await hon.translation_keys(language)
|
||||
if json_output:
|
||||
print(json.dumps(keys, indent=4))
|
||||
|
@ -10,24 +10,29 @@ _LOGGER = logging.getLogger()
|
||||
|
||||
|
||||
class HonAPI:
|
||||
def __init__(self, email="", password="", anonymous=False) -> None:
|
||||
def __init__(self, email="", password="", anonymous=False, session=None) -> None:
|
||||
super().__init__()
|
||||
self._email = email
|
||||
self._password = password
|
||||
self._anonymous = anonymous
|
||||
self._hon = None
|
||||
self._hon_anonymous = None
|
||||
self._session = session
|
||||
|
||||
async def __aenter__(self):
|
||||
return await self.create()
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
await self._hon.close()
|
||||
await self.close()
|
||||
|
||||
async def create(self):
|
||||
self._hon_anonymous = HonAnonymousConnectionHandler()
|
||||
self._hon_anonymous = await HonAnonymousConnectionHandler(
|
||||
self._session
|
||||
).create()
|
||||
if not self._anonymous:
|
||||
self._hon = await HonConnectionHandler(self._email, self._password).create()
|
||||
self._hon = await HonConnectionHandler(
|
||||
self._email, self._password, self._session
|
||||
).create()
|
||||
return self
|
||||
|
||||
async def load_appliances(self):
|
||||
@ -145,4 +150,7 @@ class HonAPI:
|
||||
return {}
|
||||
|
||||
async def close(self):
|
||||
await self._hon.close()
|
||||
if self._hon:
|
||||
await self._hon.close()
|
||||
if self._hon_anonymous:
|
||||
await self._hon_anonymous.close()
|
||||
|
@ -11,8 +11,8 @@ from pyhon.connection.device import HonDevice
|
||||
class HonBaseConnectionHandler:
|
||||
_HEADERS = {"user-agent": const.USER_AGENT, "Content-Type": "application/json"}
|
||||
|
||||
def __init__(self):
|
||||
self._session = None
|
||||
def __init__(self, session=None):
|
||||
self._session = session
|
||||
self._auth = None
|
||||
|
||||
async def __aenter__(self):
|
||||
@ -26,20 +26,26 @@ class HonBaseConnectionHandler:
|
||||
return self
|
||||
|
||||
@asynccontextmanager
|
||||
async def get(self, *args, **kwargs):
|
||||
async def _intercept(self, method, *args, loop=0, **kwargs):
|
||||
raise NotImplementedError
|
||||
|
||||
@asynccontextmanager
|
||||
async def get(self, *args, **kwargs):
|
||||
async with self._intercept(self._session.get, *args, **kwargs) as response:
|
||||
yield response
|
||||
|
||||
@asynccontextmanager
|
||||
async def post(self, *args, **kwargs):
|
||||
raise NotImplementedError
|
||||
async with self._intercept(self._session.post, *args, **kwargs) as response:
|
||||
yield response
|
||||
|
||||
async def close(self):
|
||||
await self._session.close()
|
||||
|
||||
|
||||
class HonConnectionHandler(HonBaseConnectionHandler):
|
||||
def __init__(self, email, password):
|
||||
super().__init__()
|
||||
def __init__(self, email, password, session=None):
|
||||
super().__init__(session=session)
|
||||
self._device = HonDevice()
|
||||
self._email = email
|
||||
self._password = password
|
||||
@ -108,26 +114,14 @@ class HonConnectionHandler(HonBaseConnectionHandler):
|
||||
)
|
||||
yield {}
|
||||
|
||||
@asynccontextmanager
|
||||
async def get(self, *args, **kwargs):
|
||||
async with self._intercept(self._session.get, *args, **kwargs) as response:
|
||||
yield response
|
||||
|
||||
@asynccontextmanager
|
||||
async def post(self, *args, **kwargs):
|
||||
async with self._intercept(self._session.post, *args, **kwargs) as response:
|
||||
yield response
|
||||
|
||||
|
||||
class HonAnonymousConnectionHandler(HonBaseConnectionHandler):
|
||||
_HEADERS = HonBaseConnectionHandler._HEADERS | {"x-api-key": const.API_KEY}
|
||||
|
||||
@asynccontextmanager
|
||||
async def get(self, *args, **kwargs):
|
||||
async with self._session.post(*args, **kwargs) as response:
|
||||
yield response
|
||||
|
||||
@asynccontextmanager
|
||||
async def post(self, *args, **kwargs):
|
||||
async with self._session.post(*args, **kwargs) as response:
|
||||
async def _intercept(self, method, *args, loop=0, **kwargs):
|
||||
kwargs["headers"] = kwargs.pop("headers", {}) | self._HEADERS
|
||||
async with method(*args, **kwargs) as response:
|
||||
if response.status == 403:
|
||||
print("Can't authorize")
|
||||
yield response
|
||||
|
19
pyhon/hon.py
19
pyhon/hon.py
@ -6,19 +6,25 @@ from pyhon.appliance import HonAppliance
|
||||
|
||||
|
||||
class Hon:
|
||||
def __init__(self, email, password):
|
||||
def __init__(self, email, password, session=None):
|
||||
self._email = email
|
||||
self._password = password
|
||||
self._session = session
|
||||
self._appliances = []
|
||||
self._api = None
|
||||
|
||||
async def __aenter__(self):
|
||||
self._api = await HonAPI(self._email, self._password).create()
|
||||
await self.setup()
|
||||
return self
|
||||
return await self.create()
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
await self._api.close()
|
||||
await self.close()
|
||||
|
||||
async def create(self):
|
||||
self._api = await HonAPI(
|
||||
self._email, self._password, session=self._session
|
||||
).create()
|
||||
await self.setup()
|
||||
return self
|
||||
|
||||
@property
|
||||
def appliances(self) -> List[HonAppliance]:
|
||||
@ -37,3 +43,6 @@ class Hon:
|
||||
]
|
||||
)
|
||||
self._appliances.append(appliance)
|
||||
|
||||
async def close(self):
|
||||
await self._api.close()
|
||||
|
Loading…
Reference in New Issue
Block a user