Add UniFi Uptime sensor (#40058)
* Added UniFi Uptime sensor Added the UniFi uptime data as a sensor. Untested. * Update sensor.py Updated code as a result of the tests. * Changed timestamp format and device class Converted state to iso timestamp and changed device class to DEVICE_CLASS_TIMESTAMP. * Updated unit of measurement to None * Added import * Update homeassistant/components/unifi/sensor.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Removed whitespace * Added the uptime sensors option to the config flow * All the unit tests should be there now * Whoops * Fixed translation * Properly formatted the code * Flake8 really has angel eyes * Black should also be satisfied now * Should have satisfied all static code analysis tools * Fixed add uptime sensor function * Fixed overintendation * Fixed unit tests * Made a spelling mistake during editing of unit tests * Test verifies if utc time is correct * Converted to iso format * Converted unit test to iso format * Unit test sensor json had the wrong uptime name * Added options_updated handler * Fixed remove sensors unit test * Update homeassistant/components/unifi/sensor.py Co-authored-by: Robert Svensson <Kane610@users.noreply.github.com> * Update homeassistant/components/unifi/sensor.py Co-authored-by: Robert Svensson <Kane610@users.noreply.github.com> * Update test_device_tracker.py Removed uptime from the devices * Fixed black formatting issue * I think the code coverage should be good now * Trying to add the sensors again * Using signals to hopefully trigger the controller to add them again * Forgot import * Sorted components * fixed isort comments * Removed CLASS and DEVICE_CLASS * Added TYPE again * Removed double underscores Co-authored-by: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Robert Svensson <Kane610@users.noreply.github.com>
This commit is contained in:
9 files changed
+163
-15
No files matched your search
@@ -16,6 +16,7 @@ import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from .const import (
|
||||
CONF_ALLOW_BANDWIDTH_SENSORS,
|
||||
CONF_ALLOW_UPTIME_SENSORS,
|
||||
CONF_BLOCK_CLIENT,
|
||||
CONF_CONTROLLER,
|
||||
CONF_DETECTION_TIME,
|
||||
@@ -312,7 +313,11 @@ class UnifiOptionsFlowHandler(config_entries.OptionsFlow):
|
||||
vol.Optional(
|
||||
CONF_ALLOW_BANDWIDTH_SENSORS,
|
||||
default=self.controller.option_allow_bandwidth_sensors,
|
||||
): bool
|
||||
): bool,
|
||||
vol.Optional(
|
||||
CONF_ALLOW_UPTIME_SENSORS,
|
||||
default=self.controller.option_allow_uptime_sensors,
|
||||
): bool,
|
||||
}
|
||||
),
|
||||
)
|
||||
|
||||
@@ -12,6 +12,7 @@ CONF_SITE_ID = "site"
|
||||
UNIFI_WIRELESS_CLIENTS = "unifi_wireless_clients"
|
||||
|
||||
CONF_ALLOW_BANDWIDTH_SENSORS = "allow_bandwidth_sensors"
|
||||
CONF_ALLOW_UPTIME_SENSORS = "allow_uptime_sensors"
|
||||
CONF_BLOCK_CLIENT = "block_client"
|
||||
CONF_DETECTION_TIME = "detection_time"
|
||||
CONF_IGNORE_WIRED_BUG = "ignore_wired_bug"
|
||||
@@ -22,6 +23,7 @@ CONF_TRACK_WIRED_CLIENTS = "track_wired_clients"
|
||||
CONF_SSID_FILTER = "ssid_filter"
|
||||
|
||||
DEFAULT_ALLOW_BANDWIDTH_SENSORS = False
|
||||
DEFAULT_ALLOW_UPTIME_SENSORS = False
|
||||
DEFAULT_IGNORE_WIRED_BUG = False
|
||||
DEFAULT_POE_CLIENTS = True
|
||||
DEFAULT_TRACK_CLIENTS = True
|
||||
|
||||
@@ -33,6 +33,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
|
||||
from .const import (
|
||||
CONF_ALLOW_BANDWIDTH_SENSORS,
|
||||
CONF_ALLOW_UPTIME_SENSORS,
|
||||
CONF_BLOCK_CLIENT,
|
||||
CONF_CONTROLLER,
|
||||
CONF_DETECTION_TIME,
|
||||
@@ -45,6 +46,7 @@ from .const import (
|
||||
CONF_TRACK_WIRED_CLIENTS,
|
||||
CONTROLLER_ID,
|
||||
DEFAULT_ALLOW_BANDWIDTH_SENSORS,
|
||||
DEFAULT_ALLOW_UPTIME_SENSORS,
|
||||
DEFAULT_DETECTION_TIME,
|
||||
DEFAULT_IGNORE_WIRED_BUG,
|
||||
DEFAULT_POE_CLIENTS,
|
||||
@@ -184,6 +186,13 @@ class UniFiController:
|
||||
CONF_ALLOW_BANDWIDTH_SENSORS, DEFAULT_ALLOW_BANDWIDTH_SENSORS
|
||||
)
|
||||
|
||||
@property
|
||||
def option_allow_uptime_sensors(self):
|
||||
"""Config entry option to allow uptime sensors."""
|
||||
return self.config_entry.options.get(
|
||||
CONF_ALLOW_UPTIME_SENSORS, DEFAULT_ALLOW_UPTIME_SENSORS
|
||||
)
|
||||
|
||||
@callback
|
||||
def async_unifi_signalling_callback(self, signal, data):
|
||||
"""Handle messages back from UniFi library."""
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
"""Support for bandwidth sensors with UniFi clients."""
|
||||
import logging
|
||||
|
||||
from homeassistant.components.sensor import DOMAIN
|
||||
from homeassistant.components.sensor import DEVICE_CLASS_TIMESTAMP, DOMAIN
|
||||
from homeassistant.const import DATA_MEGABYTES
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from .const import DOMAIN as UNIFI_DOMAIN
|
||||
from .unifi_client import UniFiClient
|
||||
@@ -13,6 +14,7 @@ LOGGER = logging.getLogger(__name__)
|
||||
|
||||
RX_SENSOR = "rx"
|
||||
TX_SENSOR = "tx"
|
||||
UPTIME_SENSOR = "uptime"
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
@@ -22,7 +24,11 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up sensors for UniFi integration."""
|
||||
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
||||
controller.entities[DOMAIN] = {RX_SENSOR: set(), TX_SENSOR: set()}
|
||||
controller.entities[DOMAIN] = {
|
||||
RX_SENSOR: set(),
|
||||
TX_SENSOR: set(),
|
||||
UPTIME_SENSOR: set(),
|
||||
}
|
||||
|
||||
@callback
|
||||
def items_added(
|
||||
@@ -30,7 +36,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
) -> None:
|
||||
"""Update the values of the controller."""
|
||||
if controller.option_allow_bandwidth_sensors:
|
||||
add_entities(controller, async_add_entities, clients)
|
||||
add_bandwith_entities(controller, async_add_entities, clients)
|
||||
|
||||
if controller.option_allow_uptime_sensors:
|
||||
add_uptime_entities(controller, async_add_entities, clients)
|
||||
|
||||
for signal in (controller.signal_update, controller.signal_options_update):
|
||||
controller.listeners.append(async_dispatcher_connect(hass, signal, items_added))
|
||||
@@ -39,7 +48,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
|
||||
|
||||
@callback
|
||||
def add_entities(controller, async_add_entities, clients):
|
||||
def add_bandwith_entities(controller, async_add_entities, clients):
|
||||
"""Add new sensor entities from the controller."""
|
||||
sensors = []
|
||||
|
||||
@@ -55,6 +64,22 @@ def add_entities(controller, async_add_entities, clients):
|
||||
async_add_entities(sensors)
|
||||
|
||||
|
||||
@callback
|
||||
def add_uptime_entities(controller, async_add_entities, clients):
|
||||
"""Add new sensor entities from the controller."""
|
||||
sensors = []
|
||||
|
||||
for mac in clients:
|
||||
if mac in controller.entities[DOMAIN][UniFiUpTimeSensor.TYPE]:
|
||||
continue
|
||||
|
||||
client = controller.api.clients[mac]
|
||||
sensors.append(UniFiUpTimeSensor(client, controller))
|
||||
|
||||
if sensors:
|
||||
async_add_entities(sensors)
|
||||
|
||||
|
||||
class UniFiBandwidthSensor(UniFiClient):
|
||||
"""UniFi bandwidth sensor base class."""
|
||||
|
||||
@@ -100,3 +125,30 @@ class UniFiTxBandwidthSensor(UniFiBandwidthSensor):
|
||||
if self._is_wired:
|
||||
return self.client.wired_tx_bytes / 1000000
|
||||
return self.client.tx_bytes / 1000000
|
||||
|
||||
|
||||
class UniFiUpTimeSensor(UniFiClient):
|
||||
"""UniFi uptime sensor."""
|
||||
|
||||
DOMAIN = DOMAIN
|
||||
TYPE = UPTIME_SENSOR
|
||||
|
||||
@property
|
||||
def device_class(self) -> str:
|
||||
"""Return device class."""
|
||||
return DEVICE_CLASS_TIMESTAMP
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the client."""
|
||||
return f"{super().name} {self.TYPE.capitalize()}"
|
||||
|
||||
@property
|
||||
def state(self) -> int:
|
||||
"""Return the uptime of the client."""
|
||||
return dt_util.utc_from_timestamp(float(self.client.uptime)).isoformat()
|
||||
|
||||
async def options_updated(self) -> None:
|
||||
"""Config entry options are updated, remove entity if option is disabled."""
|
||||
if not self.controller.option_allow_uptime_sensors:
|
||||
await self.remove_item({self.client.mac})
|
||||
@@ -54,7 +54,8 @@
|
||||
},
|
||||
"statistics_sensors": {
|
||||
"data": {
|
||||
"allow_bandwidth_sensors": "Bandwidth usage sensors for network clients"
|
||||
"allow_bandwidth_sensors": "Bandwidth usage sensors for network clients",
|
||||
"allow_uptime_sensors": "Uptime sensors for network clients"
|
||||
},
|
||||
"description": "Configure statistics sensors",
|
||||
"title": "UniFi options 3/3"
|
||||
|
||||
@@ -58,7 +58,8 @@
|
||||
},
|
||||
"statistics_sensors": {
|
||||
"data": {
|
||||
"allow_bandwidth_sensors": "Maak bandbreedtegebruiksensoren voor netwerkclients"
|
||||
"allow_bandwidth_sensors": "Maak bandbreedtegebruiksensoren voor netwerkclients",
|
||||
"allow_uptime_sensors": "Maak uptime-sensoren voor netwerkclients"
|
||||
},
|
||||
"description": "Configureer statistische sensoren",
|
||||
"title": "UniFi-opties 3/3"
|
||||
|
||||
Reference in new issue
Block a user