Use EntityDescription - freebox (#55675)
* Use EntityDescription - freebox * Remove default valuespull/55696/head
parent
7111fc47c4
commit
fbf812a845
|
@ -1,12 +1,10 @@
|
||||||
"""Freebox component constants."""
|
"""Freebox component constants."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.components.sensor import SensorEntityDescription
|
||||||
DATA_RATE_KILOBYTES_PER_SECOND,
|
from homeassistant.const import DATA_RATE_KILOBYTES_PER_SECOND, PERCENTAGE
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
|
||||||
PERCENTAGE,
|
|
||||||
TEMP_CELSIUS,
|
|
||||||
)
|
|
||||||
|
|
||||||
DOMAIN = "freebox"
|
DOMAIN = "freebox"
|
||||||
SERVICE_REBOOT = "reboot"
|
SERVICE_REBOOT = "reboot"
|
||||||
|
@ -27,51 +25,39 @@ DEFAULT_DEVICE_NAME = "Unknown device"
|
||||||
STORAGE_KEY = DOMAIN
|
STORAGE_KEY = DOMAIN
|
||||||
STORAGE_VERSION = 1
|
STORAGE_VERSION = 1
|
||||||
|
|
||||||
# Sensor
|
|
||||||
SENSOR_NAME = "name"
|
|
||||||
SENSOR_UNIT = "unit"
|
|
||||||
SENSOR_ICON = "icon"
|
|
||||||
SENSOR_DEVICE_CLASS = "device_class"
|
|
||||||
|
|
||||||
CONNECTION_SENSORS = {
|
CONNECTION_SENSORS: tuple[SensorEntityDescription, ...] = (
|
||||||
"rate_down": {
|
SensorEntityDescription(
|
||||||
SENSOR_NAME: "Freebox download speed",
|
key="rate_down",
|
||||||
SENSOR_UNIT: DATA_RATE_KILOBYTES_PER_SECOND,
|
name="Freebox download speed",
|
||||||
SENSOR_ICON: "mdi:download-network",
|
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND,
|
||||||
SENSOR_DEVICE_CLASS: None,
|
icon="mdi:download-network",
|
||||||
},
|
),
|
||||||
"rate_up": {
|
SensorEntityDescription(
|
||||||
SENSOR_NAME: "Freebox upload speed",
|
key="rate_up",
|
||||||
SENSOR_UNIT: DATA_RATE_KILOBYTES_PER_SECOND,
|
name="Freebox upload speed",
|
||||||
SENSOR_ICON: "mdi:upload-network",
|
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND,
|
||||||
SENSOR_DEVICE_CLASS: None,
|
icon="mdi:upload-network",
|
||||||
},
|
),
|
||||||
}
|
)
|
||||||
|
CONNECTION_SENSORS_KEYS: list[str] = [desc.key for desc in CONNECTION_SENSORS]
|
||||||
|
|
||||||
CALL_SENSORS = {
|
CALL_SENSORS: tuple[SensorEntityDescription, ...] = (
|
||||||
"missed": {
|
SensorEntityDescription(
|
||||||
SENSOR_NAME: "Freebox missed calls",
|
key="missed",
|
||||||
SENSOR_UNIT: None,
|
name="Freebox missed calls",
|
||||||
SENSOR_ICON: "mdi:phone-missed",
|
icon="mdi:phone-missed",
|
||||||
SENSOR_DEVICE_CLASS: None,
|
),
|
||||||
},
|
)
|
||||||
}
|
|
||||||
|
|
||||||
DISK_PARTITION_SENSORS = {
|
DISK_PARTITION_SENSORS: tuple[SensorEntityDescription, ...] = (
|
||||||
"partition_free_space": {
|
SensorEntityDescription(
|
||||||
SENSOR_NAME: "free space",
|
key="partition_free_space",
|
||||||
SENSOR_UNIT: PERCENTAGE,
|
name="free space",
|
||||||
SENSOR_ICON: "mdi:harddisk",
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
SENSOR_DEVICE_CLASS: None,
|
icon="mdi:harddisk",
|
||||||
},
|
),
|
||||||
}
|
)
|
||||||
|
|
||||||
TEMPERATURE_SENSOR_TEMPLATE = {
|
|
||||||
SENSOR_NAME: None,
|
|
||||||
SENSOR_UNIT: TEMP_CELSIUS,
|
|
||||||
SENSOR_ICON: "mdi:thermometer",
|
|
||||||
SENSOR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
|
|
||||||
}
|
|
||||||
|
|
||||||
# Icons
|
# Icons
|
||||||
DEVICE_ICONS = {
|
DEVICE_ICONS = {
|
||||||
|
|
|
@ -24,7 +24,7 @@ from homeassistant.util import slugify
|
||||||
from .const import (
|
from .const import (
|
||||||
API_VERSION,
|
API_VERSION,
|
||||||
APP_DESC,
|
APP_DESC,
|
||||||
CONNECTION_SENSORS,
|
CONNECTION_SENSORS_KEYS,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
STORAGE_KEY,
|
STORAGE_KEY,
|
||||||
STORAGE_VERSION,
|
STORAGE_VERSION,
|
||||||
|
@ -141,7 +141,7 @@ class FreeboxRouter:
|
||||||
|
|
||||||
# Connection sensors
|
# Connection sensors
|
||||||
connection_datas: dict[str, Any] = await self._api.connection.get_status()
|
connection_datas: dict[str, Any] = await self._api.connection.get_status()
|
||||||
for sensor_key in CONNECTION_SENSORS:
|
for sensor_key in CONNECTION_SENSORS_KEYS:
|
||||||
self.sensors_connection[sensor_key] = connection_datas[sensor_key]
|
self.sensors_connection[sensor_key] = connection_datas[sensor_key]
|
||||||
|
|
||||||
self._attrs = {
|
self._attrs = {
|
||||||
|
|
|
@ -4,25 +4,19 @@ from __future__ import annotations
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import DATA_RATE_KILOBYTES_PER_SECOND
|
from homeassistant.const import (
|
||||||
|
DATA_RATE_KILOBYTES_PER_SECOND,
|
||||||
|
DEVICE_CLASS_TEMPERATURE,
|
||||||
|
TEMP_CELSIUS,
|
||||||
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from .const import (
|
from .const import CALL_SENSORS, CONNECTION_SENSORS, DISK_PARTITION_SENSORS, DOMAIN
|
||||||
CALL_SENSORS,
|
|
||||||
CONNECTION_SENSORS,
|
|
||||||
DISK_PARTITION_SENSORS,
|
|
||||||
DOMAIN,
|
|
||||||
SENSOR_DEVICE_CLASS,
|
|
||||||
SENSOR_ICON,
|
|
||||||
SENSOR_NAME,
|
|
||||||
SENSOR_UNIT,
|
|
||||||
TEMPERATURE_SENSOR_TEMPLATE,
|
|
||||||
)
|
|
||||||
from .router import FreeboxRouter
|
from .router import FreeboxRouter
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -41,34 +35,33 @@ async def async_setup_entry(
|
||||||
router.mac,
|
router.mac,
|
||||||
len(router.sensors_temperature),
|
len(router.sensors_temperature),
|
||||||
)
|
)
|
||||||
for sensor_name in router.sensors_temperature:
|
entities = [
|
||||||
entities.append(
|
FreeboxSensor(
|
||||||
FreeboxSensor(
|
router,
|
||||||
router,
|
SensorEntityDescription(
|
||||||
sensor_name,
|
key=sensor_name,
|
||||||
{**TEMPERATURE_SENSOR_TEMPLATE, SENSOR_NAME: f"Freebox {sensor_name}"},
|
name=f"Freebox {sensor_name}",
|
||||||
)
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
for sensor_name in router.sensors_temperature
|
||||||
|
]
|
||||||
|
|
||||||
for sensor_key, sensor in CONNECTION_SENSORS.items():
|
entities.extend(
|
||||||
entities.append(FreeboxSensor(router, sensor_key, sensor))
|
[FreeboxSensor(router, description) for description in CONNECTION_SENSORS]
|
||||||
|
)
|
||||||
for sensor_key, sensor in CALL_SENSORS.items():
|
entities.extend(
|
||||||
entities.append(FreeboxCallSensor(router, sensor_key, sensor))
|
[FreeboxCallSensor(router, description) for description in CALL_SENSORS]
|
||||||
|
)
|
||||||
|
|
||||||
_LOGGER.debug("%s - %s - %s disk(s)", router.name, router.mac, len(router.disks))
|
_LOGGER.debug("%s - %s - %s disk(s)", router.name, router.mac, len(router.disks))
|
||||||
for disk in router.disks.values():
|
entities.extend(
|
||||||
for partition in disk["partitions"]:
|
FreeboxDiskSensor(router, disk, partition, description)
|
||||||
for sensor_key, sensor in DISK_PARTITION_SENSORS.items():
|
for disk in router.disks.values()
|
||||||
entities.append(
|
for partition in disk["partitions"]
|
||||||
FreeboxDiskSensor(
|
for description in DISK_PARTITION_SENSORS
|
||||||
router,
|
)
|
||||||
disk,
|
|
||||||
partition,
|
|
||||||
sensor_key,
|
|
||||||
sensor,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
@ -76,68 +69,30 @@ async def async_setup_entry(
|
||||||
class FreeboxSensor(SensorEntity):
|
class FreeboxSensor(SensorEntity):
|
||||||
"""Representation of a Freebox sensor."""
|
"""Representation of a Freebox sensor."""
|
||||||
|
|
||||||
|
_attr_should_poll = False
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, router: FreeboxRouter, sensor_type: str, sensor: dict[str, Any]
|
self, router: FreeboxRouter, description: SensorEntityDescription
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a Freebox sensor."""
|
"""Initialize a Freebox sensor."""
|
||||||
self._state = None
|
self.entity_description = description
|
||||||
self._router = router
|
self._router = router
|
||||||
self._sensor_type = sensor_type
|
self._attr_unique_id = f"{router.mac} {description.name}"
|
||||||
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}"
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_update_state(self) -> None:
|
def async_update_state(self) -> None:
|
||||||
"""Update the Freebox sensor."""
|
"""Update the Freebox sensor."""
|
||||||
state = self._router.sensors[self._sensor_type]
|
state = self._router.sensors[self.entity_description.key]
|
||||||
if self._unit == DATA_RATE_KILOBYTES_PER_SECOND:
|
if self.native_unit_of_measurement == DATA_RATE_KILOBYTES_PER_SECOND:
|
||||||
self._state = round(state / 1000, 2)
|
self._attr_native_value = round(state / 1000, 2)
|
||||||
else:
|
else:
|
||||||
self._state = state
|
self._attr_native_value = state
|
||||||
|
|
||||||
@property
|
|
||||||
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 native_value(self) -> str:
|
|
||||||
"""Return the state."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self) -> str:
|
|
||||||
"""Return the unit."""
|
|
||||||
return self._unit
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self) -> str:
|
|
||||||
"""Return the icon."""
|
|
||||||
return self._icon
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self) -> str:
|
|
||||||
"""Return the device_class."""
|
|
||||||
return self._device_class
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self) -> DeviceInfo:
|
def device_info(self) -> DeviceInfo:
|
||||||
"""Return the device information."""
|
"""Return the device information."""
|
||||||
return self._router.device_info
|
return self._router.device_info
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self) -> bool:
|
|
||||||
"""No polling needed."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_on_demand_update(self):
|
def async_on_demand_update(self):
|
||||||
"""Update state."""
|
"""Update state."""
|
||||||
|
@ -160,10 +115,10 @@ class FreeboxCallSensor(FreeboxSensor):
|
||||||
"""Representation of a Freebox call sensor."""
|
"""Representation of a Freebox call sensor."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, router: FreeboxRouter, sensor_type: str, sensor: dict[str, Any]
|
self, router: FreeboxRouter, description: SensorEntityDescription
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a Freebox call sensor."""
|
"""Initialize a Freebox call sensor."""
|
||||||
super().__init__(router, sensor_type, sensor)
|
super().__init__(router, description)
|
||||||
self._call_list_for_type = []
|
self._call_list_for_type = []
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
@ -174,10 +129,10 @@ class FreeboxCallSensor(FreeboxSensor):
|
||||||
for call in self._router.call_list:
|
for call in self._router.call_list:
|
||||||
if not call["new"]:
|
if not call["new"]:
|
||||||
continue
|
continue
|
||||||
if call["type"] == self._sensor_type:
|
if self.entity_description.key == call["type"]:
|
||||||
self._call_list_for_type.append(call)
|
self._call_list_for_type.append(call)
|
||||||
|
|
||||||
self._state = len(self._call_list_for_type)
|
self._attr_native_value = len(self._call_list_for_type)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, Any]:
|
def extra_state_attributes(self) -> dict[str, Any]:
|
||||||
|
@ -196,15 +151,14 @@ class FreeboxDiskSensor(FreeboxSensor):
|
||||||
router: FreeboxRouter,
|
router: FreeboxRouter,
|
||||||
disk: dict[str, Any],
|
disk: dict[str, Any],
|
||||||
partition: dict[str, Any],
|
partition: dict[str, Any],
|
||||||
sensor_type: str,
|
description: SensorEntityDescription,
|
||||||
sensor: dict[str, Any],
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a Freebox disk sensor."""
|
"""Initialize a Freebox disk sensor."""
|
||||||
super().__init__(router, sensor_type, sensor)
|
super().__init__(router, description)
|
||||||
self._disk = disk
|
self._disk = disk
|
||||||
self._partition = partition
|
self._partition = partition
|
||||||
self._name = f"{partition['label']} {sensor[SENSOR_NAME]}"
|
self._attr_name = f"{partition['label']} {description.name}"
|
||||||
self._unique_id = f"{self._router.mac} {sensor_type} {self._disk['id']} {self._partition['id']}"
|
self._unique_id = f"{self._router.mac} {description.key} {self._disk['id']} {self._partition['id']}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self) -> DeviceInfo:
|
def device_info(self) -> DeviceInfo:
|
||||||
|
@ -223,6 +177,6 @@ class FreeboxDiskSensor(FreeboxSensor):
|
||||||
@callback
|
@callback
|
||||||
def async_update_state(self) -> None:
|
def async_update_state(self) -> None:
|
||||||
"""Update the Freebox disk sensor."""
|
"""Update the Freebox disk sensor."""
|
||||||
self._state = round(
|
self._attr_native_value = round(
|
||||||
self._partition["free_bytes"] * 100 / self._partition["total_bytes"], 2
|
self._partition["free_bytes"] * 100 / self._partition["total_bytes"], 2
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue