2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Speedtest.net internet speed testing sensor."""
|
2021-05-28 09:10:01 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-30 06:28:04 +00:00
|
|
|
from typing import Any, cast
|
2021-05-28 09:10:01 +00:00
|
|
|
|
2021-09-30 06:28:04 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2021-05-28 09:10:01 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2019-02-04 08:47:04 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION
|
2021-09-30 06:28:04 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-11-22 17:14:15 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
2021-10-14 22:37:31 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2021-05-28 09:10:01 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-07-06 22:18:56 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2021-09-30 06:28:04 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2020-08-30 13:50:33 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2020-06-10 16:33:48 +00:00
|
|
|
|
2021-12-13 08:39:13 +00:00
|
|
|
from . import SpeedTestDataCoordinator
|
2020-06-10 16:33:48 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_BYTES_RECEIVED,
|
|
|
|
ATTR_BYTES_SENT,
|
|
|
|
ATTR_SERVER_COUNTRY,
|
|
|
|
ATTR_SERVER_ID,
|
|
|
|
ATTR_SERVER_NAME,
|
|
|
|
ATTRIBUTION,
|
|
|
|
DEFAULT_NAME,
|
|
|
|
DOMAIN,
|
|
|
|
ICON,
|
|
|
|
SENSOR_TYPES,
|
2021-09-30 06:28:04 +00:00
|
|
|
SpeedtestSensorEntityDescription,
|
2020-06-10 16:33:48 +00:00
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-02-04 08:47:04 +00:00
|
|
|
|
2021-05-28 09:10:01 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-06-10 16:33:48 +00:00
|
|
|
"""Set up the Speedtestdotnet sensors."""
|
|
|
|
speedtest_coordinator = hass.data[DOMAIN]
|
2021-05-28 09:10:01 +00:00
|
|
|
async_add_entities(
|
2021-08-02 15:00:25 +00:00
|
|
|
SpeedtestSensor(speedtest_coordinator, description)
|
|
|
|
for description in SENSOR_TYPES
|
2021-05-28 09:10:01 +00:00
|
|
|
)
|
2019-02-04 08:47:04 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class SpeedtestSensor(CoordinatorEntity, RestoreEntity, SensorEntity):
|
2019-02-04 08:47:04 +00:00
|
|
|
"""Implementation of a speedtest.net sensor."""
|
|
|
|
|
2021-05-28 09:10:01 +00:00
|
|
|
coordinator: SpeedTestDataCoordinator
|
2021-09-30 06:28:04 +00:00
|
|
|
entity_description: SpeedtestSensorEntityDescription
|
2021-05-28 09:10:01 +00:00
|
|
|
_attr_icon = ICON
|
|
|
|
|
2021-08-02 15:00:25 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: SpeedTestDataCoordinator,
|
2021-09-30 06:28:04 +00:00
|
|
|
description: SpeedtestSensorEntityDescription,
|
2021-08-02 15:00:25 +00:00
|
|
|
) -> None:
|
2019-02-04 08:47:04 +00:00
|
|
|
"""Initialize the sensor."""
|
2020-08-30 13:50:33 +00:00
|
|
|
super().__init__(coordinator)
|
2021-08-02 15:00:25 +00:00
|
|
|
self.entity_description = description
|
|
|
|
self._attr_name = f"{DEFAULT_NAME} {description.name}"
|
|
|
|
self._attr_unique_id = description.key
|
2021-09-30 06:28:04 +00:00
|
|
|
self._state: StateType = None
|
2021-07-22 10:25:54 +00:00
|
|
|
self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
2021-10-14 22:37:31 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, self.coordinator.config_entry.entry_id)},
|
|
|
|
name=DEFAULT_NAME,
|
2021-11-22 17:14:15 +00:00
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
2021-10-14 22:37:31 +00:00
|
|
|
configuration_url="https://www.speedtest.net/",
|
|
|
|
)
|
2021-09-30 06:28:04 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self) -> StateType:
|
|
|
|
"""Return native value for entity."""
|
|
|
|
if self.coordinator.data:
|
|
|
|
state = self.coordinator.data[self.entity_description.key]
|
|
|
|
self._state = cast(StateType, self.entity_description.value(state))
|
|
|
|
return self._state
|
2019-02-04 08:47:04 +00:00
|
|
|
|
|
|
|
@property
|
2021-07-22 10:25:54 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
2019-02-04 08:47:04 +00:00
|
|
|
"""Return the state attributes."""
|
2021-07-22 10:25:54 +00:00
|
|
|
if self.coordinator.data:
|
|
|
|
self._attrs.update(
|
|
|
|
{
|
|
|
|
ATTR_SERVER_NAME: self.coordinator.data["server"]["name"],
|
|
|
|
ATTR_SERVER_COUNTRY: self.coordinator.data["server"]["country"],
|
|
|
|
ATTR_SERVER_ID: self.coordinator.data["server"]["id"],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-08-02 15:00:25 +00:00
|
|
|
if self.entity_description.key == "download":
|
2021-07-22 10:25:54 +00:00
|
|
|
self._attrs[ATTR_BYTES_RECEIVED] = self.coordinator.data[
|
2021-09-23 18:44:59 +00:00
|
|
|
ATTR_BYTES_RECEIVED
|
2021-07-22 10:25:54 +00:00
|
|
|
]
|
2021-08-02 15:00:25 +00:00
|
|
|
elif self.entity_description.key == "upload":
|
2021-09-23 18:44:59 +00:00
|
|
|
self._attrs[ATTR_BYTES_SENT] = self.coordinator.data[ATTR_BYTES_SENT]
|
2021-07-22 10:25:54 +00:00
|
|
|
|
|
|
|
return self._attrs
|
2019-02-04 08:47:04 +00:00
|
|
|
|
2021-05-28 09:10:01 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2019-02-04 08:47:04 +00:00
|
|
|
"""Handle entity which will be added."""
|
2020-07-06 22:18:56 +00:00
|
|
|
await super().async_added_to_hass()
|
2021-10-30 14:29:07 +00:00
|
|
|
if state := await self.async_get_last_state():
|
2021-09-30 06:28:04 +00:00
|
|
|
self._state = state.state
|