elad-bar / ha-edgeos

Integration with EdgeOS (Ubiquiti)
134 stars 23 forks source link

Fix 2 bugs with Unknown Devices Sensor #156

Open illuzn opened 1 month ago

illuzn commented 1 month ago

Addresses issues #154 and #153.

Unknown Devices Logic

https://github.com/illuzn/ha-edgeos/blob/cc9678c575eea992d070038aa6cb4d0e7ec94a2a/custom_components/edgeos/data_processors/device_processor.py#L146-L208

The _update_leased_devices and _set_devices logic was incorrect. When a device first connects (or the integration is loaded), it will be added to the _devices variable as connected. For all such existing devices, the is_leased attribute does not change e.g. if it disconnects then _set_devices is not called and it remains in the list of _devices with is_leased set to True.

This corrects that logic by:

  1. When _update_leased_devices is called, all _devices set is_leased to False.
  2. When set_devices is called: (a) if it is a new device it is added as normal; or (b) if it is an existing known device (e.g. a device connects, disconnects then reconnects some time later), the existing data is used with is_leased set to true.

I considered the possibility to simply delete the entirety of _devices using for example clear() but decided against it since that might give rise to further issues.

Unknown devices leased attribute

During the refactor, the dict leased_devices was erroneously written directly to attributes instead of under the subkey leased (which corresponds to the CONST DHCP_SERVER_LEASED) .