core/tests/components/ring/test_sensor.py

116 lines
4.4 KiB
Python
Raw Normal View History

"""The tests for the Ring sensor platform."""
import os
import unittest
import requests_mock
Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
import homeassistant.components.ring.sensor as ring
from homeassistant.components import ring as base_ring
from homeassistant.helpers.icon import icon_for_battery_level
from tests.components.ring.test_init import ATTRIBUTION, VALID_CONFIG
2019-07-31 19:25:30 +00:00
from tests.common import get_test_config_dir, get_test_home_assistant, load_fixture
class TestRingSensorSetup(unittest.TestCase):
"""Test the Ring platform."""
DEVICES = []
def add_entities(self, devices, action):
"""Mock add devices."""
for device in devices:
self.DEVICES.append(device)
def cleanup(self):
"""Cleanup any data created from the tests."""
if os.path.isfile(self.cache):
os.remove(self.cache)
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
self.cache = get_test_config_dir(base_ring.DEFAULT_CACHEDB)
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",
],
}
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
self.cleanup()
@requests_mock.Mocker()
def test_sensor(self, mock):
"""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"),
)
base_ring.setup(self.hass, VALID_CONFIG)
2019-07-31 19:25:30 +00:00
ring.setup_platform(self.hass, self.config, self.add_entities, None)
for device in self.DEVICES:
device.update()
2019-07-31 19:25:30 +00:00
if device.name == "Front Battery":
expected_icon = icon_for_battery_level(
2019-07-31 19:25:30 +00:00
battery_level=int(device.state), charging=False
)
assert device.icon == expected_icon
assert 80 == device.state
2019-07-31 19:25:30 +00:00
assert "hp_cam_v1" == device.device_state_attributes["kind"]
assert "stickup_cams" == device.device_state_attributes["type"]
if device.name == "Front Door Battery":
assert 100 == device.state
2019-07-31 19:25:30 +00:00
assert "lpd_v1" == device.device_state_attributes["kind"]
assert "chimes" != device.device_state_attributes["type"]
if device.name == "Downstairs Volume":
assert 2 == device.state
2019-07-31 19:25:30 +00:00
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"]
if device.name == "Front Door Last Activity":
assert not device.device_state_attributes["answered"]
assert "America/New_York" == device.device_state_attributes["timezone"]
2019-07-31 19:25:30 +00:00
if device.name == "Downstairs WiFi Signal Strength":
assert -39 == device.state
2019-07-31 19:25:30 +00:00
if device.name == "Front Door WiFi Signal Category":
assert "good" == device.state
2019-07-31 19:25:30 +00:00
if device.name == "Front Door WiFi Signal Strength":
assert -58 == device.state
assert device.entity_picture is None
2019-07-31 19:25:30 +00:00
assert ATTRIBUTION == device.device_state_attributes["attribution"]
assert not device.should_poll