use track_time_change
parent
78e758925b
commit
e837e97c9d
|
@ -13,6 +13,7 @@ from datetime import timedelta
|
||||||
from subprocess import check_output
|
from subprocess import check_output
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
from homeassistant.helpers import event
|
||||||
|
|
||||||
REQUIREMENTS = ['speedtest-cli==0.3.4']
|
REQUIREMENTS = ['speedtest-cli==0.3.4']
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -20,6 +21,10 @@ _LOGGER = logging.getLogger(__name__)
|
||||||
_SPEEDTEST_REGEX = re.compile(r'Ping:\s(\d+\.\d+)\sms\nDownload:\s(\d+\.\d+)'
|
_SPEEDTEST_REGEX = re.compile(r'Ping:\s(\d+\.\d+)\sms\nDownload:\s(\d+\.\d+)'
|
||||||
r'\sMbit/s\nUpload:\s(\d+\.\d+)\sMbit/s\n')
|
r'\sMbit/s\nUpload:\s(\d+\.\d+)\sMbit/s\n')
|
||||||
|
|
||||||
|
CONF_MONITORED_CONDITIONS = 'monitored_conditions'
|
||||||
|
CONF_MINUTE = 'minute'
|
||||||
|
CONF_HOUR = 'hour'
|
||||||
|
CONF_DAY = 'day'
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES = {
|
||||||
'ping': ['Ping', 'ms'],
|
'ping': ['Ping', 'ms'],
|
||||||
'download': ['Download', 'Mbit/s'],
|
'download': ['Download', 'Mbit/s'],
|
||||||
|
@ -27,20 +32,20 @@ SENSOR_TYPES = {
|
||||||
}
|
}
|
||||||
|
|
||||||
# Return cached results if last scan was less then this time ago
|
# Return cached results if last scan was less then this time ago
|
||||||
MIN_TIME_BETWEEN_UPDATES = timedelta(hours=1)
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
""" Setup the Speedtest sensor. """
|
""" Setup the Speedtest sensor. """
|
||||||
|
|
||||||
data = SpeedtestData(hass.config.path)
|
data = SpeedtestData(hass, event, config)
|
||||||
|
|
||||||
dev = []
|
dev = []
|
||||||
for variable in config['monitored_conditions']:
|
for sensor in config[CONF_MONITORED_CONDITIONS]:
|
||||||
if variable not in SENSOR_TYPES:
|
if sensor not in SENSOR_TYPES:
|
||||||
_LOGGER.error('Sensor type: "%s" does not exist', variable)
|
_LOGGER.error('Sensor type: "%s" does not exist', sensor)
|
||||||
else:
|
else:
|
||||||
dev.append(SpeedtestSensor(data, variable))
|
dev.append(SpeedtestSensor(data, sensor))
|
||||||
|
|
||||||
add_devices(dev)
|
add_devices(dev)
|
||||||
|
|
||||||
|
@ -50,17 +55,15 @@ class SpeedtestSensor(Entity):
|
||||||
""" Implements a speedtest.net sensor. """
|
""" Implements a speedtest.net sensor. """
|
||||||
|
|
||||||
def __init__(self, speedtest_data, sensor_type):
|
def __init__(self, speedtest_data, sensor_type):
|
||||||
self.client_name = 'Speedtest'
|
|
||||||
self._name = SENSOR_TYPES[sensor_type][0]
|
self._name = SENSOR_TYPES[sensor_type][0]
|
||||||
self.speedtest_client = speedtest_data
|
self.speedtest_client = speedtest_data
|
||||||
self.type = sensor_type
|
self.type = sensor_type
|
||||||
self._state = None
|
self._state = None
|
||||||
self._unit_of_measurement = SENSOR_TYPES[self.type][1]
|
self._unit_of_measurement = SENSOR_TYPES[self.type][1]
|
||||||
self.update()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
return '{} {}'.format(self.client_name, self._name)
|
return '{} {}'.format('Speedtest', self._name)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
|
@ -74,27 +77,31 @@ class SpeedtestSensor(Entity):
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
""" Gets the latest data from Forecast.io and updates the states. """
|
""" Gets the latest data from Forecast.io and updates the states. """
|
||||||
self.speedtest_client.update()
|
|
||||||
data = self.speedtest_client.data
|
data = self.speedtest_client.data
|
||||||
|
if data is not None:
|
||||||
if self.type == 'ping':
|
if self.type == 'ping':
|
||||||
self._state = data['ping']
|
self._state = data['ping']
|
||||||
elif self.type == 'download':
|
elif self.type == 'download':
|
||||||
self._state = data['download']
|
self._state = data['download']
|
||||||
elif self.type == 'upload':
|
elif self.type == 'upload':
|
||||||
self._state = data['upload']
|
self._state = data['upload']
|
||||||
|
|
||||||
|
|
||||||
class SpeedtestData(object):
|
class SpeedtestData(object):
|
||||||
""" Gets the latest data from speedtest.net. """
|
""" Gets the latest data from speedtest.net. """
|
||||||
|
|
||||||
def __init__(self, path):
|
def __init__(self, hass, event, config):
|
||||||
self.data = None
|
self.data = None
|
||||||
self.path = path
|
self.hass = hass
|
||||||
self.update()
|
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))
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||||
def update(self):
|
def update(self, event=None):
|
||||||
""" Gets the latest data from speedtest.net. """
|
""" Gets the latest data from speedtest.net. """
|
||||||
_LOGGER.info('Executing speedtest')
|
_LOGGER.info('Executing speedtest')
|
||||||
re_output = _SPEEDTEST_REGEX.split(
|
re_output = _SPEEDTEST_REGEX.split(
|
||||||
|
|
Loading…
Reference in New Issue