Refactor Freebox : add config flow + temperature sensor + signal dispatch (#30334)
* Add config flow to Freebox * Add manufacturer in device_tracker info * Add device_info to sensor + switch * Add device_info: connections * Add config_flow test + update .coveragerc * Typing * Add device_type icon * Remove one error log * Fix pylint * Add myself as CODEOWNER * Handle sync in one place * Separate the Freebox[Router/Device/Sensor] from __init__.py * Add link step to config flow * Make temperature sensors auto-discovered * Use device activity instead of reachablility for device_tracker * Store token file in .storage Depending on host if list of Freebox integration on the future without breaking change * Remove IP sensors + add Freebox router as a device with attrs : IPs, conection type, uptime, version & serial * Add sensor should_poll=False * Test typing * Handle devices with no name * None is the default for data * Fix comment * Use config_entry.unique_id * Add async_unload_entry with asyncio * Add and use bunch of data size and rate related constants (#31781) * Review * Remove useless "already_configured" error string * Review : merge 2 device & 2 sensor classes * Entities from platforms * Fix unload + add device after setup + clean loggers * async_add_entities True * Review * Use pathlib + refactor get_api * device_tracker set + tests with CoroutineMock() * Removing active & reachable from tracker attrs * Review * Fix pipeline * typing * typing * typing * Raise ConfigEntryNotReady when HttpRequestError at setup * Review * Multiple Freebox s * Review: store sensors in router * Freebox: a sensor story
This commit is contained in:
1 parent
180bcad477
commit
19be4a5d6d
17 files changed
+917
-165
No files matched your search
@@ -1,81 +1,127 @@
|
||||
"""Support for Freebox devices (Freebox v6 and Freebox mini 4K)."""
|
||||
import logging
|
||||
from typing import Dict
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import DATA_RATE_KILOBYTES_PER_SECOND
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from . import DATA_FREEBOX
|
||||
from .const import (
|
||||
CONNECTION_SENSORS,
|
||||
DOMAIN,
|
||||
SENSOR_DEVICE_CLASS,
|
||||
SENSOR_ICON,
|
||||
SENSOR_NAME,
|
||||
SENSOR_UNIT,
|
||||
TEMPERATURE_SENSOR_TEMPLATE,
|
||||
)
|
||||
from .router import FreeboxRouter
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
|
||||
) -> None:
|
||||
"""Set up the sensors."""
|
||||
fbx = hass.data[DATA_FREEBOX]
|
||||
async_add_entities([FbxRXSensor(fbx), FbxTXSensor(fbx)], True)
|
||||
router = hass.data[DOMAIN][entry.unique_id]
|
||||
entities = []
|
||||
|
||||
for sensor_name in router.sensors_temperature:
|
||||
entities.append(
|
||||
FreeboxSensor(
|
||||
router,
|
||||
sensor_name,
|
||||
{**TEMPERATURE_SENSOR_TEMPLATE, SENSOR_NAME: f"Freebox {sensor_name}"},
|
||||
)
|
||||
)
|
||||
|
||||
for sensor_key in CONNECTION_SENSORS:
|
||||
entities.append(
|
||||
FreeboxSensor(router, sensor_key, CONNECTION_SENSORS[sensor_key])
|
||||
)
|
||||
|
||||
async_add_entities(entities, True)
|
||||
|
||||
|
||||
class FbxSensor(Entity):
|
||||
"""Representation of a freebox sensor."""
|
||||
class FreeboxSensor(Entity):
|
||||
"""Representation of a Freebox sensor."""
|
||||
|
||||
_name = "generic"
|
||||
_unit = None
|
||||
_icon = None
|
||||
|
||||
def __init__(self, fbx):
|
||||
"""Initialize the sensor."""
|
||||
self._fbx = fbx
|
||||
def __init__(
|
||||
self, router: FreeboxRouter, sensor_type: str, sensor: Dict[str, any]
|
||||
) -> None:
|
||||
"""Initialize a Freebox sensor."""
|
||||
self._state = None
|
||||
self._datas = None
|
||||
self._router = router
|
||||
self._sensor_type = sensor_type
|
||||
self._name = sensor[SENSOR_NAME]
|
||||
self._unit = sensor[SENSOR_UNIT]
|
||||
self._icon = sensor[SENSOR_ICON]
|
||||
self._device_class = sensor[SENSOR_DEVICE_CLASS]
|
||||
self._unique_id = f"{self._router.mac} {self._name}"
|
||||
|
||||
self._unsub_dispatcher = None
|
||||
|
||||
def update(self) -> None:
|
||||
"""Update the Freebox sensor."""
|
||||
state = self._router.sensors[self._sensor_type]
|
||||
if self._unit == DATA_RATE_KILOBYTES_PER_SECOND:
|
||||
self._state = round(state / 1000, 2)
|
||||
else:
|
||||
self._state = state
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique ID."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit of the sensor."""
|
||||
def state(self) -> str:
|
||||
"""Return the state."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self) -> str:
|
||||
"""Return the unit."""
|
||||
return self._unit
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the icon of the sensor."""
|
||||
def icon(self) -> str:
|
||||
"""Return the icon."""
|
||||
return self._icon
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
return self._state
|
||||
def device_class(self) -> str:
|
||||
"""Return the device_class."""
|
||||
return self._device_class
|
||||
|
||||
async def async_update(self):
|
||||
"""Fetch status from freebox."""
|
||||
self._datas = await self._fbx.connection.get_status()
|
||||
@property
|
||||
def device_info(self) -> Dict[str, any]:
|
||||
"""Return the device information."""
|
||||
return self._router.device_info
|
||||
|
||||
@property
|
||||
def should_poll(self) -> bool:
|
||||
"""No polling needed."""
|
||||
return False
|
||||
|
||||
class FbxRXSensor(FbxSensor):
|
||||
"""Update the Freebox RxSensor."""
|
||||
async def async_on_demand_update(self):
|
||||
"""Update state."""
|
||||
self.async_schedule_update_ha_state(True)
|
||||
|
||||
_name = "Freebox download speed"
|
||||
_unit = DATA_RATE_KILOBYTES_PER_SECOND
|
||||
_icon = "mdi:download-network"
|
||||
async def async_added_to_hass(self):
|
||||
"""Register state update callback."""
|
||||
self._unsub_dispatcher = async_dispatcher_connect(
|
||||
self.hass, self._router.signal_sensor_update, self.async_on_demand_update
|
||||
)
|
||||
|
||||
async def async_update(self):
|
||||
"""Get the value from fetched datas."""
|
||||
await super().async_update()
|
||||
if self._datas is not None:
|
||||
self._state = round(self._datas["rate_down"] / 1000, 2)
|
||||
|
||||
|
||||
class FbxTXSensor(FbxSensor):
|
||||
"""Update the Freebox TxSensor."""
|
||||
|
||||
_name = "Freebox upload speed"
|
||||
_unit = DATA_RATE_KILOBYTES_PER_SECOND
|
||||
_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:
|
||||
self._state = round(self._datas["rate_up"] / 1000, 2)
|
||||
async def async_will_remove_from_hass(self):
|
||||
"""Clean up after entity before removal."""
|
||||
self._unsub_dispatcher()
|
||||
Reference in new issue
Block a user