2019-02-13 21:21:14 +01:00
|
|
|
"""Support for Fast.com internet speed testing sensor."""
|
2021-07-05 11:19:37 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2023-07-24 16:59:39 +02:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorStateClass,
|
|
|
|
)
|
2023-11-21 09:59:34 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-12-10 11:41:44 +01:00
|
|
|
from homeassistant.const import UnitOfDataRate
|
2021-07-05 11:19:37 +03:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2019-02-01 21:50:22 -08:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-07-05 11:19:37 +03:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-02-01 21:50:22 -08:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
|
|
|
|
2023-11-21 09:59:34 +01:00
|
|
|
from .const import DATA_UPDATED, DOMAIN
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2019-02-01 21:50:22 -08:00
|
|
|
|
2023-11-21 09:59:34 +01:00
|
|
|
async def async_setup_entry(
|
2021-07-05 11:19:37 +03:00
|
|
|
hass: HomeAssistant,
|
2023-11-21 09:59:34 +01:00
|
|
|
entry: ConfigEntry,
|
2021-07-05 11:19:37 +03:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2019-02-01 21:50:22 -08:00
|
|
|
"""Set up the Fast.com sensor."""
|
2023-11-21 09:59:34 +01:00
|
|
|
async_add_entities([SpeedtestSensor(hass.data[DOMAIN])])
|
2019-02-01 21:50:22 -08:00
|
|
|
|
|
|
|
|
2023-05-24 12:49:35 +02:00
|
|
|
# pylint: disable-next=hass-invalid-inheritance # needs fixing
|
2021-03-22 19:45:17 +01:00
|
|
|
class SpeedtestSensor(RestoreEntity, SensorEntity):
|
2023-11-21 09:59:34 +01:00
|
|
|
"""Implementation of a Fast.com sensor."""
|
2019-02-01 21:50:22 -08:00
|
|
|
|
2021-07-05 11:19:37 +03:00
|
|
|
_attr_name = "Fast.com Download"
|
2022-12-10 11:41:44 +01:00
|
|
|
_attr_device_class = SensorDeviceClass.DATA_RATE
|
|
|
|
_attr_native_unit_of_measurement = UnitOfDataRate.MEGABITS_PER_SECOND
|
2023-07-24 16:59:39 +02:00
|
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
2022-12-10 11:41:44 +01:00
|
|
|
_attr_icon = "mdi:speedometer"
|
2021-07-05 11:19:37 +03:00
|
|
|
_attr_should_poll = False
|
2019-02-01 21:50:22 -08:00
|
|
|
|
2021-07-05 11:19:37 +03:00
|
|
|
def __init__(self, speedtest_data: dict[str, Any]) -> None:
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._speedtest_data = speedtest_data
|
2019-02-01 21:50:22 -08:00
|
|
|
|
2021-07-05 11:19:37 +03:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2019-02-01 21:50:22 -08:00
|
|
|
"""Handle entity which will be added."""
|
|
|
|
await super().async_added_to_hass()
|
2020-04-02 09:25:33 -07:00
|
|
|
|
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, DATA_UPDATED, self._schedule_immediate_update
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-10-30 16:29:07 +02:00
|
|
|
if not (state := await self.async_get_last_state()):
|
2019-02-01 21:50:22 -08:00
|
|
|
return
|
2021-08-12 14:23:56 +02:00
|
|
|
self._attr_native_value = state.state
|
2019-02-01 21:50:22 -08:00
|
|
|
|
2021-07-05 11:19:37 +03:00
|
|
|
def update(self) -> None:
|
2019-02-01 21:50:22 -08:00
|
|
|
"""Get the latest data and update the states."""
|
2021-10-17 20:05:11 +02:00
|
|
|
if (data := self._speedtest_data.data) is None: # type: ignore[attr-defined]
|
2019-02-01 21:50:22 -08:00
|
|
|
return
|
2021-08-12 14:23:56 +02:00
|
|
|
self._attr_native_value = data["download"]
|
2019-02-01 21:50:22 -08:00
|
|
|
|
|
|
|
@callback
|
2021-07-05 11:19:37 +03:00
|
|
|
def _schedule_immediate_update(self) -> None:
|
2019-02-01 21:50:22 -08:00
|
|
|
self.async_schedule_update_ha_state(True)
|