2017-03-06 11:15:08 +00:00
|
|
|
"""The tests for the Ring sensor platform."""
|
2017-04-05 09:26:56 +00:00
|
|
|
import os
|
2017-03-06 11:15:08 +00:00
|
|
|
import unittest
|
2017-03-31 15:53:56 +00:00
|
|
|
import requests_mock
|
2017-03-06 11:15:08 +00:00
|
|
|
|
|
|
|
from homeassistant.components.sensor import ring
|
2017-03-31 15:53:56 +00:00
|
|
|
from homeassistant.components import ring as base_ring
|
2017-03-06 11:15:08 +00:00
|
|
|
|
2017-03-31 15:53:56 +00:00
|
|
|
from tests.components.test_ring import ATTRIBUTION, VALID_CONFIG
|
2017-04-05 09:26:56 +00:00
|
|
|
from tests.common import (
|
|
|
|
get_test_config_dir, get_test_home_assistant, load_fixture)
|
2017-03-06 11:15:08 +00:00
|
|
|
|
|
|
|
|
2017-03-31 15:53:56 +00:00
|
|
|
class TestRingSensorSetup(unittest.TestCase):
|
2017-03-06 11:15:08 +00:00
|
|
|
"""Test the Ring platform."""
|
|
|
|
|
|
|
|
DEVICES = []
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def add_entities(self, devices, action):
|
2017-03-06 11:15:08 +00:00
|
|
|
"""Mock add devices."""
|
|
|
|
for device in devices:
|
|
|
|
self.DEVICES.append(device)
|
|
|
|
|
2017-04-05 09:26:56 +00:00
|
|
|
def cleanup(self):
|
|
|
|
"""Cleanup any data created from the tests."""
|
|
|
|
if os.path.isfile(self.cache):
|
|
|
|
os.remove(self.cache)
|
|
|
|
|
2017-03-06 11:15:08 +00:00
|
|
|
def setUp(self):
|
|
|
|
"""Initialize values for this testcase class."""
|
|
|
|
self.hass = get_test_home_assistant()
|
2017-04-05 09:26:56 +00:00
|
|
|
self.cache = get_test_config_dir(base_ring.DEFAULT_CACHEDB)
|
2017-03-31 15:53:56 +00:00
|
|
|
self.config = {
|
|
|
|
'username': 'foo',
|
|
|
|
'password': 'bar',
|
|
|
|
'monitored_conditions': [
|
|
|
|
'battery',
|
|
|
|
'last_activity',
|
|
|
|
'last_ding',
|
|
|
|
'last_motion',
|
2017-10-21 14:08:40 +00:00
|
|
|
'volume',
|
|
|
|
'wifi_signal_category',
|
|
|
|
'wifi_signal_strength']
|
2017-03-31 15:53:56 +00:00
|
|
|
}
|
2017-03-06 11:15:08 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
2017-04-05 09:26:56 +00:00
|
|
|
self.cleanup()
|
2017-03-06 11:15:08 +00:00
|
|
|
|
2017-03-31 15:53:56 +00:00
|
|
|
@requests_mock.Mocker()
|
|
|
|
def test_sensor(self, mock):
|
2018-01-29 22:37:19 +00:00
|
|
|
"""Test the Ring sensor class and methods."""
|
2018-06-16 06:58:39 +00:00
|
|
|
mock.post('https://oauth.ring.com/oauth/token',
|
|
|
|
text=load_fixture('ring_oauth.json'))
|
2017-03-31 15:53:56 +00:00
|
|
|
mock.post('https://api.ring.com/clients_api/session',
|
|
|
|
text=load_fixture('ring_session.json'))
|
|
|
|
mock.get('https://api.ring.com/clients_api/ring_devices',
|
|
|
|
text=load_fixture('ring_devices.json'))
|
|
|
|
mock.get('https://api.ring.com/clients_api/doorbots/987652/history',
|
|
|
|
text=load_fixture('ring_doorbots.json'))
|
2017-10-21 14:08:40 +00:00
|
|
|
mock.get('https://api.ring.com/clients_api/doorbots/987652/health',
|
|
|
|
text=load_fixture('ring_doorboot_health_attrs.json'))
|
|
|
|
mock.get('https://api.ring.com/clients_api/chimes/999999/health',
|
|
|
|
text=load_fixture('ring_chime_health_attrs.json'))
|
2017-03-31 15:53:56 +00:00
|
|
|
base_ring.setup(self.hass, VALID_CONFIG)
|
|
|
|
ring.setup_platform(self.hass,
|
|
|
|
self.config,
|
2018-08-24 14:37:30 +00:00
|
|
|
self.add_entities,
|
2017-03-31 15:53:56 +00:00
|
|
|
None)
|
2017-03-06 11:15:08 +00:00
|
|
|
|
|
|
|
for device in self.DEVICES:
|
|
|
|
device.update()
|
2017-10-21 14:08:40 +00:00
|
|
|
if device.name == 'Front Battery':
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 80 == device.state
|
|
|
|
assert 'hp_cam_v1' == \
|
|
|
|
device.device_state_attributes['kind']
|
|
|
|
assert 'stickup_cams' == \
|
|
|
|
device.device_state_attributes['type']
|
2017-03-06 11:15:08 +00:00
|
|
|
if device.name == 'Front Door Battery':
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 100 == device.state
|
|
|
|
assert 'lpd_v1' == \
|
|
|
|
device.device_state_attributes['kind']
|
|
|
|
assert 'chimes' != \
|
|
|
|
device.device_state_attributes['type']
|
2017-03-06 11:15:08 +00:00
|
|
|
if device.name == 'Downstairs Volume':
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 2 == device.state
|
|
|
|
assert '1.2.3' == \
|
|
|
|
device.device_state_attributes['firmware']
|
|
|
|
assert 'ring_mock_wifi' == \
|
|
|
|
device.device_state_attributes['wifi_name']
|
|
|
|
assert 'mdi:bell-ring' == device.icon
|
|
|
|
assert 'chimes' == \
|
|
|
|
device.device_state_attributes['type']
|
2017-03-06 11:15:08 +00:00
|
|
|
if device.name == 'Front Door Last Activity':
|
2018-10-24 10:10:05 +00:00
|
|
|
assert not device.device_state_attributes['answered']
|
|
|
|
assert 'America/New_York' == \
|
|
|
|
device.device_state_attributes['timezone']
|
2017-03-06 11:15:08 +00:00
|
|
|
|
2017-10-21 14:08:40 +00:00
|
|
|
if device.name == 'Downstairs WiFi Signal Strength':
|
2018-10-24 10:10:05 +00:00
|
|
|
assert -39 == device.state
|
2017-10-21 14:08:40 +00:00
|
|
|
|
|
|
|
if device.name == 'Front Door WiFi Signal Category':
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'good' == device.state
|
2017-10-21 14:08:40 +00:00
|
|
|
|
|
|
|
if device.name == 'Front Door WiFi Signal Strength':
|
2018-10-24 10:10:05 +00:00
|
|
|
assert -58 == device.state
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert device.entity_picture is None
|
|
|
|
assert ATTRIBUTION == \
|
|
|
|
device.device_state_attributes['attribution']
|