1 """UniFi Network entity helper."""
3 from __future__
import annotations
5 from datetime
import datetime, timedelta
8 from aiounifi.models.device
import DeviceSetPoePortModeRequest
17 """UniFi Network integration handling platforms for entity registration."""
19 def __init__(self, hass: HomeAssistant, api: aiounifi.Controller) ->
None:
20 """Initialize the UniFi entity loader."""
35 """Initialize entity helper."""
40 """Event to signal new heartbeat missed."""
45 """Update device time in heartbeat monitor."""
50 """Update device time in heartbeat monitor."""
55 self, device_id: str, port_idx: int, poe_mode: str
57 """Queue commands to execute them together per device."""
58 self.
_device_command_device_command.queue_poe_command(device_id, port_idx, poe_mode)
62 """UniFi entity heartbeat monitor."""
66 def __init__(self, hass: HomeAssistant) ->
None:
67 """Initialize the heartbeat monitor."""
71 self._heartbeat_time: dict[str, datetime] = {}
82 """Initialize heartbeat monitor."""
89 """Event to signal new heartbeat missed."""
90 return "unifi-heartbeat-missed"
93 def update(self, unique_id: str, heartbeat_expire_time: datetime) ->
None:
94 """Update device time in heartbeat monitor."""
95 self._heartbeat_time[unique_id] = heartbeat_expire_time
98 def remove(self, unique_id: str) ->
None:
99 """Remove device from heartbeat monitor."""
100 self._heartbeat_time.pop(unique_id,
None)
104 """Check for any devices scheduled to be marked disconnected."""
105 now = dt_util.utcnow()
107 unique_ids_to_remove = []
108 for unique_id, heartbeat_expire_time
in self._heartbeat_time.items():
109 if now > heartbeat_expire_time:
111 unique_ids_to_remove.append(unique_id)
113 for unique_id
in unique_ids_to_remove:
114 del self._heartbeat_time[unique_id]
118 """UniFi Device command helper class."""
122 def __init__(self, hass: HomeAssistant, api: aiounifi.Controller) ->
None:
123 """Initialize device command helper."""
127 self._command_queue: dict[str, dict[int, str]] = {}
139 """Queue commands to execute them together per device."""
142 device_queue = self._command_queue.setdefault(device_id, {})
143 device_queue[port_idx] = poe_mode
145 async
def _command(now: datetime) ->
None:
146 """Execute previously queued commands."""
147 queue = self._command_queue.copy()
148 self._command_queue.clear()
149 for dev_id, device_commands
in queue.items():
150 device = self.
apiapi.devices[dev_id]
151 commands =
list(device_commands.items())
152 await self.
apiapi.request(
153 DeviceSetPoePortModeRequest.create(device, targets=commands)
None __init__(self, HomeAssistant hass, aiounifi.Controller api)
None queue_poe_command(self, str device_id, int port_idx, str poe_mode)
None remove(self, str unique_id)
None _check_for_stale(self, *datetime _)
None update(self, str unique_id, datetime heartbeat_expire_time)
None __init__(self, HomeAssistant hass)
None __init__(self, HomeAssistant hass, aiounifi.Controller api)
None queue_poe_port_command(self, str device_id, int port_idx, str poe_mode)
None remove_heartbeat(self, str unique_id)
str signal_heartbeat(self)
None update_heartbeat(self, str unique_id, datetime heartbeat_expire_time)
bool remove(self, _T matcher)
IssData update(pyiss.ISS iss)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
CALLBACK_TYPE async_call_later(HomeAssistant hass, float|timedelta delay, HassJob[[datetime], Coroutine[Any, Any, None]|None]|Callable[[datetime], Coroutine[Any, Any, None]|None] action)
CALLBACK_TYPE async_track_time_interval(HomeAssistant hass, Callable[[datetime], Coroutine[Any, Any, None]|None] action, timedelta interval, *str|None name=None, bool|None cancel_on_shutdown=None)