2021-11-26 21:44:49 +00:00
|
|
|
"""Track both clients and devices using UniFi Network."""
|
2020-05-07 07:58:04 +00:00
|
|
|
from datetime import timedelta
|
2019-06-02 16:24:13 +00:00
|
|
|
|
2022-01-24 14:11:33 +00:00
|
|
|
from aiounifi.api import SOURCE_DATA
|
2020-05-08 20:19:27 +00:00
|
|
|
from aiounifi.events import (
|
2020-05-10 15:14:45 +00:00
|
|
|
ACCESS_POINT_UPGRADED,
|
|
|
|
GATEWAY_UPGRADED,
|
|
|
|
SWITCH_UPGRADED,
|
2020-05-08 20:19:27 +00:00
|
|
|
WIRED_CLIENT_CONNECTED,
|
|
|
|
WIRELESS_CLIENT_CONNECTED,
|
|
|
|
WIRELESS_CLIENT_ROAM,
|
|
|
|
WIRELESS_CLIENT_ROAMRADIO,
|
2020-06-22 19:50:34 +00:00
|
|
|
WIRELESS_GUEST_CONNECTED,
|
|
|
|
WIRELESS_GUEST_ROAM,
|
|
|
|
WIRELESS_GUEST_ROAMRADIO,
|
2020-05-08 20:19:27 +00:00
|
|
|
)
|
2020-05-07 07:58:04 +00:00
|
|
|
|
2020-04-19 19:30:06 +00:00
|
|
|
from homeassistant.components.device_tracker import DOMAIN
|
2019-07-14 19:57:09 +00:00
|
|
|
from homeassistant.components.device_tracker.config_entry import ScannerEntity
|
|
|
|
from homeassistant.components.device_tracker.const import SOURCE_TYPE_ROUTER
|
2021-12-27 22:42:24 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2019-07-14 19:57:09 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-12-27 22:42:24 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2017-10-13 08:13:58 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2016-02-19 23:19:03 +00:00
|
|
|
|
2022-01-05 07:16:43 +00:00
|
|
|
from .const import DOMAIN as UNIFI_DOMAIN
|
2020-01-31 19:23:25 +00:00
|
|
|
from .unifi_client import UniFiClient
|
2020-04-23 14:48:24 +00:00
|
|
|
from .unifi_entity_base import UniFiBase
|
2019-06-02 16:24:13 +00:00
|
|
|
|
2020-05-08 20:19:27 +00:00
|
|
|
CLIENT_TRACKER = "client"
|
|
|
|
DEVICE_TRACKER = "device"
|
|
|
|
|
2020-04-05 23:26:11 +00:00
|
|
|
CLIENT_CONNECTED_ATTRIBUTES = [
|
2019-07-31 19:25:30 +00:00
|
|
|
"_is_guest_by_uap",
|
2019-08-16 18:29:38 +00:00
|
|
|
"ap_mac",
|
2019-07-31 19:25:30 +00:00
|
|
|
"authorized",
|
|
|
|
"essid",
|
|
|
|
"ip",
|
|
|
|
"is_11r",
|
|
|
|
"is_guest",
|
2021-07-27 10:35:16 +00:00
|
|
|
"note",
|
2019-07-31 19:25:30 +00:00
|
|
|
"qos_policy_applied",
|
|
|
|
"radio",
|
|
|
|
"radio_proto",
|
|
|
|
"vlan",
|
2019-07-14 19:57:09 +00:00
|
|
|
]
|
|
|
|
|
2020-04-05 23:26:11 +00:00
|
|
|
CLIENT_STATIC_ATTRIBUTES = [
|
|
|
|
"hostname",
|
|
|
|
"mac",
|
|
|
|
"name",
|
|
|
|
"oui",
|
|
|
|
]
|
|
|
|
|
2021-01-16 00:01:37 +00:00
|
|
|
|
2021-01-10 19:12:21 +00:00
|
|
|
CLIENT_CONNECTED_ALL_ATTRIBUTES = CLIENT_CONNECTED_ATTRIBUTES + CLIENT_STATIC_ATTRIBUTES
|
|
|
|
|
2020-05-10 15:14:45 +00:00
|
|
|
DEVICE_UPGRADED = (ACCESS_POINT_UPGRADED, GATEWAY_UPGRADED, SWITCH_UPGRADED)
|
|
|
|
|
2020-05-08 20:19:27 +00:00
|
|
|
WIRED_CONNECTION = (WIRED_CLIENT_CONNECTED,)
|
|
|
|
WIRELESS_CONNECTION = (
|
|
|
|
WIRELESS_CLIENT_CONNECTED,
|
|
|
|
WIRELESS_CLIENT_ROAM,
|
|
|
|
WIRELESS_CLIENT_ROAMRADIO,
|
2020-06-22 19:50:34 +00:00
|
|
|
WIRELESS_GUEST_CONNECTED,
|
|
|
|
WIRELESS_GUEST_ROAM,
|
|
|
|
WIRELESS_GUEST_ROAMRADIO,
|
2020-05-08 20:19:27 +00:00
|
|
|
)
|
2020-04-19 19:30:06 +00:00
|
|
|
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2021-12-27 22:42:24 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2021-11-26 21:44:49 +00:00
|
|
|
"""Set up device tracker for UniFi Network integration."""
|
2020-04-23 14:48:24 +00:00
|
|
|
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
2020-04-19 19:30:06 +00:00
|
|
|
controller.entities[DOMAIN] = {CLIENT_TRACKER: set(), DEVICE_TRACKER: set()}
|
2019-07-29 21:13:04 +00:00
|
|
|
|
2019-07-14 19:57:09 +00:00
|
|
|
@callback
|
2020-05-04 17:29:49 +00:00
|
|
|
def items_added(
|
|
|
|
clients: set = controller.api.clients, devices: set = controller.api.devices
|
|
|
|
) -> None:
|
2019-07-14 19:57:09 +00:00
|
|
|
"""Update the values of the controller."""
|
2020-05-04 17:29:49 +00:00
|
|
|
if controller.option_track_clients:
|
|
|
|
add_client_entities(controller, async_add_entities, clients)
|
|
|
|
|
|
|
|
if controller.option_track_devices:
|
|
|
|
add_device_entities(controller, async_add_entities, devices)
|
2020-02-13 00:15:08 +00:00
|
|
|
|
2020-04-19 19:30:06 +00:00
|
|
|
for signal in (controller.signal_update, controller.signal_options_update):
|
2021-04-20 18:50:42 +00:00
|
|
|
config_entry.async_on_unload(
|
|
|
|
async_dispatcher_connect(hass, signal, items_added)
|
|
|
|
)
|
2016-02-19 23:19:03 +00:00
|
|
|
|
2020-04-16 22:08:53 +00:00
|
|
|
items_added()
|
2016-02-19 23:19:03 +00:00
|
|
|
|
2018-04-16 06:20:58 +00:00
|
|
|
|
2019-07-14 19:57:09 +00:00
|
|
|
@callback
|
2020-05-04 17:29:49 +00:00
|
|
|
def add_client_entities(controller, async_add_entities, clients):
|
|
|
|
"""Add new client tracker entities from the controller."""
|
2020-04-19 19:30:06 +00:00
|
|
|
trackers = []
|
2018-08-13 19:18:25 +00:00
|
|
|
|
2020-05-04 17:29:49 +00:00
|
|
|
for mac in clients:
|
|
|
|
if mac in controller.entities[DOMAIN][UniFiClientTracker.TYPE]:
|
2020-02-13 00:15:08 +00:00
|
|
|
continue
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2020-05-04 17:29:49 +00:00
|
|
|
client = controller.api.clients[mac]
|
2019-07-30 08:05:51 +00:00
|
|
|
|
2020-05-04 17:29:49 +00:00
|
|
|
if mac not in controller.wireless_clients:
|
|
|
|
if not controller.option_track_wired_clients:
|
2019-08-02 08:13:00 +00:00
|
|
|
continue
|
2020-05-04 17:29:49 +00:00
|
|
|
elif (
|
|
|
|
client.essid
|
|
|
|
and controller.option_ssid_filter
|
|
|
|
and client.essid not in controller.option_ssid_filter
|
|
|
|
):
|
|
|
|
continue
|
2019-07-30 08:05:51 +00:00
|
|
|
|
2020-05-04 17:29:49 +00:00
|
|
|
trackers.append(UniFiClientTracker(client, controller))
|
2020-02-13 00:15:08 +00:00
|
|
|
|
2020-05-04 17:29:49 +00:00
|
|
|
if trackers:
|
|
|
|
async_add_entities(trackers)
|
2019-07-14 19:57:09 +00:00
|
|
|
|
|
|
|
|
2020-05-04 17:29:49 +00:00
|
|
|
@callback
|
|
|
|
def add_device_entities(controller, async_add_entities, devices):
|
|
|
|
"""Add new device tracker entities from the controller."""
|
|
|
|
trackers = []
|
|
|
|
|
|
|
|
for mac in devices:
|
|
|
|
if mac in controller.entities[DOMAIN][UniFiDeviceTracker.TYPE]:
|
|
|
|
continue
|
|
|
|
|
|
|
|
device = controller.api.devices[mac]
|
|
|
|
trackers.append(UniFiDeviceTracker(device, controller))
|
2020-04-16 22:08:53 +00:00
|
|
|
|
2020-04-19 19:30:06 +00:00
|
|
|
if trackers:
|
|
|
|
async_add_entities(trackers)
|
2020-04-16 22:08:53 +00:00
|
|
|
|
|
|
|
|
2020-01-31 19:23:25 +00:00
|
|
|
class UniFiClientTracker(UniFiClient, ScannerEntity):
|
2019-07-30 08:05:51 +00:00
|
|
|
"""Representation of a network client."""
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2020-04-21 04:17:14 +00:00
|
|
|
DOMAIN = DOMAIN
|
2020-04-19 19:30:06 +00:00
|
|
|
TYPE = CLIENT_TRACKER
|
|
|
|
|
2019-07-14 19:57:09 +00:00
|
|
|
def __init__(self, client, controller):
|
2019-07-30 08:05:51 +00:00
|
|
|
"""Set up tracked client."""
|
2020-01-31 19:23:25 +00:00
|
|
|
super().__init__(client, controller)
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2021-01-17 02:10:52 +00:00
|
|
|
self.heartbeat_check = False
|
2021-02-01 16:55:16 +00:00
|
|
|
self._controller_connection_state_changed = False
|
2021-01-26 15:41:28 +00:00
|
|
|
|
2022-01-24 14:11:33 +00:00
|
|
|
self._last_seen = client.last_seen or 0
|
|
|
|
self.schedule_update = self._is_connected = (
|
|
|
|
self.is_wired == client.is_wired
|
|
|
|
and dt_util.utcnow() - dt_util.utc_from_timestamp(float(self._last_seen))
|
|
|
|
< controller.option_detection_time
|
|
|
|
)
|
2019-12-30 18:40:52 +00:00
|
|
|
|
2021-01-10 19:12:21 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Watch object when added."""
|
2021-01-17 02:10:52 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
|
|
|
f"{self.controller.signal_heartbeat_missed}_{self.unique_id}",
|
|
|
|
self._make_disconnected,
|
|
|
|
)
|
|
|
|
)
|
2021-01-10 19:12:21 +00:00
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
2020-05-08 20:19:27 +00:00
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
|
|
|
"""Disconnect object when removed."""
|
2021-01-17 02:10:52 +00:00
|
|
|
self.controller.async_heartbeat(self.unique_id)
|
2020-05-08 20:19:27 +00:00
|
|
|
await super().async_will_remove_from_hass()
|
2019-08-31 20:04:04 +00:00
|
|
|
|
2020-05-08 20:19:27 +00:00
|
|
|
@callback
|
2021-01-26 15:41:28 +00:00
|
|
|
def async_signal_reachable_callback(self) -> None:
|
|
|
|
"""Call when controller connection state change."""
|
2021-02-01 16:55:16 +00:00
|
|
|
self._controller_connection_state_changed = True
|
|
|
|
super().async_signal_reachable_callback()
|
2021-01-26 15:41:28 +00:00
|
|
|
|
|
|
|
@callback
|
2021-02-01 16:55:16 +00:00
|
|
|
def async_update_callback(self) -> None:
|
2020-05-08 20:19:27 +00:00
|
|
|
"""Update the clients state."""
|
2020-03-11 16:24:52 +00:00
|
|
|
|
2021-02-01 16:55:16 +00:00
|
|
|
if self._controller_connection_state_changed:
|
|
|
|
self._controller_connection_state_changed = False
|
|
|
|
|
2021-01-26 15:41:28 +00:00
|
|
|
if self.controller.available:
|
|
|
|
self.schedule_update = True
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.controller.async_heartbeat(self.unique_id)
|
2022-01-24 14:11:33 +00:00
|
|
|
super().async_update_callback()
|
2020-05-08 20:19:27 +00:00
|
|
|
|
2021-03-27 09:54:59 +00:00
|
|
|
elif (
|
2022-01-24 14:11:33 +00:00
|
|
|
self.client.last_updated == SOURCE_DATA
|
|
|
|
and self._last_seen != self.client.last_seen
|
2021-03-27 09:54:59 +00:00
|
|
|
and self.is_wired == self.client.is_wired
|
|
|
|
):
|
2022-01-24 14:11:33 +00:00
|
|
|
self._last_seen = self.client.last_seen
|
2021-03-27 09:54:59 +00:00
|
|
|
self._is_connected = True
|
|
|
|
self.schedule_update = True
|
2020-05-08 20:19:27 +00:00
|
|
|
|
|
|
|
if self.schedule_update:
|
|
|
|
self.schedule_update = False
|
2021-01-17 02:10:52 +00:00
|
|
|
self.controller.async_heartbeat(
|
|
|
|
self.unique_id, dt_util.utcnow() + self.controller.option_detection_time
|
2020-05-08 20:19:27 +00:00
|
|
|
)
|
2021-01-17 02:10:52 +00:00
|
|
|
self.heartbeat_check = True
|
2020-03-11 16:24:52 +00:00
|
|
|
|
2022-01-24 14:11:33 +00:00
|
|
|
super().async_update_callback()
|
2020-05-08 20:19:27 +00:00
|
|
|
|
2020-07-02 22:46:37 +00:00
|
|
|
@callback
|
2021-01-17 02:10:52 +00:00
|
|
|
def _make_disconnected(self, *_):
|
|
|
|
"""No heart beat by device."""
|
2020-07-02 22:46:37 +00:00
|
|
|
self._is_connected = False
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2022-01-05 07:16:43 +00:00
|
|
|
@property
|
|
|
|
def device_info(self) -> None:
|
|
|
|
"""Return no device info."""
|
|
|
|
return None
|
|
|
|
|
2020-05-08 20:19:27 +00:00
|
|
|
@property
|
|
|
|
def is_connected(self):
|
|
|
|
"""Return true if the client is connected to the network."""
|
2020-04-21 19:12:44 +00:00
|
|
|
if (
|
|
|
|
not self.is_wired
|
|
|
|
and self.client.essid
|
|
|
|
and self.controller.option_ssid_filter
|
|
|
|
and self.client.essid not in self.controller.option_ssid_filter
|
|
|
|
):
|
|
|
|
return False
|
|
|
|
|
2020-05-08 20:19:27 +00:00
|
|
|
return self._is_connected
|
2019-07-14 19:57:09 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def source_type(self):
|
2019-07-30 08:05:51 +00:00
|
|
|
"""Return the source type of the client."""
|
2019-07-14 19:57:09 +00:00
|
|
|
return SOURCE_TYPE_ROUTER
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return a unique identifier for this client."""
|
2019-09-03 19:12:51 +00:00
|
|
|
return f"{self.client.mac}-{self.controller.site}"
|
2019-07-14 19:57:09 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 19:16:26 +00:00
|
|
|
def extra_state_attributes(self):
|
2019-07-30 08:05:51 +00:00
|
|
|
"""Return the client state attributes."""
|
2021-01-10 19:12:21 +00:00
|
|
|
raw = self.client.raw
|
2020-04-05 23:26:11 +00:00
|
|
|
|
2021-07-27 10:35:16 +00:00
|
|
|
attributes_to_check = CLIENT_STATIC_ATTRIBUTES
|
2020-07-02 22:46:37 +00:00
|
|
|
if self.is_connected:
|
2021-07-27 10:35:16 +00:00
|
|
|
attributes_to_check = CLIENT_CONNECTED_ALL_ATTRIBUTES
|
2020-07-02 22:46:37 +00:00
|
|
|
|
2021-07-27 10:35:16 +00:00
|
|
|
attributes = {k: raw[k] for k in attributes_to_check if k in raw}
|
2021-01-10 19:12:21 +00:00
|
|
|
attributes["is_wired"] = self.is_wired
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2018-08-13 19:18:25 +00:00
|
|
|
return attributes
|
2019-07-30 08:05:51 +00:00
|
|
|
|
2021-01-16 00:01:37 +00:00
|
|
|
@property
|
|
|
|
def ip_address(self) -> str:
|
|
|
|
"""Return the primary ip address of the device."""
|
|
|
|
return self.client.raw.get("ip")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def mac_address(self) -> str:
|
|
|
|
"""Return the mac address of the device."""
|
|
|
|
return self.client.raw.get("mac")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def hostname(self) -> str:
|
|
|
|
"""Return hostname of the device."""
|
|
|
|
return self.client.raw.get("hostname")
|
|
|
|
|
2020-04-19 19:30:06 +00:00
|
|
|
async def options_updated(self) -> None:
|
|
|
|
"""Config entry options are updated, remove entity if option is disabled."""
|
|
|
|
if not self.controller.option_track_clients:
|
2020-05-19 21:57:41 +00:00
|
|
|
await self.remove_item({self.client.mac})
|
2020-04-19 19:30:06 +00:00
|
|
|
|
|
|
|
elif self.is_wired:
|
|
|
|
if not self.controller.option_track_wired_clients:
|
2020-05-19 21:57:41 +00:00
|
|
|
await self.remove_item({self.client.mac})
|
2020-05-08 20:19:27 +00:00
|
|
|
|
|
|
|
elif (
|
|
|
|
self.controller.option_ssid_filter
|
|
|
|
and self.client.essid not in self.controller.option_ssid_filter
|
|
|
|
):
|
2020-05-19 21:57:41 +00:00
|
|
|
await self.remove_item({self.client.mac})
|
2020-04-19 19:30:06 +00:00
|
|
|
|
2019-07-30 08:05:51 +00:00
|
|
|
|
2020-04-19 19:30:06 +00:00
|
|
|
class UniFiDeviceTracker(UniFiBase, ScannerEntity):
|
2019-07-30 08:05:51 +00:00
|
|
|
"""Representation of a network infrastructure device."""
|
|
|
|
|
2020-04-21 04:17:14 +00:00
|
|
|
DOMAIN = DOMAIN
|
2020-04-19 19:30:06 +00:00
|
|
|
TYPE = DEVICE_TRACKER
|
|
|
|
|
2019-07-30 08:05:51 +00:00
|
|
|
def __init__(self, device, controller):
|
|
|
|
"""Set up tracked device."""
|
2020-05-08 22:34:18 +00:00
|
|
|
super().__init__(device, controller)
|
2020-04-19 19:30:06 +00:00
|
|
|
|
2021-01-20 22:58:02 +00:00
|
|
|
self.device = self._item
|
2020-05-08 22:34:18 +00:00
|
|
|
self._is_connected = device.state == 1
|
2021-02-01 16:55:16 +00:00
|
|
|
self._controller_connection_state_changed = False
|
2021-01-26 15:41:28 +00:00
|
|
|
self.schedule_update = False
|
2020-05-07 07:58:04 +00:00
|
|
|
|
2021-01-10 19:12:21 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Watch object when added."""
|
2021-01-17 02:10:52 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
|
|
|
f"{self.controller.signal_heartbeat_missed}_{self.unique_id}",
|
|
|
|
self._make_disconnected,
|
|
|
|
)
|
|
|
|
)
|
2021-01-10 19:12:21 +00:00
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
2020-01-31 19:23:25 +00:00
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
2021-01-10 19:12:21 +00:00
|
|
|
"""Disconnect object when removed."""
|
2021-01-17 02:10:52 +00:00
|
|
|
self.controller.async_heartbeat(self.unique_id)
|
2020-05-08 20:19:27 +00:00
|
|
|
await super().async_will_remove_from_hass()
|
2019-12-11 14:45:21 +00:00
|
|
|
|
2020-01-31 19:23:25 +00:00
|
|
|
@callback
|
2021-01-26 15:41:28 +00:00
|
|
|
def async_signal_reachable_callback(self) -> None:
|
|
|
|
"""Call when controller connection state change."""
|
2021-02-01 16:55:16 +00:00
|
|
|
self._controller_connection_state_changed = True
|
|
|
|
super().async_signal_reachable_callback()
|
2021-01-26 15:41:28 +00:00
|
|
|
|
|
|
|
@callback
|
2021-02-01 16:55:16 +00:00
|
|
|
def async_update_callback(self) -> None:
|
2020-05-07 07:58:04 +00:00
|
|
|
"""Update the devices' state."""
|
|
|
|
|
2021-02-01 16:55:16 +00:00
|
|
|
if self._controller_connection_state_changed:
|
|
|
|
self._controller_connection_state_changed = False
|
|
|
|
|
2021-01-26 15:41:28 +00:00
|
|
|
if self.controller.available:
|
|
|
|
if self._is_connected:
|
|
|
|
self.schedule_update = True
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.controller.async_heartbeat(self.unique_id)
|
|
|
|
|
|
|
|
elif self.device.last_updated == SOURCE_DATA:
|
2020-05-07 07:58:04 +00:00
|
|
|
self._is_connected = True
|
2021-01-26 15:41:28 +00:00
|
|
|
self.schedule_update = True
|
2020-05-07 07:58:04 +00:00
|
|
|
|
2021-01-26 15:41:28 +00:00
|
|
|
if self.schedule_update:
|
|
|
|
self.schedule_update = False
|
|
|
|
self.controller.async_heartbeat(
|
|
|
|
self.unique_id,
|
|
|
|
dt_util.utcnow() + timedelta(seconds=self.device.next_interval + 60),
|
|
|
|
)
|
|
|
|
|
2020-05-08 20:19:27 +00:00
|
|
|
super().async_update_callback()
|
2019-07-30 08:05:51 +00:00
|
|
|
|
2020-07-02 22:46:37 +00:00
|
|
|
@callback
|
2021-01-17 02:10:52 +00:00
|
|
|
def _make_disconnected(self, *_):
|
2020-07-02 22:46:37 +00:00
|
|
|
"""No heart beat by device."""
|
|
|
|
self._is_connected = False
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2019-07-30 08:05:51 +00:00
|
|
|
@property
|
|
|
|
def is_connected(self):
|
|
|
|
"""Return true if the device is connected to the network."""
|
2020-05-07 07:58:04 +00:00
|
|
|
return self._is_connected
|
2019-07-30 08:05:51 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def source_type(self):
|
|
|
|
"""Return the source type of the device."""
|
|
|
|
return SOURCE_TYPE_ROUTER
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the name of the device."""
|
2019-08-06 05:00:06 +00:00
|
|
|
return self.device.name or self.device.model
|
2019-07-30 08:05:51 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return a unique identifier for this device."""
|
|
|
|
return self.device.mac
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if controller is available."""
|
2019-08-01 15:22:08 +00:00
|
|
|
return not self.device.disabled and self.controller.available
|
2019-07-30 08:05:51 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 19:16:26 +00:00
|
|
|
def extra_state_attributes(self):
|
2019-07-30 08:05:51 +00:00
|
|
|
"""Return the device state attributes."""
|
2019-08-11 20:40:44 +00:00
|
|
|
if self.device.state == 0:
|
2019-08-06 21:55:36 +00:00
|
|
|
return {}
|
|
|
|
|
2019-07-30 08:05:51 +00:00
|
|
|
attributes = {}
|
|
|
|
|
|
|
|
if self.device.has_fan:
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes["fan_level"] = self.device.fan_level
|
2019-07-30 08:05:51 +00:00
|
|
|
|
2019-08-11 20:40:44 +00:00
|
|
|
if self.device.overheating:
|
|
|
|
attributes["overheating"] = self.device.overheating
|
|
|
|
|
|
|
|
if self.device.upgradable:
|
|
|
|
attributes["upgradable"] = self.device.upgradable
|
|
|
|
|
2019-07-30 08:05:51 +00:00
|
|
|
return attributes
|
2020-01-31 19:23:25 +00:00
|
|
|
|
2020-04-19 19:30:06 +00:00
|
|
|
async def options_updated(self) -> None:
|
|
|
|
"""Config entry options are updated, remove entity if option is disabled."""
|
|
|
|
if not self.controller.option_track_devices:
|
2020-05-19 21:57:41 +00:00
|
|
|
await self.remove_item({self.device.mac})
|