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