parent
365f21b209
commit
50cd6c9a9c
2 changed files with 55 additions and 60 deletions
|
@ -1,5 +1,5 @@
|
|||
"""
|
||||
Support for retreiving status info from Google Wifi/OnHub routers.
|
||||
Support for retrieving status info from Google Wifi/OnHub routers.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.google_wifi/
|
||||
|
@ -12,28 +12,27 @@ import requests
|
|||
|
||||
import homeassistant.util.dt as dt
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.util import Throttle
|
||||
from homeassistant.const import (
|
||||
CONF_NAME, CONF_HOST, CONF_MONITORED_CONDITIONS,
|
||||
STATE_UNKNOWN)
|
||||
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=1)
|
||||
CONF_NAME, CONF_HOST, CONF_MONITORED_CONDITIONS, STATE_UNKNOWN)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ENDPOINT = '/api/v1/status'
|
||||
|
||||
ATTR_CURRENT_VERSION = 'current_version'
|
||||
ATTR_NEW_VERSION = 'new_version'
|
||||
ATTR_UPTIME = 'uptime'
|
||||
ATTR_LAST_RESTART = 'last_restart'
|
||||
ATTR_LOCAL_IP = 'local_ip'
|
||||
ATTR_NEW_VERSION = 'new_version'
|
||||
ATTR_STATUS = 'status'
|
||||
ATTR_UPTIME = 'uptime'
|
||||
|
||||
DEFAULT_NAME = 'google_wifi'
|
||||
DEFAULT_HOST = 'testwifi.here'
|
||||
DEFAULT_NAME = 'google_wifi'
|
||||
|
||||
ENDPOINT = '/api/v1/status'
|
||||
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=1)
|
||||
|
||||
MONITORED_CONDITIONS = {
|
||||
ATTR_CURRENT_VERSION: [
|
||||
|
@ -69,10 +68,10 @@ MONITORED_CONDITIONS = {
|
|||
}
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=MONITORED_CONDITIONS):
|
||||
vol.All(cv.ensure_list, [vol.In(MONITORED_CONDITIONS)]),
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
})
|
||||
|
||||
|
||||
|
@ -85,7 +84,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||
api = GoogleWifiAPI(host, conditions)
|
||||
dev = []
|
||||
for condition in conditions:
|
||||
dev.append(GoogleWifiSensor(hass, api, name, condition))
|
||||
dev.append(GoogleWifiSensor(api, name, condition))
|
||||
|
||||
add_devices(dev, True)
|
||||
|
||||
|
@ -93,9 +92,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||
class GoogleWifiSensor(Entity):
|
||||
"""Representation of a Google Wifi sensor."""
|
||||
|
||||
def __init__(self, hass, api, name, variable):
|
||||
"""Initialize a Pi-Hole sensor."""
|
||||
self._hass = hass
|
||||
def __init__(self, api, name, variable):
|
||||
"""Initialize a Google Wifi sensor."""
|
||||
self._api = api
|
||||
self._name = name
|
||||
self._state = STATE_UNKNOWN
|
||||
|
@ -108,7 +106,7 @@ class GoogleWifiSensor(Entity):
|
|||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return "{}_{}".format(self._name, self._var_name)
|
||||
return '{}_{}'.format(self._name, self._var_name)
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
|
@ -121,9 +119,9 @@ class GoogleWifiSensor(Entity):
|
|||
return self._var_units
|
||||
|
||||
@property
|
||||
def availiable(self):
|
||||
"""Return availiability of goole wifi api."""
|
||||
return self._api.availiable
|
||||
def available(self):
|
||||
"""Return availability of Google Wifi API."""
|
||||
return self._api.available
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
|
@ -133,7 +131,7 @@ class GoogleWifiSensor(Entity):
|
|||
def update(self):
|
||||
"""Get the latest data from the Google Wifi API."""
|
||||
self._api.update()
|
||||
if self.availiable:
|
||||
if self.available:
|
||||
self._state = self._api.data[self._var_name]
|
||||
else:
|
||||
self._state = STATE_UNKNOWN
|
||||
|
@ -157,7 +155,7 @@ class GoogleWifiAPI(object):
|
|||
ATTR_LOCAL_IP: STATE_UNKNOWN,
|
||||
ATTR_STATUS: STATE_UNKNOWN
|
||||
}
|
||||
self.availiable = True
|
||||
self.available = True
|
||||
self.update()
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
|
@ -165,14 +163,13 @@ class GoogleWifiAPI(object):
|
|||
"""Get the latest data from the router."""
|
||||
try:
|
||||
with requests.Session() as sess:
|
||||
response = sess.send(
|
||||
self._request, timeout=10)
|
||||
response = sess.send(self._request, timeout=10)
|
||||
self.raw_data = response.json()
|
||||
self.data_format()
|
||||
self.availiable = True
|
||||
except ValueError:
|
||||
_LOGGER.error('Unable to fetch data from Google Wifi')
|
||||
self.availiable = False
|
||||
self.available = True
|
||||
except (ValueError, requests.exceptions.ConnectionError):
|
||||
_LOGGER.warning("Unable to fetch data from Google Wifi")
|
||||
self.available = False
|
||||
self.raw_data = None
|
||||
|
||||
def data_format(self):
|
||||
|
@ -191,10 +188,10 @@ class GoogleWifiAPI(object):
|
|||
elif attr_key == ATTR_UPTIME:
|
||||
sensor_value /= 3600 * 24
|
||||
elif attr_key == ATTR_LAST_RESTART:
|
||||
last_restart = (dt.now() -
|
||||
timedelta(seconds=sensor_value))
|
||||
sensor_value = last_restart.strftime(('%Y-%m-%d '
|
||||
'%H:%M:%S'))
|
||||
last_restart = (
|
||||
dt.now() - timedelta(seconds=sensor_value))
|
||||
sensor_value = last_restart.strftime(
|
||||
'%Y-%m-%d %H:%M:%S')
|
||||
elif attr_key == ATTR_STATUS:
|
||||
if sensor_value:
|
||||
sensor_value = 'Online'
|
||||
|
@ -206,7 +203,7 @@ class GoogleWifiAPI(object):
|
|||
|
||||
self.data[attr_key] = sensor_value
|
||||
except KeyError:
|
||||
_LOGGER.error('Router does not support %s field. '
|
||||
'Please remove %s from monitored_conditions.',
|
||||
_LOGGER.error("Router does not support %s field. "
|
||||
"Please remove %s from monitored_conditions",
|
||||
sensor_key, attr_key)
|
||||
self.data[attr_key] = STATE_UNKNOWN
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
import unittest
|
||||
from unittest.mock import patch, Mock
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import requests_mock
|
||||
|
||||
from homeassistant import core as ha
|
||||
|
@ -32,7 +33,7 @@ MOCK_DATA_MISSING = ('{"software": {},'
|
|||
|
||||
|
||||
class TestGoogleWifiSetup(unittest.TestCase):
|
||||
"""Tests for setting up the Google Wifi switch platform."""
|
||||
"""Tests for setting up the Google Wifi sensor platform."""
|
||||
|
||||
def setUp(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
|
@ -45,9 +46,8 @@ class TestGoogleWifiSetup(unittest.TestCase):
|
|||
@requests_mock.Mocker()
|
||||
def test_setup_minimum(self, mock_req):
|
||||
"""Test setup with minimum configuration."""
|
||||
resource = '{}{}{}'.format('http://',
|
||||
google_wifi.DEFAULT_HOST,
|
||||
google_wifi.ENDPOINT)
|
||||
resource = '{}{}{}'.format(
|
||||
'http://', google_wifi.DEFAULT_HOST, google_wifi.ENDPOINT)
|
||||
mock_req.get(resource, status_code=200)
|
||||
self.assertTrue(setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
|
@ -60,9 +60,8 @@ class TestGoogleWifiSetup(unittest.TestCase):
|
|||
@requests_mock.Mocker()
|
||||
def test_setup_get(self, mock_req):
|
||||
"""Test setup with full configuration."""
|
||||
resource = '{}{}{}'.format('http://',
|
||||
'localhost',
|
||||
google_wifi.ENDPOINT)
|
||||
resource = '{}{}{}'.format(
|
||||
'http://', 'localhost', google_wifi.ENDPOINT)
|
||||
mock_req.get(resource, status_code=200)
|
||||
self.assertTrue(setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
|
@ -95,34 +94,33 @@ class TestGoogleWifiSensor(unittest.TestCase):
|
|||
|
||||
def setup_api(self, data, mock_req):
|
||||
"""Setup API with fake data."""
|
||||
resource = '{}{}{}'.format('http://',
|
||||
'localhost',
|
||||
google_wifi.ENDPOINT)
|
||||
resource = '{}{}{}'.format(
|
||||
'http://', 'localhost', google_wifi.ENDPOINT)
|
||||
now = datetime(1970, month=1, day=1)
|
||||
with patch('homeassistant.util.dt.now', return_value=now):
|
||||
mock_req.get(resource, text=data, status_code=200)
|
||||
conditions = google_wifi.MONITORED_CONDITIONS.keys()
|
||||
self.api = google_wifi.GoogleWifiAPI("localhost",
|
||||
conditions)
|
||||
self.api = google_wifi.GoogleWifiAPI("localhost", conditions)
|
||||
self.name = NAME
|
||||
self.sensor_dict = dict()
|
||||
for condition, cond_list in google_wifi.MONITORED_CONDITIONS.items():
|
||||
sensor = google_wifi.GoogleWifiSensor(self.hass, self.api,
|
||||
self.name, condition)
|
||||
sensor = google_wifi.GoogleWifiSensor(
|
||||
self.api, self.name, condition)
|
||||
name = '{}_{}'.format(self.name, condition)
|
||||
units = cond_list[1]
|
||||
icon = cond_list[2]
|
||||
self.sensor_dict[condition] = {'sensor': sensor,
|
||||
self.sensor_dict[condition] = {
|
||||
'sensor': sensor,
|
||||
'name': name,
|
||||
'units': units,
|
||||
'icon': icon}
|
||||
'icon': icon
|
||||
}
|
||||
|
||||
def fake_delay(self, ha_delay):
|
||||
"""Fake delay to prevent update throttle."""
|
||||
hass_now = dt_util.utcnow()
|
||||
shifted_time = hass_now + timedelta(seconds=ha_delay)
|
||||
self.hass.bus.fire(ha.EVENT_TIME_CHANGED,
|
||||
{ha.ATTR_NOW: shifted_time})
|
||||
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: shifted_time})
|
||||
|
||||
def test_name(self):
|
||||
"""Test the name."""
|
||||
|
@ -135,8 +133,8 @@ class TestGoogleWifiSensor(unittest.TestCase):
|
|||
"""Test the unit of measurement."""
|
||||
for name in self.sensor_dict:
|
||||
sensor = self.sensor_dict[name]['sensor']
|
||||
self.assertEqual(self.sensor_dict[name]['units'],
|
||||
sensor.unit_of_measurement)
|
||||
self.assertEqual(
|
||||
self.sensor_dict[name]['units'], sensor.unit_of_measurement)
|
||||
|
||||
def test_icon(self):
|
||||
"""Test the icon."""
|
||||
|
@ -208,8 +206,8 @@ class TestGoogleWifiSensor(unittest.TestCase):
|
|||
sensor.update()
|
||||
self.assertEqual(STATE_UNKNOWN, sensor.state)
|
||||
|
||||
def test_update_when_unavailiable(self):
|
||||
"""Test state updates when Google Wifi unavailiable."""
|
||||
def test_update_when_unavailable(self):
|
||||
"""Test state updates when Google Wifi unavailable."""
|
||||
self.api.update = Mock('google_wifi.GoogleWifiAPI.update',
|
||||
side_effect=self.update_side_effect())
|
||||
for name in self.sensor_dict:
|
||||
|
@ -220,4 +218,4 @@ class TestGoogleWifiSensor(unittest.TestCase):
|
|||
def update_side_effect(self):
|
||||
"""Mock representation of update function."""
|
||||
self.api.data = None
|
||||
self.api.availiable = False
|
||||
self.api.available = False
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue