Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/asuswrt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
MAX_RETRY_TIME = 900

SECRET_GROUP = "Password or SSH Key"
SENSOR_TYPES = ["upload_speed", "download_speed", "download", "upload"]
SENSOR_TYPES = ["devices", "upload_speed", "download_speed", "download", "upload"]

CONFIG_SCHEMA = vol.Schema(
{
Expand Down
39 changes: 38 additions & 1 deletion homeassistant/components/asuswrt/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Asuswrt status sensors."""
import logging

from aioasuswrt.asuswrt import AsusWrt

from homeassistant.const import DATA_GIGABYTES, DATA_RATE_MEGABITS_PER_SECOND
from homeassistant.helpers.entity import Entity

Expand All @@ -18,6 +20,8 @@ async def async_setup_platform(hass, config, add_entities, discovery_info=None):

devices = []

if "devices" in discovery_info:
devices.append(AsuswrtDevicesSensor(api))
if "download" in discovery_info:
devices.append(AsuswrtTotalRXSensor(api))
if "upload" in discovery_info:
Expand All @@ -35,10 +39,12 @@ class AsuswrtSensor(Entity):

_name = "generic"

def __init__(self, api):
def __init__(self, api: AsusWrt):
"""Initialize the sensor."""
self._api = api
self._state = None
self._attributes = None
self._devices = None
self._rates = None
self._speed = None

Expand All @@ -54,10 +60,41 @@ def state(self):

async def async_update(self):
"""Fetch status from asuswrt."""
self._devices = await self._api.async_get_connected_devices()
self._rates = await self._api.async_get_bytes_total()
self._speed = await self._api.async_get_current_transfer_rates()


class AsuswrtDevicesSensor(AsuswrtSensor):
"""Representation of a asuswrt download speed sensor."""

_name = "Asuswrt Devices Connected"

@property
def device_state_attributes(self):
"""Return the attributes of the sensor."""
return self._attributes

async def async_update(self):
"""Fetch new state data for the sensor."""
await super().async_update()
if self._devices:
self._state = len(self._devices)
mac_addresses = []
ip_addresses = []
hosts = []
for mac in self._devices:
device = self._devices[mac]._asdict()
mac_addresses.append(device["mac"])
ip_addresses.append(device["ip"])
hosts.append(device["name"])
self._attributes = {
"mac_addresses": mac_addresses,
"ip_addresses": ip_addresses,
"hosts": hosts,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We try to avoid grouping values as attributes. Can we instead make one binary sensor per connected device with device class connectivity?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Well we have device_tracker for that so I guess I'll remove the attributes

}


class AsuswrtRXSensor(AsuswrtSensor):
"""Representation of a asuswrt download speed sensor."""

Expand Down