mezz64 / pyHik

Python wrapper for Hikvision camera event stream
MIT License
178 stars 50 forks source link

etchannel_num = int(etchannel.text) - TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType' #93

Open acuszka opened 6 months ago

acuszka commented 6 months ago

Hi,

I'm using pyHik through Home Assistant. Everything was working fine for the last 2 years, but suddenly I started receiving the error message below. I recently upgraded my NVR firmware to V4.75.200, and I'm not sure if this is what caused the issue.

NVR: Hikvision DS-7604NI-K1/4P(C) V4.75.200 build 231110

Error while setting up hikvision platform for binary_sensor

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 360, in _async_setup_platform
    await asyncio.shield(task)
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/hikvision/binary_sensor.py", line 110, in setup_platform
    data = HikvisionData(hass, url, port, name, username, password)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/hikvision/binary_sensor.py", line 157, in __init__
    self.camdata = HikCamera(self._url, self._port, self._username, self._password)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyhik/hikvision.py", line 116, in __init__
    self.initialize()
  File "/usr/local/lib/python3.11/site-packages/pyhik/hikvision.py", line 291, in initialize
    events_available = self.get_event_triggers()
                       ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyhik/hikvision.py", line 435, in get_event_triggers
    etchannel_num = int(etchannel.text)
                    ^^^^^^^^^^^^^^^^^^^
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

HA config: binary_sensor:

mezz64 commented 6 months ago

That error would suggest the updated firmware has changed the form of the XML response in the event stream which either changed the name for the channel field or removed it.

If you can post a snippet from the event stream when some events are occurring I can see if it can be accounted for.

acuszka commented 6 months ago

That error would suggest the updated firmware has changed the form of the XML response in the event stream which either changed the name for the channel field or removed it.

If you can post a snippet from the event stream when some events are occurring I can see if it can be accounted for.

Certainly. What specific steps should I take? Do you want me to export the log from the NVR through the web interface? Sorry a bit confused

@mezz64

mezz64 commented 6 months ago

You need to use curl to monitor the alertStream output. Something like this:

curl -u username --digest -N http://YOUR_NVR_IP/ISAPI/Event/notification/alertStream

If you look through other active issues you can find some more detailed info if that doesn't work for you.

acuszka commented 6 months ago

You need to use curl to monitor the alertStream output. Something like this:

curl -u username --digest -N http://YOUR_NVR_IP/ISAPI/Event/notification/alertStream

If you look through other active issues you can find some more detailed info if that doesn't work for you.

<EventNotificationAlert version="1.0" xmlns="http://www.hikvision.com/ver20/XMLSchema">
<ipAddress>192.168.0.10</ipAddress>
<portNo>80</portNo>
<protocol>HTTP</protocol>
<macAddress>24:28:fd:09:e5:1d</macAddress>
<dynChannelID>3</dynChannelID>
<dateTime>2024-01-12T17:32:52</dateTime>
<activePostCount>1</activePostCount>
<eventType>linedetection</eventType>
<eventState>active</eventState>
<eventDescription>linedetection alarm</eventDescription>
<DetectionRegionList>
<DetectionRegionEntry>
<regionID></regionID>
<sensitivityLevel>0</sensitivityLevel>
<RegionCoordinatesList>
</RegionCoordinatesList>
</DetectionRegionEntry>
</DetectionRegionList>
<detectionPictureTransType>url</detectionPictureTransType>
<detectionPicturesNumber>1</detectionPicturesNumber>
<bkgUrl>http://192.168.0.10:80/picture/Streaming/tracks/303/?name=ch00003_00000000294048583526400388747&amp;size=388747</bkgUrl>
<URLCertificationType>digest</URLCertificationType>
</EventNotificationAlert>
--boundary
Content-Type: application/xml; charset="UTF-8"
Content-Length: 969
acuszka commented 6 months ago

full log new 13.txt

acuszka commented 6 months ago

it looks like <channelID>0</channelID> is there for <eventType>videoloss</eventType> but there's only <dynChannelID>3</dynChannelID> for <eventDescription>linedetection alarm</eventDescription> and <eventDescription>fielddetection alarm</eventDescription> ?

mezz64 commented 6 months ago

I didn't realize initially this is thrown on setup, the alertStream looks OK so something must be different in triggers. Please post the output of this:

curl -u username --digest -N http://YOUR_NVR_IP/ISAPI/Event/triggers

acuszka commented 6 months ago

I didn't realize initially this is thrown on setup, the alertStream looks OK so something must be different in triggers. Please post the output of this:

curl -u username --digest -N http://YOUR_NVR_IP/ISAPI/Event/triggers

triggers.txt

mezz64 commented 6 months ago

Thanks. From a quick look I think the problem is in some of the NVR status sensors. They are set to notify the center so they are being parsed, but they don't report any ID's so it doesn't know what camera to associate them with. You also have center notify turned on for videoloss events, which can cause problems.

Try turning off notifying the surveillance center on these alert types and see if it will startup ok. diskfull diskerror illaccess videoloss for each camera

acuszka commented 6 months ago

Thanks. From a quick look I think the problem is in some of the NVR status sensors. They are set to notify the center so they are being parsed, but they don't report any ID's so it doesn't know what camera to associate them with. You also have center notify turned on for videoloss events, which can cause problems.

Try turning off notifying the surveillance center on these alert types and see if it will startup ok. diskfull diskerror illaccess videoloss for each camera

I disabled all events and notifications in both the NVR and in each individual camera. However, I am still encountering the same error in Home Assistant.

image

Is it correct to assume that when Home Assistant initializes Hikvision/pyHik, the script fails due to not receiving the expected data during the 'get_event_triggers' process?

mezz64 commented 6 months ago

Can you generate another output from the trigger endpoint once everything is disabled? I'm wondering if the new firmware isn't replicating certain changes to the api endpoints.

acuszka commented 6 months ago

Can you generate another output from the trigger endpoint once everything is disabled? I'm wondering if the new firmware isn't replicating certain changes to the api endpoints.

triggers2.txt

mezz64 commented 6 months ago

With everything disabled do you get the same error or a different one? Things looked to have changed correctly, but with everything off there won't be any sensors to setup so i'm not surprised it doesn't work. Enable something (motion, linedetection) on one camera and see if it loads.

acuszka commented 6 months ago

With everything disabled do you get the same error or a different one? Things looked to have changed correctly, but with everything off there won't be any sensors to setup so i'm not surprised it doesn't work. Enable something (motion, linedetection) on one camera and see if it loads.

The error remains the same even when all settings are disabled except for motion detection.

Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 360, in _async_setup_platform await asyncio.shield(task) File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/hikvision/binary_sensor.py", line 110, in setup_platform data = HikvisionData(hass, url, port, name, username, password) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/hikvision/binary_sensor.py", line 157, in __init__ self.camdata = HikCamera(self._url, self._port, self._username, self._password) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/pyhik/hikvision.py", line 116, in __init__ self.initialize() File "/usr/local/lib/python3.11/site-packages/pyhik/hikvision.py", line 291, in initialize events_available = self.get_event_triggers() ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/pyhik/hikvision.py", line 435, in get_event_triggers etchannel_num = int(etchannel.text) ^^^^^^^^^^^^^^^^^^^ TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

I enabled motion detection in NVR for one cam: image

Triggers changed, they now include notificationMethod in VMD-1:

`VMD-1

VMD 1 record-1 record 1 center center `
mezz64 commented 6 months ago

Ok, i'll run your trigger output through independently and see if I can track it down. Should be able to find some time this weekend.

mezz64 commented 6 months ago

I think i've found the problem and put together a fix in a new branch (https://github.com/mezz64/pyHik/tree/trig-fixes) for you to test. Please replace the hikvision.py file in your local install with the one from that branch and give home assistant a restart to test. If all works well i'll put together the necessary PR's to update the library and get things fixed in the main branch.

acuszka commented 6 months ago

I think i've found the problem and put together a fix in a new branch (https://github.com/mezz64/pyHik/tree/trig-fixes) for you to test. Please replace the hikvision.py file in your local install with the one from that branch and give home assistant a restart to test. If all works well i'll put together the necessary PR's to update the library and get things fixed in the main branch.

I seem to lack access to both /usr/src/homeassistant and /usr/local/lib/python3.11/site-packages/pyhik/ while using the root account on SSH, as I'm running Home Assistant Operating System.

I believe I need to create a hikvision folder in custom_components to prompt HA to load an updated hikvision script.

Should I place the files from github.com/home-assistant/core/tree/dev/homeassistant/components/hikvision inside this folder and modify them somehow to load a test version of the hikvision.py file?

mezz64 commented 6 months ago

I'm not familiar with the HA OS architecture these days, nor if you can get away with forcing something with custom components.

Let me run the changes through my tests to make sure something unintended didn't break. If all work well I can push the new version to pypi which should make it easier for you to fetch it and test.

acuszka commented 6 months ago

I'm not familiar with the HA OS architecture these days, nor if you can get away with forcing something with custom components.

Let me run the changes through my tests to make sure something unintended didn't break. If all work well I can push the new version to pypi which should make it easier for you to fetch it and test.

Thank you. I've asked about this on the HA forums, but it seems a bit complicated...

https://community.home-assistant.io/t/force-ha-to-use-custom-pyhik-version/673512/5

Marsmz commented 1 month ago

I think i've found the problem and put together a fix in a new branch (https://github.com/mezz64/pyHik/tree/trig-fixes) for you to test. Please replace the hikvision.py file in your local install with the one from that branch and give home assistant a restart to test. If all works well i'll put together the necessary PR's to update the library and get things fixed in the main branch.

I encountered the same issue in Home Assistant, and the error messages were identical. After testing your modified hikvision.py, the binary sensor entities can now be generated correctly. Additionally, there were no other error messages found in the Home Assistant logs. It seems to be functioning properly now. I tested the line cross functionality, and it triggers correctly in Home Assistant.

mezz64 commented 1 month ago

@Marsmz Thanks for confirming. I'll try to find some time to merge this in soon.