Skip to content

Commit

Permalink
feat: loading of devices from configuration #36
Browse files Browse the repository at this point in the history
  • Loading branch information
petrleocompel committed May 9, 2023
1 parent 106a47c commit 634f166
Show file tree
Hide file tree
Showing 6 changed files with 416 additions and 18 deletions.
21 changes: 20 additions & 1 deletion custom_components/hikvision_axpro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import DATA_COORDINATOR, DOMAIN, USE_CODE_ARMING, INTERNAL_API, ENABLE_DEBUG_OUTPUT
from .model import ZonesResponse, Zone, SubSystemResponse, SubSys, Arming
from .model import ZonesResponse, Zone, SubSystemResponse, SubSys, Arming, ZonesConf, ZoneConfig

PLATFORMS: list[Platform] = [Platform.ALARM_CONTROL_PANEL, Platform.SENSOR]
_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -135,6 +135,8 @@ class HikAxProDataUpdateCoordinator(DataUpdateCoordinator):
device_model: Optional[str] = None
device_name: Optional[str] = None
sub_systems: dict[int, SubSys] = {}
""" Zones aka devices """
devices: dict[int, ZoneConfig] = {}

def __init__(
self,
Expand Down Expand Up @@ -172,8 +174,25 @@ def init_device(self):
self.device_name = self.device_info['DeviceInfo']['deviceName']
self.device_model = self.device_info['DeviceInfo']['model']
_LOGGER.debug(self.device_info)
self.load_devices()
self._update_data()

def load_devices(self):
devices = self._load_devices()
if devices is not None:
self.devices = {}
for item in devices.list:
self.devices[item.zone.id] = item.zone

def _load_devices(self) -> ZonesConf:
endpoint = self.axpro.buildUrl(f"http://{self.host}" + hikaxpro.consts.Endpoints.ZonesConfig, True)
response = self.axpro.makeRequest(endpoint, "GET", False)

if response.status_code != 200:
raise hikaxpro.errors.UnexpectedResponseCodeError(response.status_code, response.text)
_LOGGER.debug(response.text)
return ZonesConf.from_dict(response.json())

def _update_data(self) -> None:
"""Fetch data from axpro via sync functions."""
status = STATE_ALARM_DISARMED
Expand Down
4 changes: 2 additions & 2 deletions custom_components/hikvision_axpro/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def code_format(self) -> CodeFormat | None:

def __get_code_format(self, code_format_str) -> CodeFormat:
"""Returns CodeFormat according to the given code format string."""
code_format: CodeFormat = None
code_format: CodeFormat | None = None

if not self.coordinator.use_code:
code_format = None
Expand Down Expand Up @@ -132,7 +132,7 @@ class HikAxProSubPanel(CoordinatorEntity, AlarmControlPanelEntity):
sys: SubSys
coordinator: HikAxProDataUpdateCoordinator

def __init__(self, coordinator: CoordinatorEntity, sys: SubSys):
def __init__(self, coordinator: HikAxProDataUpdateCoordinator, sys: SubSys):
self.sys = sys
super().__init__(coordinator=coordinator)

Expand Down
2 changes: 1 addition & 1 deletion custom_components/hikvision_axpro/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"iot_class": "local_polling",
"issue_tracker": "https://github.com/petrleocompel/hikaxpro_hacs/issues",
"requirements": [
"hikaxpro==1.3.0",
"hikaxpro==2.1.3",
"requests"
],
"version": "1.3.1"
Expand Down
Loading

0 comments on commit 634f166

Please sign in to comment.