Skip to content

Commit

Permalink
Added additional buttons for alarm on/off
Browse files Browse the repository at this point in the history
  • Loading branch information
pergolafabio committed Aug 7, 2023
1 parent 132411c commit 67348be
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
13 changes: 7 additions & 6 deletions hikvision-doorbell/src/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ async def update_door_entities(door_id: str, control_source: str):
"""
Helper function to update the sensor and device trigger of a given door
"""
logger.info("Door {} unlocked by {}, updating sensor and device trigger", door_id+1, control_source)

logger.info("Door {} unlocked by {} , updating sensor and device trigger", door_id+1, control_source)
entity_id = f'door_{door_id}'
door_sensor = cast(Switch, self._sensors[doorbell].get(entity_id))
attributes = {
Expand All @@ -213,6 +213,9 @@ async def update_door_entities(door_id: str, control_source: str):
if alarm_info.byEventType == VIDEO_INTERCOM_EVENT_EVENTTYPE_UNLOCK_LOG:
door_id = alarm_info.uEventInfo.struUnlockRecord.wLockID
control_source = alarm_info.uEventInfo.struUnlockRecord.controlSource()
# unlock_type = alarm_info.uEventInfo.struUnlockRecord.byUnlockType
# card_number = str(alarm_info.uEventInfo.struAuthInfo.byCardNo

# Name of the entity inside the dict array containing all the sensors
entity_id = f'door_{door_id}'
# Extract the sensor entity from the dict and cast to know type
Expand Down Expand Up @@ -350,7 +353,5 @@ def handle_device_trigger(self, doorbell: Doorbell, trigger: DeviceTriggerMetada
# Trigger the event
logger.debug("Invoking device trigger {}", trigger)
# Serialize the payload, if provided as part of the trigger
device_trigger.trigger()
# Getting error here?
# json_payload = json.dumps(trigger['payload']) if trigger.get('payload') else None
# device_trigger.trigger(json_payload)
json_payload = json.dumps(trigger['payload']) if trigger.get('payload') else None
device_trigger.trigger(json_payload)
48 changes: 47 additions & 1 deletion hikvision-doorbell/src/mqtt_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __init__(self, config: AppConfig.MQTT, doorbells: Registry) -> None:
self._sensors[doorbell]['isapi_text'] = isapi_text

if doorbell._config.scenes is True:
# Define scene buttons for indoor stations: "atHome", "goOut", "goToBed", "custom"
# Define scene/alarm buttons for indoor stations: "atHome", "goOut", "goToBed", "custom"

###########
# atHome Button
Expand Down Expand Up @@ -149,6 +149,30 @@ def __init__(self, config: AppConfig.MQTT, doorbells: Registry) -> None:
custom_button = Button(settings, self._custom_callback, doorbell)
custom_button.set_availability(True)

###########
# setupAlarm Button
button_info = ButtonInfo(
name="Alarm On",
unique_id=f"{sanitized_doorbell_name}_setupAlarm",
device=device,
icon="mdi:alarm",
object_id=f"{sanitized_doorbell_name}_setupAlarm")
settings = Settings(mqtt=mqtt_settings, entity=button_info, manual_availability=True)
setupAlarm_button = Button(settings, self._setupAlarm_callback, doorbell)
setupAlarm_button.set_availability(True)

###########
# closeAlarm Button
button_info = ButtonInfo(
name="Alarm Off",
unique_id=f"{sanitized_doorbell_name}_closeAlarm",
device=device,
icon="mdi:alarm-off",
object_id=f"{sanitized_doorbell_name}_closeAlarm")
settings = Settings(mqtt=mqtt_settings, entity=button_info, manual_availability=True)
closeAlarm_button = Button(settings, self._closeAlarm_callback, doorbell)
closeAlarm_button.set_availability(True)

def _reboot_callback(self, client, doorbell: Doorbell, message: MQTTMessage):
logger.info("Received reboot command for doorbell: {}", doorbell._config.name)
# Avoid crashing inside the callback, otherwise we lose the MQTT client
Expand Down Expand Up @@ -246,6 +270,28 @@ def _custom_callback(self, client, doorbell: Doorbell, message: MQTTMessage):
except SDKError as err:
logger.error("Error setting scene: {}", err)

def _setupAlarm_callback(self, client, doorbell: Doorbell, message: MQTTMessage):
logger.info("Received setupAlarm command for doorbell: {}", doorbell._config.name)

url = "/ISAPI/SecurityCP/AlarmControlByPhone"
requestBody = "<AlarmControlByPhoneCfg><commandType>setupAlarm</commandType></AlarmControlByPhoneCfg>"
# Avoid crashing inside the callback, otherwise we lose the MQTT client
try:
doorbell._call_isapi("PUT", url, requestBody)
except SDKError as err:
logger.error("Error setting scene: {}", err)

def _closeAlarm_callback(self, client, doorbell: Doorbell, message: MQTTMessage):
logger.info("Received closeAlarm command for doorbell: {}", doorbell._config.name)

url = "/ISAPI/SecurityCP/AlarmControlByPhone"
requestBody = "<AlarmControlByPhoneCfg><commandType>closeAlarm</commandType></AlarmControlByPhoneCfg>"
# Avoid crashing inside the callback, otherwise we lose the MQTT client
try:
doorbell._call_isapi("PUT", url, requestBody)
except SDKError as err:
logger.error("Error setting scene: {}", err)

def _isapi_input_callback(self, client, doorbell: Doorbell, message: MQTTMessage):
logger.debug("Received input text for doorbell: {}", doorbell._config.name)

Expand Down

0 comments on commit 67348be

Please sign in to comment.