2016-02-04 18:21:37 -05:00
|
|
|
"""
|
2016-02-23 06:21:49 +01:00
|
|
|
Support for Speedtest.net based on speedtest-cli.
|
2016-02-04 18:21:37 -05:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.speedtest/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import re
|
2016-02-18 21:27:50 -08:00
|
|
|
import sys
|
2016-02-04 18:21:37 -05:00
|
|
|
from datetime import timedelta
|
|
|
|
from subprocess import check_output
|
2016-02-18 21:27:50 -08:00
|
|
|
|
2016-02-04 18:21:37 -05:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-02-18 21:27:50 -08:00
|
|
|
from homeassistant.util import Throttle
|
2016-02-04 18:21:37 -05:00
|
|
|
|
|
|
|
REQUIREMENTS = ['speedtest-cli==0.3.4']
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-02-12 11:44:24 -05:00
|
|
|
_SPEEDTEST_REGEX = re.compile(r'Ping:\s(\d+\.\d+)\sms[\r\n]+'
|
|
|
|
r'Download:\s(\d+\.\d+)\sMbit/s[\r\n]+'
|
|
|
|
r'Upload:\s(\d+\.\d+)\sMbit/s[\r\n]+')
|
2016-02-04 18:21:37 -05:00
|
|
|
|
2016-02-11 19:09:51 -05:00
|
|
|
CONF_MONITORED_CONDITIONS = 'monitored_conditions'
|
|
|
|
CONF_MINUTE = 'minute'
|
|
|
|
CONF_HOUR = 'hour'
|
|
|
|
CONF_DAY = 'day'
|
2016-02-04 18:21:37 -05: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-04-06 07:38:35 -07:00
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15)
|
2016-02-04 18:21:37 -05:00
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-02-23 06:21:49 +01:00
|
|
|
"""Setup the Speedtest sensor."""
|
2016-04-06 07:38:35 -07:00
|
|
|
data = SpeedtestData()
|
2016-02-04 18:21:37 -05:00
|
|
|
dev = []
|
2016-02-11 19:09:51 -05: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 18:21:37 -05:00
|
|
|
else:
|
2016-02-11 19:09:51 -05:00
|
|
|
dev.append(SpeedtestSensor(data, sensor))
|
2016-02-04 18:21:37 -05:00
|
|
|
|
|
|
|
add_devices(dev)
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class SpeedtestSensor(Entity):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Implementation of a speedtest.net sensor."""
|
2016-02-04 18:21:37 -05:00
|
|
|
|
|
|
|
def __init__(self, speedtest_data, sensor_type):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Initialize the sensor."""
|
2016-02-04 18:21:37 -05:00
|
|
|
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-03-08 16:46:34 +01:00
|
|
|
"""Return the name of the sensor."""
|
2016-02-11 19:09:51 -05:00
|
|
|
return '{} {}'.format('Speedtest', self._name)
|
2016-02-04 18:21:37 -05:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the state of the device."""
|
2016-02-04 18:21:37 -05:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2016-02-04 18:21:37 -05:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Get the latest data and update the states."""
|
2016-04-06 07:38:35 -07:00
|
|
|
self.speedtest_client.update()
|
2016-02-04 18:21:37 -05:00
|
|
|
data = self.speedtest_client.data
|
2016-04-06 07:38:35 -07:00
|
|
|
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']
|
2016-02-04 18:21:37 -05:00
|
|
|
|
|
|
|
|
|
|
|
class SpeedtestData(object):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Get the latest data from speedtest.net."""
|
2016-02-04 18:21:37 -05:00
|
|
|
|
2016-04-06 07:38:35 -07:00
|
|
|
def __init__(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Initialize the data object."""
|
2016-02-04 18:21:37 -05:00
|
|
|
self.data = None
|
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
2016-04-06 07:38:35 -07:00
|
|
|
def update(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Get the latest data from speedtest.net."""
|
2016-04-06 07:38:35 -07:00
|
|
|
import speedtest_cli
|
|
|
|
|
2016-02-04 18:21:37 -05:00
|
|
|
_LOGGER.info('Executing speedtest')
|
|
|
|
re_output = _SPEEDTEST_REGEX.split(
|
2016-04-06 07:38:35 -07:00
|
|
|
check_output([sys.executable, speedtest_cli.__file__,
|
|
|
|
'--simple']).decode("utf-8"))
|
2016-02-04 18:21:37 -05:00
|
|
|
self.data = {'ping': round(float(re_output[1]), 2),
|
|
|
|
'download': round(float(re_output[2]), 2),
|
|
|
|
'upload': round(float(re_output[3]), 2)}
|