2017-03-06 11:15:08 +00:00
|
|
|
"""The tests for the Ring sensor platform."""
|
2024-03-08 18:16:21 +00:00
|
|
|
|
2024-01-31 18:39:18 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
2024-05-29 12:10:00 +00:00
|
|
|
import pytest
|
2023-02-17 17:45:48 +00:00
|
|
|
|
2024-01-31 18:39:18 +00:00
|
|
|
from homeassistant.components.ring.const import SCAN_INTERVAL
|
2024-02-15 15:52:11 +00:00
|
|
|
from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorStateClass
|
2024-01-31 18:39:18 +00:00
|
|
|
from homeassistant.const import Platform
|
2023-02-17 17:45:48 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-06-06 18:13:19 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2023-02-17 17:45:48 +00:00
|
|
|
|
2020-01-14 20:54:45 +00:00
|
|
|
from .common import setup_platform
|
2019-12-09 10:58:40 +00:00
|
|
|
|
2024-06-06 18:13:19 +00:00
|
|
|
from tests.common import async_fire_time_changed
|
2024-01-31 18:39:18 +00:00
|
|
|
|
2017-03-06 11:15:08 +00:00
|
|
|
|
2024-06-06 18:13:19 +00:00
|
|
|
async def test_sensor(hass: HomeAssistant, mock_ring_client) -> None:
|
2020-01-14 20:54:45 +00:00
|
|
|
"""Test the Ring sensors."""
|
|
|
|
await setup_platform(hass, "sensor")
|
2017-03-06 11:15:08 +00:00
|
|
|
|
2020-01-14 20:54:45 +00:00
|
|
|
front_battery_state = hass.states.get("sensor.front_battery")
|
|
|
|
assert front_battery_state is not None
|
|
|
|
assert front_battery_state.state == "80"
|
2024-02-15 15:52:11 +00:00
|
|
|
assert (
|
|
|
|
front_battery_state.attributes[ATTR_STATE_CLASS] == SensorStateClass.MEASUREMENT
|
|
|
|
)
|
2017-03-06 11:15:08 +00:00
|
|
|
|
2020-01-14 20:54:45 +00:00
|
|
|
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"
|
2024-02-15 15:52:11 +00:00
|
|
|
assert (
|
|
|
|
front_door_battery_state.attributes[ATTR_STATE_CLASS]
|
|
|
|
== SensorStateClass.MEASUREMENT
|
|
|
|
)
|
2017-03-06 11:15:08 +00:00
|
|
|
|
2020-01-14 20:54:45 +00:00
|
|
|
downstairs_volume_state = hass.states.get("sensor.downstairs_volume")
|
|
|
|
assert downstairs_volume_state is not None
|
|
|
|
assert downstairs_volume_state.state == "2"
|
2017-03-06 11:15:08 +00:00
|
|
|
|
2024-03-15 11:59:36 +00:00
|
|
|
ingress_mic_volume_state = hass.states.get("sensor.ingress_mic_volume")
|
|
|
|
assert ingress_mic_volume_state.state == "11"
|
|
|
|
|
|
|
|
ingress_doorbell_volume_state = hass.states.get("sensor.ingress_doorbell_volume")
|
|
|
|
assert ingress_doorbell_volume_state.state == "8"
|
|
|
|
|
|
|
|
ingress_voice_volume_state = hass.states.get("sensor.ingress_voice_volume")
|
|
|
|
assert ingress_voice_volume_state.state == "11"
|
|
|
|
|
2017-03-06 11:15:08 +00:00
|
|
|
|
2024-06-06 18:13:19 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("device_id", "device_name", "sensor_name", "expected_value"),
|
|
|
|
[
|
|
|
|
(987654, "front_door", "wifi_signal_category", "good"),
|
|
|
|
(987654, "front_door", "wifi_signal_strength", "-58"),
|
|
|
|
(123456, "downstairs", "wifi_signal_category", "good"),
|
|
|
|
(123456, "downstairs", "wifi_signal_strength", "-39"),
|
|
|
|
(765432, "front", "wifi_signal_category", "good"),
|
|
|
|
(765432, "front", "wifi_signal_strength", "-58"),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
"doorbell-category",
|
|
|
|
"doorbell-strength",
|
|
|
|
"chime-category",
|
|
|
|
"chime-strength",
|
|
|
|
"stickup_cam-category",
|
|
|
|
"stickup_cam-strength",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_health_sensor(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_ring_client,
|
|
|
|
freezer: FrozenDateTimeFactory,
|
|
|
|
entity_registry: er.EntityRegistry,
|
|
|
|
device_id,
|
|
|
|
device_name,
|
|
|
|
sensor_name,
|
|
|
|
expected_value,
|
|
|
|
) -> None:
|
|
|
|
"""Test the Ring health sensors."""
|
|
|
|
entity_id = f"sensor.{device_name}_{sensor_name}"
|
|
|
|
# Enable the sensor as the health sensors are disabled by default
|
|
|
|
entity_entry = entity_registry.async_get_or_create(
|
|
|
|
"sensor",
|
|
|
|
"ring",
|
|
|
|
f"{device_id}-{sensor_name}",
|
|
|
|
suggested_object_id=f"{device_name}_{sensor_name}",
|
|
|
|
disabled_by=None,
|
2020-01-14 20:54:45 +00:00
|
|
|
)
|
2024-06-06 18:13:19 +00:00
|
|
|
assert entity_entry.disabled is False
|
|
|
|
assert entity_entry.entity_id == entity_id
|
2017-03-06 11:15:08 +00:00
|
|
|
|
2024-06-06 18:13:19 +00:00
|
|
|
await setup_platform(hass, "sensor")
|
|
|
|
await hass.async_block_till_done()
|
2024-01-31 18:39:18 +00:00
|
|
|
|
2024-06-06 18:13:19 +00:00
|
|
|
sensor_state = hass.states.get(entity_id)
|
|
|
|
assert sensor_state is not None
|
|
|
|
assert sensor_state.state == "unknown"
|
2024-01-31 18:39:18 +00:00
|
|
|
|
2024-06-06 18:13:19 +00:00
|
|
|
freezer.tick(SCAN_INTERVAL)
|
|
|
|
async_fire_time_changed(hass)
|
|
|
|
await hass.async_block_till_done(wait_background_tasks=True)
|
|
|
|
sensor_state = hass.states.get(entity_id)
|
|
|
|
assert sensor_state is not None
|
|
|
|
assert sensor_state.state == expected_value
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("device_name", "sensor_name", "expected_value"),
|
|
|
|
[
|
|
|
|
("front_door", "last_motion", "2017-03-05T15:03:40+00:00"),
|
|
|
|
("front_door", "last_ding", "2018-03-05T15:03:40+00:00"),
|
|
|
|
("front_door", "last_activity", "2018-03-05T15:03:40+00:00"),
|
|
|
|
("front", "last_motion", "2017-03-05T15:03:40+00:00"),
|
|
|
|
("ingress", "last_activity", "2024-02-02T11:21:24+00:00"),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
"doorbell-motion",
|
|
|
|
"doorbell-ding",
|
|
|
|
"doorbell-activity",
|
|
|
|
"stickup_cam-motion",
|
|
|
|
"other-activity",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_history_sensor(
|
2024-03-15 11:59:36 +00:00
|
|
|
hass: HomeAssistant,
|
2024-06-06 18:13:19 +00:00
|
|
|
mock_ring_client,
|
2024-03-15 11:59:36 +00:00
|
|
|
freezer: FrozenDateTimeFactory,
|
2024-06-06 18:13:19 +00:00
|
|
|
device_name,
|
|
|
|
sensor_name,
|
|
|
|
expected_value,
|
2024-03-15 11:59:36 +00:00
|
|
|
) -> None:
|
2024-06-06 18:13:19 +00:00
|
|
|
"""Test the Ring sensors."""
|
|
|
|
await setup_platform(hass, "sensor")
|
2024-03-15 11:59:36 +00:00
|
|
|
|
2024-06-06 18:13:19 +00:00
|
|
|
entity_id = f"sensor.{device_name}_{sensor_name}"
|
|
|
|
sensor_state = hass.states.get(entity_id)
|
|
|
|
assert sensor_state is not None
|
|
|
|
assert sensor_state.state == "unknown"
|
2024-03-15 11:59:36 +00:00
|
|
|
|
2024-06-06 18:13:19 +00:00
|
|
|
freezer.tick(SCAN_INTERVAL)
|
|
|
|
async_fire_time_changed(hass)
|
|
|
|
await hass.async_block_till_done(wait_background_tasks=True)
|
|
|
|
sensor_state = hass.states.get(entity_id)
|
|
|
|
assert sensor_state is not None
|
|
|
|
assert sensor_state.state == expected_value
|
2024-03-15 11:59:36 +00:00
|
|
|
|
|
|
|
|
2024-01-31 18:39:18 +00:00
|
|
|
async def test_only_chime_devices(
|
|
|
|
hass: HomeAssistant,
|
2024-06-06 18:13:19 +00:00
|
|
|
mock_ring_client,
|
|
|
|
mock_ring_devices,
|
2024-01-31 18:39:18 +00:00
|
|
|
freezer: FrozenDateTimeFactory,
|
2024-05-29 12:10:00 +00:00
|
|
|
caplog: pytest.LogCaptureFixture,
|
2024-01-31 18:39:18 +00:00
|
|
|
) -> None:
|
|
|
|
"""Tests the update service works correctly if only chimes are returned."""
|
2024-05-20 09:06:03 +00:00
|
|
|
await hass.config.async_set_time_zone("UTC")
|
2024-01-31 18:39:18 +00:00
|
|
|
freezer.move_to("2021-01-09 12:00:00+00:00")
|
2024-06-06 18:13:19 +00:00
|
|
|
|
|
|
|
mock_ring_devices.all_devices = mock_ring_devices.chimes
|
|
|
|
|
2024-01-31 18:39:18 +00:00
|
|
|
await setup_platform(hass, Platform.SENSOR)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
caplog.clear()
|
|
|
|
freezer.tick(SCAN_INTERVAL)
|
|
|
|
async_fire_time_changed(hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert "UnboundLocalError" not in caplog.text # For issue #109210
|