2016-02-04 23:21:37 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.sensor.speedtest
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Speedtest.net sensor based on speedtest-cli.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.speedtest/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
from datetime import timedelta
|
|
|
|
from subprocess import check_output
|
|
|
|
from homeassistant.util import Throttle
|
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-02-12 00:09:51 +00:00
|
|
|
from homeassistant.helpers import event
|
2016-02-04 23:21:37 +00:00
|
|
|
|
|
|
|
REQUIREMENTS = ['speedtest-cli==0.3.4']
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-02-04 23:39:09 +00:00
|
|
|
_SPEEDTEST_REGEX = re.compile(r'Ping:\s(\d+\.\d+)\sms\nDownload:\s(\d+\.\d+)'
|
|
|
|
r'\sMbit/s\nUpload:\s(\d+\.\d+)\sMbit/s\n')
|
2016-02-04 23:21:37 +00:00
|
|
|
|
2016-02-12 00:09:51 +00:00
|
|
|
CONF_MONITORED_CONDITIONS = 'monitored_conditions'
|
|
|
|
CONF_MINUTE = 'minute'
|
|
|
|
CONF_HOUR = 'hour'
|
|
|
|
CONF_DAY = 'day'
|
2016-02-04 23:21:37 +00:00
|
|
|
SENSOR_TYPES = {
|
|
|
|
'ping': ['Ping', 'ms'],
|
|
|
|
'download': ['Download', 'Mbit/s'],
|
|
|
|
'upload': ['Upload', 'Mbit/s'],
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return cached results if last scan was less then this time ago
|
2016-02-12 00:09:51 +00:00
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
2016-02-04 23:21:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
|
|
""" Setup the Speedtest sensor. """
|
|
|
|
|
2016-02-12 00:09:51 +00:00
|
|
|
data = SpeedtestData(hass, event, config)
|
2016-02-04 23:21:37 +00:00
|
|
|
|
|
|
|
dev = []
|
2016-02-12 00:09:51 +00:00
|
|
|
for sensor in config[CONF_MONITORED_CONDITIONS]:
|
|
|
|
if sensor not in SENSOR_TYPES:
|
|
|
|
_LOGGER.error('Sensor type: "%s" does not exist', sensor)
|
2016-02-04 23:21:37 +00:00
|
|
|
else:
|
2016-02-12 00:09:51 +00:00
|
|
|
dev.append(SpeedtestSensor(data, sensor))
|
2016-02-04 23:21:37 +00:00
|
|
|
|
|
|
|
add_devices(dev)
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class SpeedtestSensor(Entity):
|
|
|
|
""" Implements a speedtest.net sensor. """
|
|
|
|
|
|
|
|
def __init__(self, speedtest_data, sensor_type):
|
|
|
|
self._name = SENSOR_TYPES[sensor_type][0]
|
|
|
|
self.speedtest_client = speedtest_data
|
|
|
|
self.type = sensor_type
|
|
|
|
self._state = None
|
|
|
|
self._unit_of_measurement = SENSOR_TYPES[self.type][1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-02-12 00:09:51 +00:00
|
|
|
return '{} {}'.format('Speedtest', self._name)
|
2016-02-04 23:21:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
""" Returns the state of the device. """
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
""" Unit of measurement of this entity, if any. """
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
""" Gets the latest data from Forecast.io and updates the states. """
|
|
|
|
data = self.speedtest_client.data
|
2016-02-12 00:09:51 +00:00
|
|
|
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']
|
2016-02-04 23:21:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SpeedtestData(object):
|
|
|
|
""" Gets the latest data from speedtest.net. """
|
|
|
|
|
2016-02-12 00:09:51 +00:00
|
|
|
def __init__(self, hass, event, config):
|
2016-02-04 23:21:37 +00:00
|
|
|
self.data = None
|
2016-02-12 00:09:51 +00:00
|
|
|
self.hass = hass
|
|
|
|
self.path = hass.config.path
|
|
|
|
self.event = event
|
|
|
|
self.event.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))
|
2016-02-04 23:21:37 +00:00
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
2016-02-12 00:09:51 +00:00
|
|
|
def update(self, event=None):
|
2016-02-04 23:21:37 +00:00
|
|
|
""" Gets the latest data from speedtest.net. """
|
|
|
|
_LOGGER.info('Executing speedtest')
|
|
|
|
re_output = _SPEEDTEST_REGEX.split(
|
|
|
|
check_output([sys.executable, self.path(
|
|
|
|
'lib', 'speedtest_cli.py'), '--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)}
|