mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Fix speedtest with external installed package
This commit is contained in:
parent
ecc440f459
commit
b4ddc86304
@ -10,10 +10,7 @@ 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.helpers.event import track_time_change
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
REQUIREMENTS = ['speedtest-cli==0.3.4']
|
||||
@ -34,12 +31,12 @@ SENSOR_TYPES = {
|
||||
}
|
||||
|
||||
# Return cached results if last scan was less then this time ago
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup the Speedtest sensor."""
|
||||
data = SpeedtestData(hass, config)
|
||||
data = SpeedtestData()
|
||||
dev = []
|
||||
for sensor in config[CONF_MONITORED_CONDITIONS]:
|
||||
if sensor not in SENSOR_TYPES:
|
||||
@ -49,14 +46,6 @@ 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):
|
||||
@ -87,36 +76,35 @@ 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 not None:
|
||||
if self.type == 'ping':
|
||||
self._state = data['ping']
|
||||
elif self.type == 'download':
|
||||
self._state = data['download']
|
||||
elif self.type == 'upload':
|
||||
self._state = data['upload']
|
||||
if data is None:
|
||||
return
|
||||
|
||||
elif self.type == 'ping':
|
||||
self._state = data['ping']
|
||||
elif self.type == 'download':
|
||||
self._state = data['download']
|
||||
elif self.type == 'upload':
|
||||
self._state = data['upload']
|
||||
|
||||
|
||||
class SpeedtestData(object):
|
||||
"""Get the latest data from speedtest.net."""
|
||||
|
||||
def __init__(self, hass, config):
|
||||
def __init__(self):
|
||||
"""Initialize the data object."""
|
||||
self.data = None
|
||||
self.hass = hass
|
||||
self.path = hass.config.path
|
||||
track_time_change(self.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, now):
|
||||
def update(self):
|
||||
"""Get the latest data from speedtest.net."""
|
||||
import speedtest_cli
|
||||
|
||||
_LOGGER.info('Executing speedtest')
|
||||
re_output = _SPEEDTEST_REGEX.split(
|
||||
check_output([sys.executable, self.path(
|
||||
'lib', 'speedtest_cli.py'), '--simple']).decode("utf-8"))
|
||||
check_output([sys.executable, speedtest_cli.__file__,
|
||||
'--simple']).decode("utf-8"))
|
||||
self.data = {'ping': round(float(re_output[1]), 2),
|
||||
'download': round(float(re_output[2]), 2),
|
||||
'upload': round(float(re_output[3]), 2)}
|
||||
|
Loading…
x
Reference in New Issue
Block a user