2019-04-03 15:40:03 +00:00
|
|
|
"""Asuswrt status sensors."""
|
2018-11-07 17:32:13 +00:00
|
|
|
import logging
|
|
|
|
|
2020-04-15 15:42:01 +00:00
|
|
|
from aioasuswrt.asuswrt import AsusWrt
|
|
|
|
|
2020-02-13 16:52:58 +00:00
|
|
|
from homeassistant.const import DATA_GIGABYTES, DATA_RATE_MEGABITS_PER_SECOND
|
2018-11-07 17:32:13 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
|
|
|
from . import DATA_ASUSWRT
|
2018-11-07 17:32:13 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-05-14 21:06:33 +00:00
|
|
|
UPLOAD_ICON = "mdi:upload-network"
|
|
|
|
DOWNLOAD_ICON = "mdi:download-network"
|
|
|
|
|
2018-11-07 17:32:13 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, add_entities, discovery_info=None):
|
2018-11-07 17:32:13 +00:00
|
|
|
"""Set up the asuswrt sensors."""
|
2019-01-08 14:14:16 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
2018-11-07 17:32:13 +00:00
|
|
|
api = hass.data[DATA_ASUSWRT]
|
2019-01-08 14:14:16 +00:00
|
|
|
|
|
|
|
devices = []
|
|
|
|
|
2020-04-15 15:42:01 +00:00
|
|
|
if "devices" in discovery_info:
|
|
|
|
devices.append(AsuswrtDevicesSensor(api))
|
2019-07-31 19:25:30 +00:00
|
|
|
if "download" in discovery_info:
|
2019-01-08 14:14:16 +00:00
|
|
|
devices.append(AsuswrtTotalRXSensor(api))
|
2019-07-31 19:25:30 +00:00
|
|
|
if "upload" in discovery_info:
|
2019-01-08 14:14:16 +00:00
|
|
|
devices.append(AsuswrtTotalTXSensor(api))
|
2019-07-31 19:25:30 +00:00
|
|
|
if "download_speed" in discovery_info:
|
2019-01-08 14:14:16 +00:00
|
|
|
devices.append(AsuswrtRXSensor(api))
|
2019-07-31 19:25:30 +00:00
|
|
|
if "upload_speed" in discovery_info:
|
2019-01-08 14:14:16 +00:00
|
|
|
devices.append(AsuswrtTXSensor(api))
|
|
|
|
|
|
|
|
add_entities(devices)
|
2018-11-07 17:32:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AsuswrtSensor(Entity):
|
|
|
|
"""Representation of a asuswrt sensor."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
_name = "generic"
|
2018-11-07 17:32:13 +00:00
|
|
|
|
2020-04-15 15:42:01 +00:00
|
|
|
def __init__(self, api: AsusWrt):
|
2018-11-07 17:32:13 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._api = api
|
|
|
|
self._state = None
|
2020-04-15 15:42:01 +00:00
|
|
|
self._devices = None
|
2018-11-07 17:32:13 +00:00
|
|
|
self._rates = None
|
|
|
|
self._speed = None
|
2020-06-24 16:50:58 +00:00
|
|
|
self._connect_error = False
|
2018-11-07 17:32:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Fetch status from asuswrt."""
|
2020-06-24 16:50:58 +00:00
|
|
|
try:
|
|
|
|
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()
|
|
|
|
if self._connect_error:
|
|
|
|
self._connect_error = False
|
2020-07-05 18:21:21 +00:00
|
|
|
_LOGGER.info("Reconnected to ASUS router for %s update", self.entity_id)
|
2020-06-24 16:50:58 +00:00
|
|
|
except OSError as err:
|
|
|
|
if not self._connect_error:
|
|
|
|
self._connect_error = True
|
|
|
|
_LOGGER.error(
|
|
|
|
"Error connecting to ASUS router for %s update: %s",
|
|
|
|
self.entity_id,
|
|
|
|
err,
|
|
|
|
)
|
2018-11-07 17:32:13 +00:00
|
|
|
|
|
|
|
|
2020-04-15 15:42:01 +00:00
|
|
|
class AsuswrtDevicesSensor(AsuswrtSensor):
|
|
|
|
"""Representation of a asuswrt download speed sensor."""
|
|
|
|
|
|
|
|
_name = "Asuswrt Devices Connected"
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Fetch new state data for the sensor."""
|
|
|
|
await super().async_update()
|
|
|
|
if self._devices:
|
|
|
|
self._state = len(self._devices)
|
|
|
|
|
|
|
|
|
2018-11-07 17:32:13 +00:00
|
|
|
class AsuswrtRXSensor(AsuswrtSensor):
|
|
|
|
"""Representation of a asuswrt download speed sensor."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
_name = "Asuswrt Download Speed"
|
2020-02-13 16:52:58 +00:00
|
|
|
_unit = DATA_RATE_MEGABITS_PER_SECOND
|
2018-11-07 17:32:13 +00:00
|
|
|
|
2020-05-14 21:06:33 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
|
|
|
return DOWNLOAD_ICON
|
|
|
|
|
2018-11-07 17:32:13 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return self._unit
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Fetch new state data for the sensor."""
|
|
|
|
await super().async_update()
|
2018-11-27 13:20:25 +00:00
|
|
|
if self._speed:
|
2018-11-07 17:32:13 +00:00
|
|
|
self._state = round(self._speed[0] / 125000, 2)
|
|
|
|
|
|
|
|
|
|
|
|
class AsuswrtTXSensor(AsuswrtSensor):
|
|
|
|
"""Representation of a asuswrt upload speed sensor."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
_name = "Asuswrt Upload Speed"
|
2020-02-13 16:52:58 +00:00
|
|
|
_unit = DATA_RATE_MEGABITS_PER_SECOND
|
2018-11-07 17:32:13 +00:00
|
|
|
|
2020-05-14 21:06:33 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
|
|
|
return UPLOAD_ICON
|
|
|
|
|
2018-11-07 17:32:13 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return self._unit
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Fetch new state data for the sensor."""
|
|
|
|
await super().async_update()
|
2018-11-27 13:20:25 +00:00
|
|
|
if self._speed:
|
2018-11-07 17:32:13 +00:00
|
|
|
self._state = round(self._speed[1] / 125000, 2)
|
|
|
|
|
|
|
|
|
|
|
|
class AsuswrtTotalRXSensor(AsuswrtSensor):
|
|
|
|
"""Representation of a asuswrt total download sensor."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
_name = "Asuswrt Download"
|
2020-02-13 16:52:58 +00:00
|
|
|
_unit = DATA_GIGABYTES
|
2018-11-07 17:32:13 +00:00
|
|
|
|
2020-05-14 21:06:33 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
|
|
|
return DOWNLOAD_ICON
|
|
|
|
|
2018-11-07 17:32:13 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return self._unit
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Fetch new state data for the sensor."""
|
|
|
|
await super().async_update()
|
2018-11-27 13:20:25 +00:00
|
|
|
if self._rates:
|
2018-11-07 17:32:13 +00:00
|
|
|
self._state = round(self._rates[0] / 1000000000, 1)
|
|
|
|
|
|
|
|
|
|
|
|
class AsuswrtTotalTXSensor(AsuswrtSensor):
|
|
|
|
"""Representation of a asuswrt total upload sensor."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
_name = "Asuswrt Upload"
|
2020-02-13 16:52:58 +00:00
|
|
|
_unit = DATA_GIGABYTES
|
2018-11-07 17:32:13 +00:00
|
|
|
|
2020-05-14 21:06:33 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
|
|
|
return UPLOAD_ICON
|
|
|
|
|
2018-11-07 17:32:13 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return self._unit
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Fetch new state data for the sensor."""
|
|
|
|
await super().async_update()
|
2018-11-27 13:20:25 +00:00
|
|
|
if self._rates:
|
2018-11-07 17:32:13 +00:00
|
|
|
self._state = round(self._rates[1] / 1000000000, 1)
|