mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Bring back custom scan intervals and service for speedtest.net component (#1980)
* Bring back the functionality that was removed in PR 1717. This includes the speedtest service and the ability to define the scan times in the configuration file. Restore default functionality of 1 scan per hour on the hour. * remove unnecessary code.
This commit is contained in:
parent
ec9544b9c3
commit
26ea4e41cb
@ -7,11 +7,12 @@ https://home-assistant.io/components/sensor.speedtest/
|
||||
import logging
|
||||
import re
|
||||
import sys
|
||||
from datetime import timedelta
|
||||
from subprocess import check_output
|
||||
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.components.sensor import DOMAIN
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import Throttle
|
||||
from homeassistant.helpers.event import track_time_change
|
||||
|
||||
REQUIREMENTS = ['speedtest-cli==0.3.4']
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -30,13 +31,10 @@ SENSOR_TYPES = {
|
||||
'upload': ['Upload', 'Mbit/s'],
|
||||
}
|
||||
|
||||
# Return cached results if last scan was less then this time ago
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup the Speedtest sensor."""
|
||||
data = SpeedtestData()
|
||||
data = SpeedtestData(hass, config)
|
||||
dev = []
|
||||
for sensor in config[CONF_MONITORED_CONDITIONS]:
|
||||
if sensor not in SENSOR_TYPES:
|
||||
@ -46,6 +44,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
|
||||
add_devices(dev)
|
||||
|
||||
def update(call=None):
|
||||
"""Update service for manual updates."""
|
||||
data.update(dt_util.now())
|
||||
for sensor in dev:
|
||||
sensor.update()
|
||||
|
||||
hass.services.register(DOMAIN, 'update_speedtest', update)
|
||||
|
||||
|
||||
# pylint: disable=too-few-public-methods
|
||||
class SpeedtestSensor(Entity):
|
||||
@ -76,7 +82,6 @@ class SpeedtestSensor(Entity):
|
||||
|
||||
def update(self):
|
||||
"""Get the latest data and update the states."""
|
||||
self.speedtest_client.update()
|
||||
data = self.speedtest_client.data
|
||||
if data is None:
|
||||
return
|
||||
@ -92,12 +97,15 @@ class SpeedtestSensor(Entity):
|
||||
class SpeedtestData(object):
|
||||
"""Get the latest data from speedtest.net."""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, hass, config):
|
||||
"""Initialize the data object."""
|
||||
self.data = None
|
||||
track_time_change(hass, self.update,
|
||||
minute=config.get(CONF_MINUTE, 0),
|
||||
hour=config.get(CONF_HOUR, None),
|
||||
day=config.get(CONF_DAY, None))
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
def update(self, now):
|
||||
"""Get the latest data from speedtest.net."""
|
||||
import speedtest_cli
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user