Add Speedtestdotnet config_flow (#36254)

This commit is contained in:
Rami Mosleh authored and GitHub committed 2020-06-10 18:33:48 +02:00
1 parent c65e72886c
commit 2c1a76cf92
14 files changed
+678 -104

No files matched your search

@@ -2,54 +2,67 @@
import logging
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.entity import Entity
from .const import DATA_UPDATED, DOMAIN as SPEEDTESTDOTNET_DOMAIN, SENSOR_TYPES
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,
)
_LOGGER = logging.getLogger(__name__)
ATTR_BYTES_RECEIVED = "bytes_received"
ATTR_BYTES_SENT = "bytes_sent"
ATTR_SERVER_COUNTRY = "server_country"
ATTR_SERVER_HOST = "server_host"
ATTR_SERVER_ID = "server_id"
ATTR_SERVER_LATENCY = "latency"
ATTR_SERVER_NAME = "server_name"
ATTRIBUTION = "Data retrieved from Speedtest.net by Ookla"
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Speedtestdotnet sensors."""
ICON = "mdi:speedometer"
speedtest_coordinator = hass.data[DOMAIN]
entities = []
for sensor_type in SENSOR_TYPES:
entities.append(SpeedtestSensor(speedtest_coordinator, sensor_type))
async_add_entities(entities)
async def async_setup_platform(hass, config, async_add_entities, discovery_info):
"""Set up the Speedtest.net sensor."""
data = hass.data[SPEEDTESTDOTNET_DOMAIN]
async_add_entities([SpeedtestSensor(data, sensor) for sensor in discovery_info])
class SpeedtestSensor(RestoreEntity):
class SpeedtestSensor(Entity):
"""Implementation of a speedtest.net sensor."""
def __init__(self, speedtest_data, sensor_type):
def __init__(self, coordinator, sensor_type):
"""Initialize the sensor."""
self._name = SENSOR_TYPES[sensor_type][0]
self.speedtest_client = speedtest_data
self.coordinator = coordinator
self.type = sensor_type
self._state = None
self._data = None
self._unit_of_measurement = SENSOR_TYPES[self.type][1]
@property
def name(self):
"""Return the name of the sensor."""
return "{} {}".format("Speedtest", self._name)
return f"{DEFAULT_NAME} {self._name}"
@property
def unique_id(self):
"""Return sensor unique_id."""
return self.type
@property
def state(self):
"""Return the state of the device."""
return self._state
state = None
if self.type == "ping":
state = self.coordinator.data["ping"]
elif self.type == "download":
state = round(self.coordinator.data["download"] / 10 ** 6, 2)
elif self.type == "upload":
state = round(self.coordinator.data["upload"] / 10 ** 6, 2)
return state
@property
def unit_of_measurement(self):
@@ -69,47 +82,27 @@ class SpeedtestSensor(RestoreEntity):
@property
def device_state_attributes(self):
"""Return the state attributes."""
attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
if self._data is not None:
return attributes.update(
{
ATTR_BYTES_RECEIVED: self._data["bytes_received"],
ATTR_BYTES_SENT: self._data["bytes_sent"],
ATTR_SERVER_COUNTRY: self._data["server"]["country"],
ATTR_SERVER_ID: self._data["server"]["id"],
ATTR_SERVER_LATENCY: self._data["server"]["latency"],
ATTR_SERVER_NAME: self._data["server"]["name"],
}
)
attributes = {
ATTR_ATTRIBUTION: ATTRIBUTION,
ATTR_SERVER_NAME: self.coordinator.data["server"]["name"],
ATTR_SERVER_COUNTRY: self.coordinator.data["server"]["country"],
ATTR_SERVER_ID: self.coordinator.data["server"]["id"],
}
if self.type == "download":
attributes[ATTR_BYTES_RECEIVED] = self.coordinator.data["bytes_received"]
if self.type == "upload":
attributes[ATTR_BYTES_SENT] = self.coordinator.data["bytes_sent"]
return attributes
async def async_added_to_hass(self):
"""Handle entity which will be added."""
await super().async_added_to_hass()
state = await self.async_get_last_state()
if not state:
return
self._state = state.state
self.async_on_remove(
async_dispatcher_connect(
self.hass, DATA_UPDATED, self._schedule_immediate_update
)
self.coordinator.async_add_listener(self.async_write_ha_state)
)
def update(self):
"""Get the latest data and update the states."""
self._data = self.speedtest_client.data
if self._data is None:
return
if self.type == "ping":
self._state = self._data["ping"]
elif self.type == "download":
self._state = round(self._data["download"] / 10 ** 6, 2)
elif self.type == "upload":
self._state = round(self._data["upload"] / 10 ** 6, 2)
@callback
def _schedule_immediate_update(self):
self.async_schedule_update_ha_state(True)
async def async_update(self):
"""Request coordinator to update data."""
await self.coordinator.async_request_refresh()