Update Ring to 0.6.0 (#30748)

* Update Ring to 0.6.0

* Update sensor tests

* update -> async_update

* Delete temp files

* Address comments

* Final tweaks

* Remove stale print
This commit is contained in:
Paulus Schoutsen committed 2020-01-14 12:57:32 -08:00
1 parent 8e5f46d5b5
commit 4bc520c724
19 files changed
+417 -418

No files matched your search

+7 -3
View File
@@ -1,4 +1,6 @@
"""Configuration for Ring tests."""
import re
import pytest
import requests_mock
@@ -33,17 +35,19 @@ def requests_mock_fixture():
)
# Mocks the response for getting the history of a device
mock.get(
"https://api.ring.com/clients_api/doorbots/987652/history",
re.compile(
r"https:\/\/api\.ring\.com\/clients_api\/doorbots\/\d+\/history"
),
text=load_fixture("ring_doorbots.json"),
)
# Mocks the response for getting the health of a device
mock.get(
"https://api.ring.com/clients_api/doorbots/987652/health",
re.compile(r"https:\/\/api\.ring\.com\/clients_api\/doorbots\/\d+\/health"),
text=load_fixture("ring_doorboot_health_attrs.json"),
)
# Mocks the response for getting a chimes health
mock.get(
"https://api.ring.com/clients_api/chimes/999999/health",
re.compile(r"https:\/\/api\.ring\.com\/clients_api\/chimes\/\d+\/health"),
text=load_fixture("ring_chime_health_attrs.json"),
)
+15 -80
View File
@@ -1,87 +1,22 @@
"""The tests for the Ring binary sensor platform."""
from asyncio import run_coroutine_threadsafe
import unittest
from unittest.mock import patch
import requests_mock
from homeassistant.components import ring as base_ring
from homeassistant.components.ring import binary_sensor as ring
from tests.common import get_test_home_assistant, load_fixture, mock_storage
from tests.components.ring.test_init import ATTRIBUTION, VALID_CONFIG
from .common import setup_platform
class TestRingBinarySensorSetup(unittest.TestCase):
"""Test the Ring Binary Sensor platform."""
async def test_binary_sensor(hass, requests_mock):
"""Test the Ring binary sensors."""
with patch(
"ring_doorbell.Ring.active_alerts",
return_value=[{"kind": "motion", "doorbot_id": 987654}],
):
await setup_platform(hass, "binary_sensor")
DEVICES = []
motion_state = hass.states.get("binary_sensor.front_door_motion")
assert motion_state is not None
assert motion_state.state == "on"
assert motion_state.attributes["device_class"] == "motion"
def add_entities(self, devices, action):
"""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()
self.config = {
"username": "foo",
"password": "bar",
"monitored_conditions": ["ding", "motion"],
}
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
@requests_mock.Mocker()
def test_binary_sensor(self, mock):
"""Test the Ring sensor class and methods."""
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/dings/active",
text=load_fixture("ring_ding_active.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"),
)
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()
for device in self.DEVICES:
device.update()
if device.name == "Front Door Ding":
assert "on" == device.state
assert "America/New_York" == device.device_state_attributes["timezone"]
elif device.name == "Front Door Motion":
assert "off" == device.state
assert "motion" == device.device_class
assert device.entity_picture is None
assert ATTRIBUTION == device.device_state_attributes["attribution"]
ding_state = hass.states.get("binary_sensor.front_door_ding")
assert ding_state is not None
assert ding_state.state == "off"
+3 -3
View File
@@ -12,10 +12,10 @@ async def test_entity_registry(hass, requests_mock):
entity_registry = await hass.helpers.entity_registry.async_get_registry()
entry = entity_registry.async_get("light.front_light")
assert entry.unique_id == "aacdef123"
assert entry.unique_id == 765432
entry = entity_registry.async_get("light.internal_light")
assert entry.unique_id == "aacdef124"
assert entry.unique_id == 345678
async def test_light_off_reports_correctly(hass, requests_mock):
@@ -42,7 +42,7 @@ async def test_light_can_be_turned_on(hass, requests_mock):
# Mocks the response for turning a light on
requests_mock.put(
"https://api.ring.com/clients_api/doorbots/987652/floodlight_light_on",
"https://api.ring.com/clients_api/doorbots/765432/floodlight_light_on",
text=load_fixture("ring_doorbot_siren_on_response.json"),
)
+33 -109
View File
@@ -1,122 +1,46 @@
"""The tests for the Ring sensor platform."""
from asyncio import run_coroutine_threadsafe
import unittest
from unittest.mock import patch
from .common import setup_platform
import requests_mock
from homeassistant.components import ring as base_ring
import homeassistant.components.ring.sensor as ring
from homeassistant.helpers.icon import icon_for_battery_level
from tests.common import get_test_home_assistant, load_fixture, mock_storage
from tests.components.ring.test_init import ATTRIBUTION, VALID_CONFIG
WIFI_ENABLED = False
class TestRingSensorSetup(unittest.TestCase):
"""Test the Ring platform."""
async def test_sensor(hass, requests_mock):
"""Test the Ring sensors."""
await setup_platform(hass, "sensor")
DEVICES = []
front_battery_state = hass.states.get("sensor.front_battery")
assert front_battery_state is not None
assert front_battery_state.state == "80"
def add_entities(self, devices, action):
"""Mock add devices."""
for device in devices:
self.DEVICES.append(device)
front_door_battery_state = hass.states.get("sensor.front_door_battery")
assert front_door_battery_state is not None
assert front_door_battery_state.state == "100"
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
self.config = {
"username": "foo",
"password": "bar",
"monitored_conditions": [
"battery",
"last_activity",
"last_ding",
"last_motion",
"volume",
"wifi_signal_category",
"wifi_signal_strength",
],
}
downstairs_volume_state = hass.states.get("sensor.downstairs_volume")
assert downstairs_volume_state is not None
assert downstairs_volume_state.state == "2"
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
front_door_last_activity_state = hass.states.get("sensor.front_door_last_activity")
assert front_door_last_activity_state is not None
@requests_mock.Mocker()
def test_sensor(self, mock):
"""Test the Ring sensor class and methods."""
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"),
)
downstairs_wifi_signal_strength_state = hass.states.get(
"sensor.downstairs_wifi_signal_strength"
)
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()
if not WIFI_ENABLED:
return
for device in self.DEVICES:
# Mimick add to hass
device.hass = self.hass
run_coroutine_threadsafe(
device.async_added_to_hass(), self.hass.loop,
).result()
assert downstairs_wifi_signal_strength_state is not None
assert downstairs_wifi_signal_strength_state.state == "-39"
# Entity update data from ring data
device.update()
if device.name == "Front Battery":
expected_icon = icon_for_battery_level(
battery_level=int(device.state), charging=False
)
assert device.icon == expected_icon
assert 80 == device.state
if device.name == "Front Door Battery":
assert 100 == device.state
if device.name == "Downstairs Volume":
assert 2 == device.state
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"]
front_door_wifi_signal_category_state = hass.states.get(
"sensor.front_door_wifi_signal_category"
)
assert front_door_wifi_signal_category_state is not None
assert front_door_wifi_signal_category_state.state == "good"
if device.name == "Downstairs WiFi Signal Strength":
assert -39 == device.state
if device.name == "Front Door WiFi Signal Category":
assert "good" == device.state
if device.name == "Front Door WiFi Signal Strength":
assert -58 == device.state
assert device.entity_picture is None
assert ATTRIBUTION == device.device_state_attributes["attribution"]
assert not device.should_poll
front_door_wifi_signal_strength_state = hass.states.get(
"sensor.front_door_wifi_signal_strength"
)
assert front_door_wifi_signal_strength_state is not None
assert front_door_wifi_signal_strength_state.state == "-58"
+3 -3
View File
@@ -12,10 +12,10 @@ async def test_entity_registry(hass, requests_mock):
entity_registry = await hass.helpers.entity_registry.async_get_registry()
entry = entity_registry.async_get("switch.front_siren")
assert entry.unique_id == "aacdef123-siren"
assert entry.unique_id == "765432-siren"
entry = entity_registry.async_get("switch.internal_siren")
assert entry.unique_id == "aacdef124-siren"
assert entry.unique_id == "345678-siren"
async def test_siren_off_reports_correctly(hass, requests_mock):
@@ -43,7 +43,7 @@ async def test_siren_can_be_turned_on(hass, requests_mock):
# Mocks the response for turning a siren on
requests_mock.put(
"https://api.ring.com/clients_api/doorbots/987652/siren_on",
"https://api.ring.com/clients_api/doorbots/765432/siren_on",
text=load_fixture("ring_doorbot_siren_on_response.json"),
)