2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Fast.com internet speed testing sensor."""
|
2024-03-08 13:15:26 +00:00
|
|
|
|
2021-07-05 08:19:37 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-07-24 14:59:39 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorStateClass,
|
|
|
|
)
|
2023-11-21 08:59:34 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-12-10 10:41:44 +00:00
|
|
|
from homeassistant.const import UnitOfDataRate
|
2023-12-11 21:28:04 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-12-12 09:41:00 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
2021-07-05 08:19:37 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-12-11 21:28:04 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2019-02-02 05:50:22 +00:00
|
|
|
|
2023-12-11 21:28:04 +00:00
|
|
|
from .const import DOMAIN
|
2024-03-15 13:42:07 +00:00
|
|
|
from .coordinator import FastdotcomDataUpdateCoordinator
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-02-02 05:50:22 +00:00
|
|
|
|
2023-11-21 08:59:34 +00:00
|
|
|
async def async_setup_entry(
|
2021-07-05 08:19:37 +00:00
|
|
|
hass: HomeAssistant,
|
2023-11-21 08:59:34 +00:00
|
|
|
entry: ConfigEntry,
|
2021-07-05 08:19:37 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2019-02-02 05:50:22 +00:00
|
|
|
"""Set up the Fast.com sensor."""
|
2024-03-15 13:42:07 +00:00
|
|
|
coordinator: FastdotcomDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
2023-12-11 21:28:04 +00:00
|
|
|
async_add_entities([SpeedtestSensor(entry.entry_id, coordinator)])
|
2019-02-02 05:50:22 +00:00
|
|
|
|
|
|
|
|
2024-03-15 13:42:07 +00:00
|
|
|
class SpeedtestSensor(CoordinatorEntity[FastdotcomDataUpdateCoordinator], SensorEntity):
|
2023-11-21 08:59:34 +00:00
|
|
|
"""Implementation of a Fast.com sensor."""
|
2019-02-02 05:50:22 +00:00
|
|
|
|
2023-12-12 09:41:00 +00:00
|
|
|
_attr_translation_key = "download"
|
2022-12-10 10:41:44 +00:00
|
|
|
_attr_device_class = SensorDeviceClass.DATA_RATE
|
|
|
|
_attr_native_unit_of_measurement = UnitOfDataRate.MEGABITS_PER_SECOND
|
2023-07-24 14:59:39 +00:00
|
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
2021-07-05 08:19:37 +00:00
|
|
|
_attr_should_poll = False
|
2023-12-12 09:41:00 +00:00
|
|
|
_attr_has_entity_name = True
|
2019-02-02 05:50:22 +00:00
|
|
|
|
2023-12-11 21:28:04 +00:00
|
|
|
def __init__(
|
2024-03-15 13:42:07 +00:00
|
|
|
self, entry_id: str, coordinator: FastdotcomDataUpdateCoordinator
|
2023-12-11 21:28:04 +00:00
|
|
|
) -> None:
|
2021-07-05 08:19:37 +00:00
|
|
|
"""Initialize the sensor."""
|
2023-12-11 21:28:04 +00:00
|
|
|
super().__init__(coordinator)
|
2023-11-30 16:11:28 +00:00
|
|
|
self._attr_unique_id = entry_id
|
2023-12-12 09:41:00 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, entry_id)},
|
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
|
|
|
configuration_url="https://www.fast.com",
|
|
|
|
)
|
2019-02-02 05:50:22 +00:00
|
|
|
|
2023-12-11 21:28:04 +00:00
|
|
|
@property
|
|
|
|
def native_value(
|
|
|
|
self,
|
|
|
|
) -> float:
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self.coordinator.data
|