Skip to content

Add device_info and unique_id#8

Open
postlund wants to merge 1 commit into
masterfrom
device_info
Open

Add device_info and unique_id#8
postlund wants to merge 1 commit into
masterfrom
device_info

Conversation

@postlund

Copy link
Copy Markdown
Owner

Device information should now be shown under Configuration -> Devices. As unique_id is set, it should also be possible to edit the entity in the UI, e.g. change name.

@postlund postlund mentioned this pull request Jul 28, 2020
@Molodax

Molodax commented Jul 28, 2020

Copy link
Copy Markdown

It doesn't seem to be working:

2020-07-28 12:59:51 ERROR (MainThread) [custom_components.dlink_hnap.binary_sensor] failed to update motion sensor
Traceback (most recent call last):
  File "/config/custom_components/dlink_hnap/binary_sensor.py", line 134, in async_update
    last_trigger = await self._sensor.latest_trigger()
  File "/config/custom_components/dlink_hnap/dlink.py", line 202, in latest_trigger
    if "GetLatestDetection" in self.client.actions:
TypeError: argument of type 'NoneType' is not iterable
2020-07-28 12:59:51 ERROR (MainThread) [custom_components.dlink_hnap.binary_sensor] failed to update motion sensor
Traceback (most recent call last):
  File "/config/custom_components/dlink_hnap/binary_sensor.py", line 134, in async_update
    last_trigger = await self._sensor.latest_trigger()
  File "/config/custom_components/dlink_hnap/dlink.py", line 202, in latest_trigger
    if "GetLatestDetection" in self.client.actions:
TypeError: argument of type 'NoneType' is not iterable
2020-07-28 12:59:51 ERROR (MainThread) [custom_components.dlink_hnap.binary_sensor] failed to update water sensor
Traceback (most recent call last):
  File "/config/custom_components/dlink_hnap/binary_sensor.py", line 163, in async_update
    water_detected = await self._sensor.water_detected()
  File "/config/custom_components/dlink_hnap/dlink.py", line 231, in water_detected
    if not self._soap_actions:
AttributeError: 'WaterSensor' object has no attribute '_soap_actions'
2020-07-28 12:59:51 ERROR (MainThread) [homeassistant.components.binary_sensor] Error while setting up dlink_hnap platform for binary_sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 187, in _async_setup_platform
    await asyncio.gather(*pending)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 293, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 330, in _async_add_entity
    if entity.unique_id is not None:
  File "/config/custom_components/dlink_hnap/binary_sensor.py", line 106, in unique_id
    return self._sensor.mac
  File "/config/custom_components/dlink_hnap/dlink.py", line 194, in mac
    return self._settings.get("DeviceMacId")
AttributeError: 'coroutine' object has no attribute 'get'
2020-07-28 12:59:56 ERROR (MainThread) [custom_components.dlink_hnap.binary_sensor] failed to update water sensor
Traceback (most recent call last):
  File "/config/custom_components/dlink_hnap/binary_sensor.py", line 163, in async_update
    water_detected = await self._sensor.water_detected()
  File "/config/custom_components/dlink_hnap/dlink.py", line 231, in water_detected
    if not self._soap_actions:
AttributeError: 'WaterSensor' object has no attribute '_soap_actions'
2020-07-28 13:00:01 ERROR (MainThread) [custom_components.dlink_hnap.binary_sensor] failed to update water sensor
Traceback (most recent call last):
  File "/config/custom_components/dlink_hnap/binary_sensor.py", line 163, in async_update
    water_detected = await self._sensor.water_detected()
  File "/config/custom_components/dlink_hnap/dlink.py", line 231, in water_detected
    if not self._soap_actions:
AttributeError: 'WaterSensor' object has no attribute '_soap_actions'
2020-07-28 13:00:06 ERROR (MainThread) [custom_components.dlink_hnap.binary_sensor] failed to update water sensor
Traceback (most recent call last):
  File "/config/custom_components/dlink_hnap/binary_sensor.py", line 163, in async_update
    water_detected = await self._sensor.water_detected()
  File "/config/custom_components/dlink_hnap/dlink.py", line 231, in water_detected
    if not self._soap_actions:
AttributeError: 'WaterSensor' object has no attribute '_soap_actions'

@postlund

Copy link
Copy Markdown
Owner Author

New update pushed, hopefully fixes the issues! 🤞

@Molodax

Molodax commented Jul 28, 2020

Copy link
Copy Markdown

New update pushed, hopefully fixes the issues! 🤞

There are still some issues :/:

2020-07-28 21:29:49 ERROR (MainThread) [custom_components.dlink_hnap.dlink] log list: OrderedDict([('@xmlns', 'http://purenetworks.com/HNAP1/'), ('GetMotionDetectorLogsResult', 'ERROR')])
2020-07-28 21:29:49 ERROR (MainThread) [custom_components.dlink_hnap.binary_sensor] failed to update motion sensor
Traceback (most recent call last):
File "/config/custom_components/dlink_hnap/binary_sensor.py", line 134, in async_update
last_trigger = await self._sensor.latest_trigger()
File "/config/custom_components/dlink_hnap/dlink.py", line 212, in latest_trigger
log_list = resp["MotionDetectorLogList"]
KeyError: 'MotionDetectorLogList'
2020-07-28 21:29:49 ERROR (MainThread) [custom_components.dlink_hnap.dlink] log list: OrderedDict([('@xmlns', 'http://purenetworks.com/HNAP1/'), ('GetMotionDetectorLogsResult', 'ERROR')])
2020-07-28 21:29:49 ERROR (MainThread) [custom_components.dlink_hnap.binary_sensor] failed to update motion sensor
Traceback (most recent call last):
File "/config/custom_components/dlink_hnap/binary_sensor.py", line 134, in async_update
last_trigger = await self._sensor.latest_trigger()
File "/config/custom_components/dlink_hnap/dlink.py", line 212, in latest_trigger
log_list = resp["MotionDetectorLogList"]
KeyError: 'MotionDetectorLogList'

@Molodax

Molodax commented Jul 28, 2020

Copy link
Copy Markdown

It also says fo the sensors in UI :/

'This entity does not have a unique ID, therefore its settings cannot be managed from the UI.'

@ebear5

ebear5 commented Jul 28, 2020

Copy link
Copy Markdown

same result for me

@postlund

Copy link
Copy Markdown
Owner Author

It was a stupid asyncio mistake (didn't await properly). Should be fixed now! Ran pylint on the code a fixed quite a few issues here and there, hope it works better now!

I believe that the ui saying it does not have unique id is a direct consequence of the error above.

@ebear5

ebear5 commented Jul 28, 2020

Copy link
Copy Markdown

I'm still getting: 'This entity does not have a unique ID, therefore its settings cannot be managed from the UI.'

@Molodax

Molodax commented Jul 28, 2020

Copy link
Copy Markdown

It was a stupid asyncio mistake (didn't await properly). Should be fixed now! Ran pylint on the code a fixed quite a few issues here and there, hope it works better now!

I believe that the ui saying it does not have unique id is a direct consequence of the error above.

I'm afraid there is still some issue left :/:

2020-07-28 22:52:39 ERROR (MainThread) [custom_components.dlink_hnap.dlink] log list: OrderedDict([('@xmlns', 'http://purenetworks.com/HNAP1/'), ('GetMotionDetectorLogsResult', 'ERROR')])
NoneType: None
2020-07-28 22:52:39 ERROR (MainThread) [custom_components.dlink_hnap.binary_sensor] failed to update motion sensor
Traceback (most recent call last):
  File "/config/custom_components/dlink_hnap/binary_sensor.py", line 128, in async_update
    last_trigger = await self._sensor.latest_trigger()
  File "/config/custom_components/dlink_hnap/dlink.py", line 206, in latest_trigger
    log_list = resp["MotionDetectorLogList"]
KeyError: 'MotionDetectorLogList'
2020-07-28 22:52:39 ERROR (MainThread) [custom_components.dlink_hnap.dlink] log list: OrderedDict([('@xmlns', 'http://purenetworks.com/HNAP1/'), ('GetMotionDetectorLogsResult', 'ERROR')])
NoneType: None
2020-07-28 22:52:39 ERROR (MainThread) [custom_components.dlink_hnap.binary_sensor] failed to update motion sensor
Traceback (most recent call last):
  File "/config/custom_components/dlink_hnap/binary_sensor.py", line 128, in async_update
    last_trigger = await self._sensor.latest_trigger()
  File "/config/custom_components/dlink_hnap/dlink.py", line 206, in latest_trigger
    log_list = resp["MotionDetectorLogList"]
KeyError: 'MotionDetectorLogList'
2020-07-28 22:52:43 ERROR (MainThread) [custom_components.dlink_hnap.dlink] log list: OrderedDict([('@xmlns', 'http://purenetworks.com/HNAP1/'), ('GetMotionDetectorLogsResult', 'ERROR')])
NoneType: None
2020-07-28 22:52:43 ERROR (MainThread) [custom_components.dlink_hnap.binary_sensor] failed to update motion sensor
Traceback (most recent call last):
  File "/config/custom_components/dlink_hnap/binary_sensor.py", line 128, in async_update
    last_trigger = await self._sensor.latest_trigger()
  File "/config/custom_components/dlink_hnap/dlink.py", line 206, in latest_trigger
    log_list = resp["MotionDetectorLogList"]
KeyError: 'MotionDetectorLogList'

@postlund

Copy link
Copy Markdown
Owner Author

So, the problem is this. Which "function" that is ought to be used to figure out latest motion, depends on the firmware version. D-link changed it at some point. I fetch the list of all supported functions and look for "the new" one and use that if available, otherwise fall back to the old method. For some reason that list doesn't seem to be fetched correctly/at all/when it should, so defection falls back to the old function which your device (with newer firmware) does not support. I don't really understand why, but I have tried to revert the behavior a bit. Can you try the latest version?

@ebear5

ebear5 commented Jul 29, 2020

Copy link
Copy Markdown

Still no unique id

@postlund

Copy link
Copy Markdown
Owner Author

Now I have resurrected an old sensor so that I can actually try the code myself... Latest push now works with unique_id, but I guess I need to use config entries in order to use the device registry. I will look into that. It should pave the way to support config flow and discovery in a simple way too.

@ebear5

ebear5 commented Jul 30, 2020

Copy link
Copy Markdown

The work you're doing is sincerely appreciated

@Molodax

Molodax commented Jul 31, 2020

Copy link
Copy Markdown

Now I have resurrected an old sensor so that I can actually try the code myself... Latest push now works with unique_id, but I guess I need to use config entries in order to use the device registry. I will look into that. It should pave the way to support config flow and discovery in a simple way too.

There are some improvements so the sensors have setting now, however, the sensors don't update anymore:

2020-07-31 19:05:53 ERROR (MainThread) [custom_components.dlink_hnap.dlink] log list: OrderedDict([('@xmlns', 'http://purenetworks.com/HNAP1/'), ('GetMotionDetectorLogsResult', 'ERROR')])
NoneType: None
2020-07-31 19:05:53 ERROR (MainThread) [custom_components.dlink_hnap.binary_sensor] failed to update motion sensor
Traceback (most recent call last):
File "/config/custom_components/dlink_hnap/binary_sensor.py", line 130, in async_update
last_trigger = await self._sensor.latest_trigger()
File "/config/custom_components/dlink_hnap/dlink.py", line 210, in latest_trigger
log_list = resp["MotionDetectorLogList"]
KeyError: 'MotionDetectorLogList'
2020-07-31 19:05:53 ERROR (MainThread) [custom_components.dlink_hnap.dlink] log list: OrderedDict([('@xmlns', 'http://purenetworks.com/HNAP1/'), ('GetMotionDetectorLogsResult', 'ERROR')])
NoneType: None
2020-07-31 19:05:53 ERROR (MainThread) [custom_components.dlink_hnap.binary_sensor] failed to update motion sensor
Traceback (most recent call last):
File "/config/custom_components/dlink_hnap/binary_sensor.py", line 130, in async_update
last_trigger = await self._sensor.latest_trigger()
File "/config/custom_components/dlink_hnap/dlink.py", line 210, in latest_trigger
log_list = resp["MotionDetectorLogList"]
KeyError: 'MotionDetectorLogList'

@MichaelB2018

Copy link
Copy Markdown

This PR is now superseded by #22 (or whatever your PR number is — check). My v2.0.0 rewrite includes everything this PR was trying to achieve and more:

device_info — Full device registry support with manufacturer, model, firmware version, and hardware version via a base entity class
unique_id — Uses device serial number from config entries, so all entities are fully manageable in the UI
Config entries / device registry — Full config flow with UI setup, SSDP auto-discovery, and options flow — exactly the direction @postlund mentioned was needed here
The bugs encountered in this PR (KeyError: 'MotionDetectorLogList', missing await, NoneType in actions) are all resolved in the rewritten HNAP client, which properly handles missing response keys and null values.

See #22 for the full v2.0.0 changes.

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.

4 participants