core: unifi switch entities for PoE do not function correctly

The problem

With a freshly installed integration on HA

HA version: 2023.5.4 Home Assistant OS 10.2 UniFi OS UDM Pro 3.0.20 Unifi Network 7.4.156

What version of Home Assistant Core has the issue?

core-2023.5.4

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Unifi Network

Link to integration documentation on our website

https://www.home-assistant.io/integrations/unifi/

Diagnostics information

config_entry-unifi-622637a359eb1e224c7930b8d4f19a42.json.txt

Example YAML snippet

N/A

Anything in the logs that might be useful for us?

The integration is correctly reporting the power from the PoE ports, but attempts to turn the PoE power off on the ports result in an "API invalid payload" error (shown below).

I note two things. 
A. I know the integration changed relatively recently with regard to how the PoE power control is implemented.
B. The unifi network application has itself undergone significant changes recently (last month?) concerning how the port profiles (via which power is controlled) are handled.  I wonder if this has resulted in changes to the API (just speculation) that could have broken this.

2023-06-25 14:25:58.591 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [140380728448320] api.err.InvalidPayload
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 199, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1849, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1889, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 226, in handle_service
    await service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 798, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 980, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 838, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/unifi/switch.py", line 283, in async_turn_off
    await self.entity_description.control_fn(
  File "/usr/src/homeassistant/homeassistant/components/unifi/switch.py", line 137, in async_poe_port_control_fn
    await api.request(DeviceSetPoePortModeRequest.create(device, int(index), state))
  File "/usr/local/lib/python3.10/site-packages/aiounifi/controller.py", line 178, in request
    response: list[dict[str, Any]] = await self._request(
  File "/usr/local/lib/python3.10/site-packages/aiounifi/controller.py", line 235, in _request
    _raise_on_error(response)
  File "/usr/local/lib/python3.10/site-packages/aiounifi/controller.py", line 253, in _raise_on_error
    raise_error(data["meta"]["msg"])
  File "/usr/local/lib/python3.10/site-packages/aiounifi/errors.py", line 61, in raise_error
    raise cls(error)
aiounifi.errors.AiounifiException: api.err.InvalidPayload

Additional information

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 17 (5 by maintainers)

Most upvoted comments

All my switches currently 6.5.59.
Further observation. This is based on trying to recall what I did/did not do in the cases where the PoE control operation worked correctly and did not work correctly.

It appears that if a given port on the switch has had the power disabled and enabled from the UI GUI, subsequently, the PoE switch entity in Home Assistant works as expected. For a newly configured switch (I factory reset, then adopted), the PoE switch entity does not function and reports the invalid payload error already reported.

I tested this with the US-8-60W switch, where I originally reported the problem with the switch relocated to a different UDMP, and the behavior appears repeatable. Unless PoE port has been previously disabled/enabled from the main UI GUI, the switch entity does not work. I have not done an exhaustive test of all combinations of fresh adoption/reboot/PoE off-on/etc. The specific sequence here was

  1. factory reset
  2. adopt
  3. Add user to UDMP for this test
  4. install Unifi Network Integration in HA
  5. Enable PoE sensor and Switch entities in HA
  6. restart HA
  7. Test PoE enable/disable in HA (port 6 and port 7) - both fail
  8. Disable/Enable PoE power in UI GUI for port 6
  9. Test PoE Enable/Disable in HA for port 6. Works as expected
  10. Test PoE Enable/Dusable in HA for port 7. Does not work
  11. Enable/Disable PoE for port 7 in UI GUI
  12. Test PoE Enable/Disable in HA for port 7. Works as expected.