Skip to content

Commit

Permalink
dynamicly add com ports for indoor
Browse files Browse the repository at this point in the history
  • Loading branch information
pergolafabio committed Sep 12, 2023
1 parent 8e2ef47 commit a5c6f86
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
40 changes: 37 additions & 3 deletions hikvision-doorbell/src/doorbell.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def sdk_device_ability() -> int:
if not result:
raise SDKError(self._sdk, "Error while getting device ability")
response_xml = output_buffer.value.decode('utf-8')
logger.info("Response url for sdk_device_ability: {}", response_xml)
logger.debug("Response url for sdk_device_ability: {}", response_xml)

# Parse the XML response
response = ET.fromstring(response_xml)
Expand All @@ -256,7 +256,7 @@ def sdk_device_ability() -> int:

def isapi_io_outputs() -> int:
io_outputs_xml = self._call_isapi("GET", "/ISAPI/System/IO/outputs")
logger.info("Response url for /ISAPI/System/IO/outputs: {}", io_outputs_xml)
logger.debug("Response url for /ISAPI/System/IO/outputs: {}", io_outputs_xml)
root = ET.fromstring(io_outputs_xml)
if 'IOOutputPortList' not in root.tag:
# XML does not contain the required tag
Expand All @@ -266,7 +266,7 @@ def isapi_io_outputs() -> int:
def isapi_remote_control() -> int:
# Device does not support previous ISAPI endpoint, try another
door_capabilities_xml = self._call_isapi("GET", "/ISAPI/AccessControl/RemoteControl/door/capabilities")
logger.info("Response url for /ISAPI/AccessControl/RemoteControl/door/capabilities: {}", door_capabilities_xml)
logger.debug("Response url for /ISAPI/AccessControl/RemoteControl/door/capabilities: {}", door_capabilities_xml)
root = ET.fromstring(door_capabilities_xml)

door_number_element = root.find('{*}doorNo')
Expand All @@ -291,6 +291,40 @@ def isapi_remote_control() -> int:
# We have run out of available endpoints to call
raise RuntimeError("Unable to get the number of doors, please configure the relays manually with this option in the config: output_relays")

def get_num_coms_indoor(self) -> int:
"""
Get the number of com relays configured for this doorbell.
"""

# Define various functions, each using a different method to gather this information
def isapi_device_info() -> int:
io_coms_xml = self._call_isapi("GET", "/ISAPI/System/deviceInfo")
logger.debug("Response url for /ISAPI/System/IO/outputs: {}", io_coms_xml)
root = ET.fromstring(io_coms_xml)
com_number_element = root.find('{*}alarmOutNum')
# Error out if we don't find attribute `max` inside the `doorNo` element
if com_number_element is None :
# Print a string representation of the response XML
logger.debug("No com ports found for the indoor device")
return 0
logger.debug("We have found {} com ports found for the indoor device", com_number_element.text)
return int(com_number_element.text)

# Define the list of available endpoints to try
available_endpoints: list[Callable] = [isapi_device_info]
for endpoint in available_endpoints:
# Invoke the endpoint, if it errors out try another one
try:
return endpoint()
except RuntimeError:
# This endpoint failed, try the next one
pass

# We have run out of available endpoints to call
#raise RuntimeError("Unable to get the number of doors, please configure the relays manually with this option in the config: output_relays")


def get_device_info(self):
"""Retrieve device information (model, sw version, etc) using the ISAPI endpoint.
Return the parsed XML document"""
Expand Down
6 changes: 4 additions & 2 deletions hikvision-doorbell/src/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ def __init__(self, config: AppConfig.MQTT, doorbells: Registry) -> None:
# Create com1 and com2 ports for indoor stations

if doorbell._type is DeviceType.INDOOR:
logger.debug("Configuring 2 com relays for indoor station")
for com_id in range(2):

num_coms = doorbell.get_num_coms_indoor()
logger.debug("Configuring {} door switches", num_coms)
for com_id in range(num_coms):
com_switch_info = SwitchInfo(
name=f"Com {com_id+1} relay",
unique_id=f"{device.identifiers}-com_relay_{com_id}",
Expand Down

0 comments on commit a5c6f86

Please sign in to comment.