Skip to content

NVR Read and set alarm inputs, outputs and holiday mode safely - #163

Open
baylanger wants to merge 2 commits into
mezz64:masterfrom
baylanger:io-holiday
Open

baylanger wants to merge 2 commits into
mezz64:masterfrom
baylanger:io-holiday

Conversation

@baylanger

@baylanger baylanger commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Depends on #162

The output, input and holiday methods went through request():

  • Any 403 was an authentication error, including the notSupport or invalidOperation of a device without them.
  • get_output_state() and get_holiday_mode_enabled() took any error as False, a rejected password included. A caller polling them kept sending it.
  • set_holiday_mode_enabled() sent the holiday list back rebuilt from a dict.

This adds:

  • get_output_active(id) / set_output_active(id, active): an output's state, and its trigger. The device's answer comes back as an ImageWriteResult. An output set to pulse on the device goes back by itself.
  • get_input_enabled(id) / set_input_enabled(id, enabled): whether an alarm input raises alarms. InputPort.enabled gives it from the list too.
  • get_input_active(id): whether an input's signal is on now.
  • get_io_status(): every input and output state in one request (/ISAPI/System/IO/status), as IOPortStates.
  • get_holidays() / set_holiday_enabled(id, enabled): each holiday, as a Holiday with its name, mode (date, week or month) and dates. A holiday is a set of dates the schedules treat as holidays: there is no device-wide holiday mode. It is written through /ISAPI/System/Holidays/<id>, or within the list on a device without that endpoint.
  • proxy=True on get_output_ports(), get_input_ports() and get_io_status(): the ports of the cameras on an NVR's digital channels (/ISAPI/ContentMgmt/IOProxy). Their IDs are channel × 100 + port (201 is port 1 of channel 2) and work as is with the other methods. The proxy's descriptors hold the camera's password: they are not returned.
  • An input and a holiday are written as the detections are: only enabled changes, the rest is sent back as read, and nothing is sent when it is already so.
  • The port lists are read as XML (name, or outputName/inputName). They return no ports for a device without them.
  • A device without them raises ISAPINotSupportedError (404, 501, notSupport, invalidOperation, also with a 403). 401 and other 403s raise ISAPIAuthError.
  • The existing get_output_state(), set_output_state(), get_holiday_mode_enabled() and set_holiday_mode_enabled() use them:
    • the getters still return False when they cannot read, but raise on rejected credentials; get_holiday_mode_enabled() is any holiday enabled;
    • set_holiday_mode_enabled() sets the first holiday;
    • the setters raise ISAPIError when the device refuses.

The reads of a document and its flags, and their writes, are shared with the detections (_get_supported_xml, _read_flag, _write_flag).

Tests: port lists (names, none, 401, other errors, a port without an ID), proxy ports and status (enabled from the list, proxy IDs, none, status list, not supported, 401, input signal), outputs (state, trigger, refused, 401, not supported, ID as one path segment, legacy methods), inputs (keeps the rest, unchanged sends nothing, not supported), holidays (list, own endpoint, within the list, unchanged sends nothing, unknown ID, a holiday without its flag, none, 401, legacy methods). README updated.

Per this comment - everything in the section "Things we could add but I'd leave out for now" were left out, but if anything is needed in this release we can add.

Not tried on a device: the documents follow the ISAPI layouts of Hikvision's NVR developer guide.

Claude AI tool was used to help write this change.

get_event_states() and set_event_enabled() went through request(), which
turns the document into a dict and back: a write could change the regions
and lines of a detection. They also took any error as "not there", a rejected
password included, so a caller kept sending it for every detection; and a
403 notSupport, what cameras answer for the detections they lack, read as an
authentication error.

- ISAPIClient.get_detection_enabled(detection, channel=1) and
  set_detection_enabled(detection, enabled, channel=1) read and write only
  the enabled flag below the document's root, as text: the rest is sent back
  as read, and nothing is sent when it is already so.
- A detection the device does not have raises ISAPINotSupportedError (404,
  501, notSupport, invalidOperation, also with a 403). 401 and any other 403
  raise ISAPIAuthError.
- DETECTIONS lists the eight of a video channel; videoLoss is added to
  EVENT_ENDPOINTS.
- get_event_states() and set_event_enabled() use them. get_event_states()
  stops on rejected credentials; set_event_enabled() raises when the device
  refuses the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The output, input and holiday methods went through request(): any 403,
including the notSupport or invalidOperation of a device without them, was an
authentication error; get_output_state() and get_holiday_mode_enabled() took
any error, a rejected password included, as False; and
set_holiday_mode_enabled() sent the holiday list back rebuilt from a dict.

- get_output_active() / set_output_active(): an output's state, and its
  trigger, with the device's answer as an ImageWriteResult.
- get_input_enabled() / set_input_enabled(): whether an alarm input raises
  alarms.
- get_input_active(): whether an input's signal is on now.
- get_io_status(): every input and output state in one request
  (/ISAPI/System/IO/status).
- get_holidays() / set_holiday_enabled(id, enabled): each holiday, with its
  name, mode and dates. A holiday is a set of dates the schedules treat as
  holidays: there is no device-wide holiday mode. It is written through
  /ISAPI/System/Holidays/<id>, or within the list on a device without it.
- proxy=True on the port lists and the status: the ports of the cameras on an
  NVR's digital channels (/ISAPI/ContentMgmt/IOProxy), with IDs of channel x
  100 + port, used as is with the other methods.
- InputPort.enabled: whether the input raises alarms, from the list.
- An input and a holiday are written as the detections are: only the
  enabled flag changes, the rest is sent back as read, nothing is sent when it
  is already so.
- A device without them raises ISAPINotSupportedError, also for a 403
  notSupport or invalidOperation; get_output_ports() and get_input_ports()
  return no ports then. 401 and other 403s raise ISAPIAuthError.
- The existing methods use them. get_output_state() and
  get_holiday_mode_enabled() (any holiday) still return False when they cannot read, but
  raise on rejected credentials; the setters raise ISAPIError when the device
  refuses.

The reads of a document, its flags and their writes are now shared with the
detections (_get_supported_xml, _read_flag, _write_flag).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@baylanger

Copy link
Copy Markdown
Contributor Author

@ptarjan whenever you have time, this should now have everything for NVR but if you flag anything let me know.

@ptarjan

ptarjan commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Thanks @baylanger — read through it at 08c44fc. The write path is a big improvement: only the enabled field is touched and nothing is sent when it's already right. A few questions rather than blockers; mezz64's call on all of them:

  1. Per-port calls for NVR cameras. The list/status calls read IOProxy (pyhik/isapi.py:792, :818), but get_input_active/set_output_active/get_input_enabled/set_input_enabled always build on /ISAPI/System/IO/... (isapi.py:879, :890, :909, :928, :945). For an ID like 203, should those go to /ISAPI/ContentMgmt/IOProxy/inputs/203 instead? There's no test for a proxy ID through those methods.
  2. Holiday mode on/off don't match. get_holiday_mode_enabled is "any holiday enabled" (isapi.py:1063) but set_holiday_mode_enabled only touches the first one (isapi.py:1077), so turning it off can leave it reading on. Since set_holiday_enabled already exists, one switch per holiday might be the cleaner shape for HA.
  3. One 403 stops all detection polling. get_event_states now re-raises ISAPIAuthError (isapi.py:1101), which includes the 403 "Insufficient permissions" case (isapi.py:491). A restricted user missing one detection would lose all of them. Maybe re-raise only on 401?
  4. Channel attribution. Proxy IDs stay as raw channel*100+port. A small helper (or a channel field on the port types) would save HA from parsing it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants