load the last good state from db if speedtest data is None (#2645)
* load the last good state from db if speedtest data is None * return if recorder is not available
This commit is contained in:
parent
ce3c89db6e
commit
3c51d2df0f
1 changed files with 15 additions and 2 deletions
|
@ -10,6 +10,7 @@ import sys
|
||||||
from subprocess import check_output, CalledProcessError
|
from subprocess import check_output, CalledProcessError
|
||||||
|
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
from homeassistant.components import recorder
|
||||||
from homeassistant.components.sensor import DOMAIN
|
from homeassistant.components.sensor import DOMAIN
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.event import track_time_change
|
from homeassistant.helpers.event import track_time_change
|
||||||
|
@ -85,8 +86,20 @@ class SpeedtestSensor(Entity):
|
||||||
"""Get the latest data and update the states."""
|
"""Get the latest data and update the states."""
|
||||||
data = self.speedtest_client.data
|
data = self.speedtest_client.data
|
||||||
if data is None:
|
if data is None:
|
||||||
return
|
entity_id = 'sensor.speedtest_' + self._name.lower()
|
||||||
|
states = recorder.get_model('States')
|
||||||
|
try:
|
||||||
|
last_state = recorder.execute(
|
||||||
|
recorder.query('States').filter(
|
||||||
|
(states.entity_id == entity_id) &
|
||||||
|
(states.last_changed == states.last_updated) &
|
||||||
|
(states.state != 'unknown')
|
||||||
|
).order_by(states.state_id.desc()).limit(1))
|
||||||
|
except TypeError:
|
||||||
|
return
|
||||||
|
if not last_state:
|
||||||
|
return
|
||||||
|
self._state = last_state[0].state
|
||||||
elif self.type == 'ping':
|
elif self.type == 'ping':
|
||||||
self._state = data['ping']
|
self._state = data['ping']
|
||||||
elif self.type == 'download':
|
elif self.type == 'download':
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue