core/homeassistant/components/freebox/sensor.py

81 lines
2.0 KiB
Python
Raw Normal View History

"""Support for Freebox devices (Freebox v6 and Freebox mini 4K)."""
import logging
from homeassistant.helpers.entity import Entity
from . import DATA_FREEBOX
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the sensors."""
fbx = hass.data[DATA_FREEBOX]
async_add_entities([FbxRXSensor(fbx), FbxTXSensor(fbx)], True)
class FbxSensor(Entity):
"""Representation of a freebox sensor."""
2019-07-31 19:25:30 +00:00
_name = "generic"
2019-12-23 16:37:41 +00:00
_unit = None
_icon = None
def __init__(self, fbx):
"""Initialize the sensor."""
self._fbx = fbx
self._state = None
self._datas = None
@property
def name(self):
"""Return the name of the sensor."""
return self._name
2019-12-23 16:37:41 +00:00
@property
def unit_of_measurement(self):
"""Return the unit of the sensor."""
return self._unit
@property
def icon(self):
"""Return the icon of the sensor."""
return self._icon
@property
def state(self):
"""Return the state of the sensor."""
return self._state
async def async_update(self):
"""Fetch status from freebox."""
self._datas = await self._fbx.connection.get_status()
class FbxRXSensor(FbxSensor):
"""Update the Freebox RxSensor."""
2019-07-31 19:25:30 +00:00
_name = "Freebox download speed"
_unit = "KB/s"
2019-12-23 16:37:41 +00:00
_icon = "mdi:download-network"
async def async_update(self):
"""Get the value from fetched datas."""
await super().async_update()
if self._datas is not None:
2019-07-31 19:25:30 +00:00
self._state = round(self._datas["rate_down"] / 1000, 2)
class FbxTXSensor(FbxSensor):
"""Update the Freebox TxSensor."""
2019-07-31 19:25:30 +00:00
_name = "Freebox upload speed"
_unit = "KB/s"
2019-12-23 16:37:41 +00:00
_icon = "mdi:upload-network"
async def async_update(self):
"""Get the value from fetched datas."""
await super().async_update()
if self._datas is not None:
2019-07-31 19:25:30 +00:00
self._state = round(self._datas["rate_up"] / 1000, 2)