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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uptime and number of reboot for bbox sensor #28880

Merged
merged 3 commits into from Nov 26, 2019
Merged
Changes from 1 commit
Commits
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
11 changes: 11 additions & 0 deletions homeassistant/components/bbox/sensor.py
Expand Up @@ -45,6 +45,8 @@
BANDWIDTH_MEGABITS_SECONDS,
"mdi:upload",
],
"uptime": ["Uptime", None, "mdi:clock",],
"number_of_reboots": ["Number of reboot", None, "mdi:restart",],
}

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
Expand Down Expand Up @@ -126,6 +128,12 @@ def update(self):
self._state = round(self.bbox_data.data["rx"]["bandwidth"] / 1000, 2)
elif self.type == "current_up_bandwidth":
self._state = round(self.bbox_data.data["tx"]["bandwidth"] / 1000, 2)
elif self.type == "uptime":
self._state = str(
timedelta(seconds=self.bbox_data.router_infos["device"]["uptime"])
Mryck marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not put relative times in the state machine since it will update a lot and just spams updates due to the time that is passing.

Please use a timestamp device class for this specific sensor as a device class and calculate/format the time to absolute UTC iso format.

Example from other integration that calculates this: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/wled/sensor.py#L121

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope it's better this time, sorry for my lack of knowledge 😄

)
elif self.type == "number_of_reboots":
self._state = self.bbox_data.router_infos["device"]["numberofboots"]


class BboxData:
Expand All @@ -134,6 +142,7 @@ class BboxData:
def __init__(self):
"""Initialize the data object."""
self.data = None
self.router_infos = None

@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
Expand All @@ -142,7 +151,9 @@ def update(self):
try:
box = pybbox.Bbox()
self.data = box.get_ip_stats()
self.router_infos = box.get_bbox_info()
except requests.exceptions.HTTPError as error:
_LOGGER.error(error)
self.data = None
self.router_infos = None
return False