Use DataRate unit and device class in freebox (#83612)
* Use DataRate unit and device class in freebox * UnitOfTemperature
This commit is contained in:
parent
549d1151b7
commit
01ee066163
2 changed files with 40 additions and 37 deletions
|
@ -3,8 +3,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntityDescription
|
from homeassistant.const import Platform
|
||||||
from homeassistant.const import DATA_RATE_KILOBYTES_PER_SECOND, PERCENTAGE, Platform
|
|
||||||
|
|
||||||
DOMAIN = "freebox"
|
DOMAIN = "freebox"
|
||||||
SERVICE_REBOOT = "reboot"
|
SERVICE_REBOOT = "reboot"
|
||||||
|
@ -26,38 +25,8 @@ STORAGE_KEY = DOMAIN
|
||||||
STORAGE_VERSION = 1
|
STORAGE_VERSION = 1
|
||||||
|
|
||||||
|
|
||||||
CONNECTION_SENSORS: tuple[SensorEntityDescription, ...] = (
|
CONNECTION_SENSORS_KEYS = {"rate_down", "rate_up"}
|
||||||
SensorEntityDescription(
|
|
||||||
key="rate_down",
|
|
||||||
name="Freebox download speed",
|
|
||||||
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND,
|
|
||||||
icon="mdi:download-network",
|
|
||||||
),
|
|
||||||
SensorEntityDescription(
|
|
||||||
key="rate_up",
|
|
||||||
name="Freebox upload speed",
|
|
||||||
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND,
|
|
||||||
icon="mdi:upload-network",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
CONNECTION_SENSORS_KEYS: list[str] = [desc.key for desc in CONNECTION_SENSORS]
|
|
||||||
|
|
||||||
CALL_SENSORS: tuple[SensorEntityDescription, ...] = (
|
|
||||||
SensorEntityDescription(
|
|
||||||
key="missed",
|
|
||||||
name="Freebox missed calls",
|
|
||||||
icon="mdi:phone-missed",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
DISK_PARTITION_SENSORS: tuple[SensorEntityDescription, ...] = (
|
|
||||||
SensorEntityDescription(
|
|
||||||
key="partition_free_space",
|
|
||||||
name="free space",
|
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
|
||||||
icon="mdi:harddisk",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Icons
|
# Icons
|
||||||
DEVICE_ICONS = {
|
DEVICE_ICONS = {
|
||||||
|
|
|
@ -10,18 +10,52 @@ from homeassistant.components.sensor import (
|
||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import DATA_RATE_KILOBYTES_PER_SECOND, TEMP_CELSIUS
|
from homeassistant.const import PERCENTAGE, UnitOfDataRate, UnitOfTemperature
|
||||||
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
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from .const import CALL_SENSORS, CONNECTION_SENSORS, DISK_PARTITION_SENSORS, DOMAIN
|
from .const import DOMAIN
|
||||||
from .router import FreeboxRouter
|
from .router import FreeboxRouter
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
CONNECTION_SENSORS: tuple[SensorEntityDescription, ...] = (
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="rate_down",
|
||||||
|
name="Freebox download speed",
|
||||||
|
device_class=SensorDeviceClass.DATA_RATE,
|
||||||
|
native_unit_of_measurement=UnitOfDataRate.KILOBYTES_PER_SECOND,
|
||||||
|
icon="mdi:download-network",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="rate_up",
|
||||||
|
name="Freebox upload speed",
|
||||||
|
device_class=SensorDeviceClass.DATA_RATE,
|
||||||
|
native_unit_of_measurement=UnitOfDataRate.KILOBYTES_PER_SECOND,
|
||||||
|
icon="mdi:upload-network",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
CALL_SENSORS: tuple[SensorEntityDescription, ...] = (
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="missed",
|
||||||
|
name="Freebox missed calls",
|
||||||
|
icon="mdi:phone-missed",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
DISK_PARTITION_SENSORS: tuple[SensorEntityDescription, ...] = (
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="partition_free_space",
|
||||||
|
name="free space",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
icon="mdi:harddisk",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
@ -42,7 +76,7 @@ async def async_setup_entry(
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key=sensor_name,
|
key=sensor_name,
|
||||||
name=f"Freebox {sensor_name}",
|
name=f"Freebox {sensor_name}",
|
||||||
native_unit_of_measurement=TEMP_CELSIUS,
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||||
device_class=SensorDeviceClass.TEMPERATURE,
|
device_class=SensorDeviceClass.TEMPERATURE,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -84,7 +118,7 @@ class FreeboxSensor(SensorEntity):
|
||||||
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.entity_description.key]
|
state = self._router.sensors[self.entity_description.key]
|
||||||
if self.native_unit_of_measurement == DATA_RATE_KILOBYTES_PER_SECOND:
|
if self.native_unit_of_measurement == UnitOfDataRate.KILOBYTES_PER_SECOND:
|
||||||
self._attr_native_value = round(state / 1000, 2)
|
self._attr_native_value = round(state / 1000, 2)
|
||||||
else:
|
else:
|
||||||
self._attr_native_value = state
|
self._attr_native_value = state
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue