2017-03-06 11:15:08 +00:00
|
|
|
"""The tests for the Ring sensor platform."""
|
2020-01-10 20:35:31 +00:00
|
|
|
from asyncio import run_coroutine_threadsafe
|
2017-03-06 11:15:08 +00:00
|
|
|
import unittest
|
2020-01-10 20:35:31 +00:00
|
|
|
from unittest.mock import patch
|
2019-12-09 10:58:40 +00:00
|
|
|
|
2017-03-31 15:53:56 +00:00
|
|
|
import requests_mock
|
2017-03-06 11:15:08 +00:00
|
|
|
|
2017-03-31 15:53:56 +00:00
|
|
|
from homeassistant.components import ring as base_ring
|
2019-12-09 10:58:40 +00:00
|
|
|
import homeassistant.components.ring.sensor as ring
|
2019-07-31 18:08:40 +00:00
|
|
|
from homeassistant.helpers.icon import icon_for_battery_level
|
2019-12-09 10:58:40 +00:00
|
|
|
|
2020-01-12 00:04:39 +00:00
|
|
|
from tests.common import get_test_home_assistant, load_fixture, mock_storage
|
2019-12-09 10:58:40 +00:00
|
|
|
from tests.components.ring.test_init import ATTRIBUTION, VALID_CONFIG
|
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)
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Initialize values for this testcase class."""
|
|
|
|
self.hass = get_test_home_assistant()
|
2017-03-31 15:53:56 +00:00
|
|
|
self.config = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"username": "foo",
|
|
|
|
"password": "bar",
|
|
|
|
"monitored_conditions": [
|
|
|
|
"battery",
|
|
|
|
"last_activity",
|
|
|
|
"last_ding",
|
|
|
|
"last_motion",
|
|
|
|
"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-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."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock.post(
|
|
|
|
"https://oauth.ring.com/oauth/token", text=load_fixture("ring_oauth.json")
|
|
|
|
)
|
|
|
|
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"),
|
|
|
|
)
|
|
|
|
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"),
|
|
|
|
)
|
2020-01-10 20:35:31 +00:00
|
|
|
|
|
|
|
with mock_storage(), patch("homeassistant.components.ring.PLATFORMS", []):
|
|
|
|
run_coroutine_threadsafe(
|
|
|
|
base_ring.async_setup(self.hass, VALID_CONFIG), self.hass.loop
|
|
|
|
).result()
|
|
|
|
run_coroutine_threadsafe(
|
|
|
|
self.hass.async_block_till_done(), self.hass.loop
|
|
|
|
).result()
|
|
|
|
run_coroutine_threadsafe(
|
|
|
|
ring.async_setup_entry(self.hass, None, self.add_entities),
|
|
|
|
self.hass.loop,
|
|
|
|
).result()
|
2017-03-06 11:15:08 +00:00
|
|
|
|
|
|
|
for device in self.DEVICES:
|
2020-01-12 00:04:39 +00:00
|
|
|
# Mimick add to hass
|
|
|
|
device.hass = self.hass
|
|
|
|
run_coroutine_threadsafe(
|
|
|
|
device.async_added_to_hass(), self.hass.loop,
|
|
|
|
).result()
|
|
|
|
|
|
|
|
# Entity update data from ring data
|
2017-03-06 11:15:08 +00:00
|
|
|
device.update()
|
2019-07-31 19:25:30 +00:00
|
|
|
if device.name == "Front Battery":
|
2019-07-31 18:08:40 +00:00
|
|
|
expected_icon = icon_for_battery_level(
|
2019-07-31 19:25:30 +00:00
|
|
|
battery_level=int(device.state), charging=False
|
|
|
|
)
|
2019-07-31 18:08:40 +00:00
|
|
|
assert device.icon == expected_icon
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 80 == device.state
|
2019-07-31 19:25:30 +00:00
|
|
|
if device.name == "Front Door Battery":
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 100 == device.state
|
2019-07-31 19:25:30 +00:00
|
|
|
if device.name == "Downstairs Volume":
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 2 == device.state
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "ring_mock_wifi" == device.device_state_attributes["wifi_name"]
|
|
|
|
assert "mdi:bell-ring" == device.icon
|
|
|
|
if device.name == "Front Door Last Activity":
|
|
|
|
assert not device.device_state_attributes["answered"]
|
|
|
|
assert "America/New_York" == device.device_state_attributes["timezone"]
|
2017-03-06 11:15:08 +00:00
|
|
|
|
2019-07-31 19:25:30 +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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if device.name == "Front Door WiFi Signal Category":
|
|
|
|
assert "good" == device.state
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2019-07-31 19:25:30 +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
|
2019-07-31 19:25:30 +00:00
|
|
|
assert ATTRIBUTION == device.device_state_attributes["attribution"]
|
2019-07-31 18:08:40 +00:00
|
|
|
assert not device.should_poll
|