Skip to content

Commit

Permalink
Improve UniFi PoE unique ID (home-assistant#80740)
Browse files Browse the repository at this point in the history
* Improve UniFi PoE unique ID

* Set available property on creation
  • Loading branch information
Kane610 authored Oct 23, 2022
1 parent 18f51db commit 423f6ae
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions homeassistant/components/unifi/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,17 +579,18 @@ class UnifiPoePortSwitch(SwitchEntity):

def __init__(self, obj_id: str, controller) -> None:
"""Set up UniFi Network entity base."""
self._attr_unique_id = f"{obj_id}-PoE"
self._device_mac, port_idx = obj_id.split("_", 1)
self._port_idx = int(port_idx)
self._device_mac, index = obj_id.split("_", 1)
self._index = int(index)
self._obj_id = obj_id
self.controller = controller

port = self.controller.api.ports[self._obj_id]
self._attr_name = f"{port.name} PoE"
self._attr_is_on = port.poe_mode != "off"
self._attr_unique_id = f"{self._device_mac}-poe-{index}"

device = self.controller.api.devices[self._device_mac]
self._attr_available = controller.available and not device.disabled
self._attr_device_info = DeviceInfo(
connections={(CONNECTION_NETWORK_MAC, device.mac)},
manufacturer=ATTR_MANUFACTURER,
Expand Down Expand Up @@ -630,12 +631,12 @@ async def async_turn_on(self, **kwargs: Any) -> None:
"""Enable POE for client."""
device = self.controller.api.devices[self._device_mac]
await self.controller.api.request(
DeviceSetPoePortModeRequest.create(device, self._port_idx, "auto")
DeviceSetPoePortModeRequest.create(device, self._index, "auto")
)

async def async_turn_off(self, **kwargs: Any) -> None:
"""Disable POE for client."""
device = self.controller.api.devices[self._device_mac]
await self.controller.api.request(
DeviceSetPoePortModeRequest.create(device, self._port_idx, "off")
DeviceSetPoePortModeRequest.create(device, self._index, "off")
)

0 comments on commit 423f6ae

Please sign in to comment.