Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UniFi Uptime sensor #40058

Merged
merged 38 commits into from Sep 18, 2020
Merged
Changes from 2 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ae63683
Added UniFi Uptime sensor
timkoers Sep 14, 2020
6d08aa2
Update sensor.py
timkoers Sep 14, 2020
b5c2f28
Changed timestamp format and device class
timkoers Sep 14, 2020
a3d55a3
Updated unit of measurement to None
timkoers Sep 14, 2020
a06cece
Added import
timkoers Sep 14, 2020
ee2bebe
Update homeassistant/components/unifi/sensor.py
timkoers Sep 14, 2020
b8613c5
Removed whitespace
timkoers Sep 14, 2020
c74fb3e
Added the uptime sensors option to the config flow
timkoers Sep 14, 2020
67c0818
All the unit tests should be there now
timkoers Sep 14, 2020
85c0fac
Whoops
timkoers Sep 14, 2020
2757268
Fixed translation
timkoers Sep 14, 2020
4fd76dd
Properly formatted the code
timkoers Sep 14, 2020
1dc42e5
Flake8 really has angel eyes
timkoers Sep 14, 2020
95ce1bb
Black should also be satisfied now
timkoers Sep 14, 2020
75f70c6
Should have satisfied all static code analysis tools
timkoers Sep 14, 2020
6b1c434
Fixed add uptime sensor function
timkoers Sep 14, 2020
08faf5c
Fixed overintendation
timkoers Sep 14, 2020
dfd796f
Fixed unit tests
timkoers Sep 14, 2020
0ea957b
Made a spelling mistake during editing of unit tests
timkoers Sep 14, 2020
47dd27d
Test verifies if utc time is correct
timkoers Sep 14, 2020
33c9874
Converted to iso format
timkoers Sep 14, 2020
d89ee56
Converted unit test to iso format
timkoers Sep 14, 2020
6fdce3a
Unit test sensor json had the wrong uptime name
timkoers Sep 14, 2020
14ccc96
Added options_updated handler
timkoers Sep 14, 2020
5acd113
Fixed remove sensors unit test
timkoers Sep 14, 2020
0436454
Update homeassistant/components/unifi/sensor.py
timkoers Sep 15, 2020
db06c80
Update homeassistant/components/unifi/sensor.py
timkoers Sep 15, 2020
45224d0
Update test_device_tracker.py
timkoers Sep 15, 2020
842c1d8
Fixed black formatting issue
timkoers Sep 15, 2020
6ebbf89
I think the code coverage should be good now
timkoers Sep 16, 2020
f22004b
Trying to add the sensors again
timkoers Sep 16, 2020
dc0ef50
Using signals to hopefully trigger the controller to add them again
timkoers Sep 16, 2020
1ad660b
Forgot import
timkoers Sep 16, 2020
94fdaa3
Sorted components
timkoers Sep 16, 2020
d169ffd
fixed isort comments
timkoers Sep 16, 2020
ec55200
Removed CLASS and DEVICE_CLASS
timkoers Sep 16, 2020
ba539a5
Added TYPE again
timkoers Sep 16, 2020
c095d08
Removed double underscores
timkoers Sep 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 26 additions & 3 deletions homeassistant/components/unifi/sensor.py
Expand Up @@ -2,7 +2,7 @@
import logging

from homeassistant.components.sensor import DOMAIN
from homeassistant.const import DATA_MEGABYTES
from homeassistant.const import DATA_MEGABYTES, TIME_SECONDS
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect

Expand All @@ -13,6 +13,7 @@

RX_SENSOR = "rx"
TX_SENSOR = "tx"
UPTIME_SENSOR = "uptime"


async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
Expand All @@ -22,7 +23,7 @@ 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(
Expand All @@ -44,7 +45,7 @@ def add_entities(controller, async_add_entities, clients):
sensors = []

for mac in clients:
for sensor_class in (UniFiRxBandwidthSensor, UniFiTxBandwidthSensor):
for sensor_class in (UniFiRxBandwidthSensor, UniFiTxBandwidthSensor, UniFiUpTimeSensor):
if mac in controller.entities[DOMAIN][sensor_class.TYPE]:
continue

Expand Down Expand Up @@ -100,3 +101,25 @@ def state(self) -> int:
if self._is_wired:
return self.client.wired_tx_bytes / 1000000
return self.client.tx_bytes / 1000000


class UniFiUpTimeSensor(UniFiClient):
"""UniFi uptime sensor base class."""
timkoers marked this conversation as resolved.
Show resolved Hide resolved

DOMAIN = DOMAIN
TYPE = UPTIME_SENSOR

@property
def name(self) -> str:
"""Return the name of the client."""
return f"{super().name} {self.TYPE.upper()}"
timkoers marked this conversation as resolved.
Show resolved Hide resolved

@property
def unit_of_measurement(self) -> str:
"""Return the unit of measurement of this entity."""
return TIME_SECONDS
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved

@property
def state(self) -> int:
"""Return the uptime of the client."""
return self.client.uptime
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved