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
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2021-08-02 15:00:25 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
2021-05-28 09:10:01 +00:00
|
|
|
from homeassistant.components.speedtestdotnet import SpeedTestDataCoordinator
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2019-02-04 08:47:04 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION
|
2021-05-28 09:10:01 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-07-06 22:18:56 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2020-08-30 13:50:33 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
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,
|
|
|
|
)
|
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
|
|
|
|
_attr_icon = ICON
|
|
|
|
|
2021-08-02 15:00:25 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: SpeedTestDataCoordinator,
|
|
|
|
description: SensorEntityDescription,
|
|
|
|
) -> 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-07-22 10:25:54 +00:00
|
|
|
self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
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()
|
2020-07-28 05:57:36 +00:00
|
|
|
state = await self.async_get_last_state()
|
|
|
|
if state:
|
2021-08-12 15:40:55 +00:00
|
|
|
self._attr_native_value = state.state
|
2020-07-06 22:18:56 +00:00
|
|
|
|
|
|
|
@callback
|
2021-05-28 09:10:01 +00:00
|
|
|
def update() -> None:
|
2020-07-06 22:18:56 +00: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 10:25:54 +00:00
|
|
|
def _update_state(self):
|
2020-07-06 22:18:56 +00:00
|
|
|
"""Update sensors state."""
|
2021-07-22 10:25:54 +00:00
|
|
|
if self.coordinator.data:
|
2021-08-02 15:00:25 +00:00
|
|
|
if self.entity_description.key == "ping":
|
2021-08-12 15:40:55 +00:00
|
|
|
self._attr_native_value = self.coordinator.data["ping"]
|
2021-08-02 15:00:25 +00:00
|
|
|
elif self.entity_description.key == "download":
|
2021-08-12 15:40:55 +00:00
|
|
|
self._attr_native_value = round(
|
|
|
|
self.coordinator.data["download"] / 10 ** 6, 2
|
|
|
|
)
|
2021-08-02 15:00:25 +00:00
|
|
|
elif self.entity_description.key == "upload":
|
2021-08-12 15:40:55 +00:00
|
|
|
self._attr_native_value = round(
|
|
|
|
self.coordinator.data["upload"] / 10 ** 6, 2
|
|
|
|
)
|