2019-02-13 21:21:14 +01:00
|
|
|
"""Support for Speedtest.net internet speed testing sensor."""
|
2021-05-28 11:10:01 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2021-08-02 17:00:25 +02:00
|
|
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
2021-05-28 11:10:01 +02:00
|
|
|
from homeassistant.components.speedtestdotnet import SpeedTestDataCoordinator
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2019-02-04 00:47:04 -08:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION
|
2021-05-28 11:10:01 +02:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-07-07 01:18:56 +03:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2020-08-30 15:50:33 +02:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2020-06-10 19:33:48 +03: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,
|
|
|
|
)
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2019-02-04 00:47:04 -08:00
|
|
|
|
2021-05-28 11:10:01 +02:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-06-10 19:33:48 +03:00
|
|
|
"""Set up the Speedtestdotnet sensors."""
|
|
|
|
speedtest_coordinator = hass.data[DOMAIN]
|
2021-05-28 11:10:01 +02:00
|
|
|
async_add_entities(
|
2021-08-02 17:00:25 +02:00
|
|
|
SpeedtestSensor(speedtest_coordinator, description)
|
|
|
|
for description in SENSOR_TYPES
|
2021-05-28 11:10:01 +02:00
|
|
|
)
|
2019-02-04 00:47:04 -08:00
|
|
|
|
|
|
|
|
2021-03-22 19:54:14 +01:00
|
|
|
class SpeedtestSensor(CoordinatorEntity, RestoreEntity, SensorEntity):
|
2019-02-04 00:47:04 -08:00
|
|
|
"""Implementation of a speedtest.net sensor."""
|
|
|
|
|
2021-05-28 11:10:01 +02:00
|
|
|
coordinator: SpeedTestDataCoordinator
|
|
|
|
_attr_icon = ICON
|
|
|
|
|
2021-08-02 17:00:25 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: SpeedTestDataCoordinator,
|
|
|
|
description: SensorEntityDescription,
|
|
|
|
) -> None:
|
2019-02-04 00:47:04 -08:00
|
|
|
"""Initialize the sensor."""
|
2020-08-30 15:50:33 +02:00
|
|
|
super().__init__(coordinator)
|
2021-08-02 17:00:25 +02:00
|
|
|
self.entity_description = description
|
|
|
|
self._attr_name = f"{DEFAULT_NAME} {description.name}"
|
|
|
|
self._attr_unique_id = description.key
|
2021-07-22 13:25:54 +03:00
|
|
|
self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
2019-02-04 00:47:04 -08:00
|
|
|
|
|
|
|
@property
|
2021-07-22 13:25:54 +03:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
2019-02-04 00:47:04 -08:00
|
|
|
"""Return the state attributes."""
|
2021-07-22 13:25:54 +03: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 17:00:25 +02:00
|
|
|
if self.entity_description.key == "download":
|
2021-07-22 13:25:54 +03:00
|
|
|
self._attrs[ATTR_BYTES_RECEIVED] = self.coordinator.data[
|
2021-09-23 21:44:59 +03:00
|
|
|
ATTR_BYTES_RECEIVED
|
2021-07-22 13:25:54 +03:00
|
|
|
]
|
2021-08-02 17:00:25 +02:00
|
|
|
elif self.entity_description.key == "upload":
|
2021-09-23 21:44:59 +03:00
|
|
|
self._attrs[ATTR_BYTES_SENT] = self.coordinator.data[ATTR_BYTES_SENT]
|
2021-07-22 13:25:54 +03:00
|
|
|
|
|
|
|
return self._attrs
|
2019-02-04 00:47:04 -08:00
|
|
|
|
2021-05-28 11:10:01 +02:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2019-02-04 00:47:04 -08:00
|
|
|
"""Handle entity which will be added."""
|
2020-07-07 01:18:56 +03:00
|
|
|
await super().async_added_to_hass()
|
2020-07-27 19:57:36 -10:00
|
|
|
state = await self.async_get_last_state()
|
|
|
|
if state:
|
2021-08-12 17:40:55 +02:00
|
|
|
self._attr_native_value = state.state
|
2020-07-07 01:18:56 +03:00
|
|
|
|
|
|
|
@callback
|
2021-05-28 11:10:01 +02:00
|
|
|
def update() -> None:
|
2020-07-07 01:18:56 +03:00
|
|
|
"""Update state."""
|
|
|
|
self._update_state()
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
self.async_on_remove(self.coordinator.async_add_listener(update))
|
|
|
|
self._update_state()
|
|
|
|
|
2021-07-22 13:25:54 +03:00
|
|
|
def _update_state(self):
|
2020-07-07 01:18:56 +03:00
|
|
|
"""Update sensors state."""
|
2021-07-22 13:25:54 +03:00
|
|
|
if self.coordinator.data:
|
2021-08-02 17:00:25 +02:00
|
|
|
if self.entity_description.key == "ping":
|
2021-08-12 17:40:55 +02:00
|
|
|
self._attr_native_value = self.coordinator.data["ping"]
|
2021-08-02 17:00:25 +02:00
|
|
|
elif self.entity_description.key == "download":
|
2021-08-12 17:40:55 +02:00
|
|
|
self._attr_native_value = round(
|
|
|
|
self.coordinator.data["download"] / 10 ** 6, 2
|
|
|
|
)
|
2021-08-02 17:00:25 +02:00
|
|
|
elif self.entity_description.key == "upload":
|
2021-08-12 17:40:55 +02:00
|
|
|
self._attr_native_value = round(
|
|
|
|
self.coordinator.data["upload"] / 10 ** 6, 2
|
|
|
|
)
|