mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add uptime and number of reboot for bbox sensor (#28880)
* Add uptime and reboot for bbox sensor * Use timestamp for uptime * Fix black format
This commit is contained in:
parent
6e7b5b71f5
commit
72ccc83651
@ -9,9 +9,15 @@ import voluptuous as vol
|
|||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import CONF_NAME, CONF_MONITORED_VARIABLES, ATTR_ATTRIBUTION
|
from homeassistant.const import (
|
||||||
|
CONF_NAME,
|
||||||
|
CONF_MONITORED_VARIABLES,
|
||||||
|
ATTR_ATTRIBUTION,
|
||||||
|
DEVICE_CLASS_TIMESTAMP,
|
||||||
|
)
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -45,6 +51,8 @@ SENSOR_TYPES = {
|
|||||||
BANDWIDTH_MEGABITS_SECONDS,
|
BANDWIDTH_MEGABITS_SECONDS,
|
||||||
"mdi:upload",
|
"mdi:upload",
|
||||||
],
|
],
|
||||||
|
"uptime": ["Uptime", None, "mdi:clock"],
|
||||||
|
"number_of_reboots": ["Number of reboot", None, "mdi:restart"],
|
||||||
}
|
}
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
@ -72,11 +80,61 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
|
|
||||||
sensors = []
|
sensors = []
|
||||||
for variable in config[CONF_MONITORED_VARIABLES]:
|
for variable in config[CONF_MONITORED_VARIABLES]:
|
||||||
|
if variable == "uptime":
|
||||||
|
sensors.append(BboxUptimeSensor(bbox_data, variable, name))
|
||||||
|
else:
|
||||||
sensors.append(BboxSensor(bbox_data, variable, name))
|
sensors.append(BboxSensor(bbox_data, variable, name))
|
||||||
|
|
||||||
add_entities(sensors, True)
|
add_entities(sensors, True)
|
||||||
|
|
||||||
|
|
||||||
|
class BboxUptimeSensor(Entity):
|
||||||
|
"""Bbox uptime sensor."""
|
||||||
|
|
||||||
|
def __init__(self, bbox_data, sensor_type, name):
|
||||||
|
"""Initialize the sensor."""
|
||||||
|
self.client_name = name
|
||||||
|
self.type = sensor_type
|
||||||
|
self._name = SENSOR_TYPES[sensor_type][0]
|
||||||
|
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
||||||
|
self._icon = SENSOR_TYPES[sensor_type][2]
|
||||||
|
self.bbox_data = bbox_data
|
||||||
|
self._state = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
"""Return the name of the sensor."""
|
||||||
|
return f"{self.client_name} {self._name}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def state(self):
|
||||||
|
"""Return the state of the sensor."""
|
||||||
|
return self._state
|
||||||
|
|
||||||
|
@property
|
||||||
|
def icon(self):
|
||||||
|
"""Icon to use in the frontend, if any."""
|
||||||
|
return self._icon
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_state_attributes(self):
|
||||||
|
"""Return the state attributes."""
|
||||||
|
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_class(self):
|
||||||
|
"""Return the class of this sensor."""
|
||||||
|
return DEVICE_CLASS_TIMESTAMP
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
"""Get the latest data from Bbox and update the state."""
|
||||||
|
self.bbox_data.update()
|
||||||
|
uptime = utcnow() - timedelta(
|
||||||
|
seconds=self.bbox_data.router_infos["device"]["uptime"]
|
||||||
|
)
|
||||||
|
self._state = uptime.replace(microsecond=0).isoformat()
|
||||||
|
|
||||||
|
|
||||||
class BboxSensor(Entity):
|
class BboxSensor(Entity):
|
||||||
"""Implementation of a Bbox sensor."""
|
"""Implementation of a Bbox sensor."""
|
||||||
|
|
||||||
@ -126,6 +184,8 @@ class BboxSensor(Entity):
|
|||||||
self._state = round(self.bbox_data.data["rx"]["bandwidth"] / 1000, 2)
|
self._state = round(self.bbox_data.data["rx"]["bandwidth"] / 1000, 2)
|
||||||
elif self.type == "current_up_bandwidth":
|
elif self.type == "current_up_bandwidth":
|
||||||
self._state = round(self.bbox_data.data["tx"]["bandwidth"] / 1000, 2)
|
self._state = round(self.bbox_data.data["tx"]["bandwidth"] / 1000, 2)
|
||||||
|
elif self.type == "number_of_reboots":
|
||||||
|
self._state = self.bbox_data.router_infos["device"]["numberofboots"]
|
||||||
|
|
||||||
|
|
||||||
class BboxData:
|
class BboxData:
|
||||||
@ -134,6 +194,7 @@ class BboxData:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Initialize the data object."""
|
"""Initialize the data object."""
|
||||||
self.data = None
|
self.data = None
|
||||||
|
self.router_infos = None
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||||
def update(self):
|
def update(self):
|
||||||
@ -142,7 +203,9 @@ class BboxData:
|
|||||||
try:
|
try:
|
||||||
box = pybbox.Bbox()
|
box = pybbox.Bbox()
|
||||||
self.data = box.get_ip_stats()
|
self.data = box.get_ip_stats()
|
||||||
|
self.router_infos = box.get_bbox_info()
|
||||||
except requests.exceptions.HTTPError as error:
|
except requests.exceptions.HTTPError as error:
|
||||||
_LOGGER.error(error)
|
_LOGGER.error(error)
|
||||||
self.data = None
|
self.data = None
|
||||||
|
self.router_infos = None
|
||||||
return False
|
return False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user