Kane610 / axis

Python library enabling easy communication with Axis devices and its metadatastream
MIT License
30 stars 14 forks source link

fix: prefer given SimpleItem if available #502

Closed swoga closed 1 month ago

swoga commented 1 month ago

fixes: https://github.com/home-assistant/core/issues/123617

Starting with Axis OS 11.11, several SimpleItem entries are sent, which means that the existing method of determining the value fails.

I have extended extract_name_value to prefer a certain SimpleItem active if it exists. I have tried to keep it as downward compatible as possible, but I can only test with Axis OS 11.11.109 and ObjectAnalytics.

payload example:

<?xml version="1.0" encoding="UTF-8"?>
<tt:MetadataStream xmlns:tt="http://www.onvif.org/ver10/schema">
    <tt:Event>
        <wsnt:NotificationMessage xmlns:tns1="http://www.onvif.org/ver10/topics"
            xmlns:tnsaxis="http://www.axis.com/2009/event/topics"
            xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2"
            xmlns:wsa5="http://www.w3.org/2005/08/addressing">
            <wsnt:Topic Dialect="http://docs.oasis-open.org/wsn/t-1/TopicExpression/Simple">tnsaxis:CameraApplicationPlatform/ObjectAnalytics/Device1Scenario2</wsnt:Topic>
            <wsnt:ProducerReference>
                <wsa5:Address>uri://cfbf96f2-9dc7-437f-aa12-155aacbb0b8f/ProducerReference</wsa5:Address>
            </wsnt:ProducerReference>
            <wsnt:Message>
                <tt:Message UtcTime="2024-10-07T21:09:26.046851Z" PropertyOperation="Changed">
                    <tt:Source></tt:Source>
                    <tt:Key></tt:Key>
                    <tt:Data>
                        <tt:SimpleItem Name="classTypes" Value="human"/>
                        <tt:SimpleItem Name="active" Value="1"/>
                        <tt:SimpleItem Name="objectId" Value="234232"/>
                    </tt:Data>
                </tt:Message>
            </wsnt:Message>
        </wsnt:NotificationMessage>
    </tt:Event>
</tt:MetadataStream>
Kane610 commented 1 month ago

Awesome! I'll get back to you during the weekend, you can start by adding a test that covers this situation

swoga commented 1 month ago

added a test! do you happen to have a payload for Axis OS < 11.11 so we can test for regression?

Kane610 commented 1 month ago

added a test! do you happen to have a payload for Axis OS < 11.11 so we can test for regression?

I'll try to fix one tomorrow, is it a property changed you want? what should the configuration be?

swoga commented 1 month ago

I'll try to fix one tomorrow, is it a property changed you want? what should the configuration be?

After further thinking, I don't think we need a payload to test for backwards compatibility after this fix, this should have already been done by other test payloads (like VMD4_ANY_CHANGE).

I have corrected the formatting objected to by the workflow and pushed the branch.

swoga commented 1 month ago

rewrote filter to generator expression: https://github.com/python/mypy/issues/12682#issuecomment-1856012357

-            item = next(filter(lambda x: x.get("@Name", "") == prefer, item), item[0])
+            item = next(
+               (item for item in item if item.get("@Name", "") == prefer), item[0]
+            )
Kane610 commented 1 month ago

I will try to get back to you by tomorrow night

swoga commented 3 weeks ago

Just a note in case it becomes relevant later, this issue resolved itself on my Axis cameras before the version with this fix was rolled out. I have changed a few other (seemingly unrelated) settings on the cameras and also restarted them a few times. Suddenly the SimpleItems are again in a sequence (active in first place) where they can be correctly evaluated by the previous logic.

Kane610 commented 3 weeks ago

If it happened once it can happen again. So its good to keep. Thanks for the update