Skip to content

Commit

Permalink
Merge pull request #23 from johnnybegood/determine-version
Browse files Browse the repository at this point in the history
Determine model dynamicly #19
  • Loading branch information
johnnybegood committed Nov 2, 2023
2 parents 8d490ae + 0ef2e7f commit e6b8d9e
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions custom_components/ksenia_lares/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Base component for Lares"""
import logging
from typing import Any
from xml.etree.ElementTree import Element

import aiohttp
from getmac import get_mac_address
from lxml import etree
from lxml.etree import Element

from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, format_mac

Expand All @@ -27,11 +27,12 @@ def __init__(self, data: dict) -> None:
self._ip = host
self._port = port
self._host = f"http://{host}:{self._port}"
self._model = None
self._zone_descriptions = None
self._partition_descriptions = None
self._scenario_descriptions = None

async def info(self):
async def info(self) -> dict | None:
"""Get general info"""
response = await self.get("info/generalInfo.xml")

Expand All @@ -57,7 +58,7 @@ async def info(self):

return info

async def device_info(self):
async def device_info(self) -> dict | None:
"""Get device info"""
device_info = await self.info()

Expand All @@ -82,16 +83,18 @@ async def device_info(self):

async def zone_descriptions(self):
"""Get available zones"""
model = await self.get_model()
if self._zone_descriptions is None:
self._zone_descriptions = await self.get_descriptions(
"zones/zonesDescription48IP.xml", "/zonesDescription/zone"
f"zones/zonesDescription{model}.xml", "/zonesDescription/zone"
)

return self._zone_descriptions

async def zones(self):
"""Get available zones"""
response = await self.get("zones/zonesStatus48IP.xml")
model = await self.get_model()
response = await self.get(f"zones/zonesStatus{model}.xml")

if response is None:
return None
Expand All @@ -108,17 +111,20 @@ async def zones(self):

async def partition_descriptions(self):
"""Get available partitions"""
model = await self.get_model()

if self._partition_descriptions is None:
self._partition_descriptions = await self.get_descriptions(
"partitions/partitionsDescription48IP.xml",
f"partitions/partitionsDescription{model}.xml",
"/partitionsDescription/partition",
)

return self._partition_descriptions

async def partitions(self):
"""Get status of partitions"""
response = await self.get("partitions/partitionsStatus48IP.xml")
model = await self.get_model()
response = await self.get(f"partitions/partitionsStatus{model}.xml")

if response is None:
return None
Expand Down Expand Up @@ -176,7 +182,7 @@ async def bypass_zone(self, zone: int, code: str, bypass: bool) -> bool:

return await self.send_command("setByPassZone", code, params)

async def get_descriptions(self, path: str, element: str) -> dict:
async def get_descriptions(self, path: str, element: str) -> dict | None:
"""Get descriptions"""
response = await self.get(path)

Expand All @@ -186,6 +192,19 @@ async def get_descriptions(self, path: str, element: str) -> dict:
content = response.xpath(element)
return [item.text for item in content]

async def get_model(self) -> str:
"""Get model information"""
if self._model is None:
info = await self.info()
if info["name"].endswith("128IP"):
self._model = "128IP"
elif info["name"].endswith("48IP"):
self._model = "48IP"
else:
self._model = "16IP"

return self._model

async def send_command(self, command: str, code: str, params: dict[str, int]) -> bool:
"""Send Command"""
urlparam = "".join(f'&{k}={v}' for k,v in params.items())
Expand Down Expand Up @@ -217,4 +236,4 @@ async def get(self, path):
_LOGGER.debug("Host %s: Connection error %s", self._host, str(conn_err))
except: # pylint: disable=bare-except
_LOGGER.debug("Host %s: Unknown exception occurred", self._host)
return
return None

0 comments on commit e6b8d9e

Please sign in to comment.