2021-10-14 22:50:09 +00:00
|
|
|
"""Test the UptimeRobot init."""
|
2021-08-08 13:41:05 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from pytest import LogCaptureFixture
|
2021-08-10 14:29:51 +00:00
|
|
|
from pyuptimerobot import UptimeRobotAuthenticationException, UptimeRobotException
|
2021-08-08 13:41:05 +00:00
|
|
|
|
|
|
|
from homeassistant import config_entries
|
2021-08-10 14:29:51 +00:00
|
|
|
from homeassistant.components.uptimerobot.const import (
|
|
|
|
COORDINATOR_UPDATE_INTERVAL,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
|
|
|
from homeassistant.const import STATE_ON, STATE_UNAVAILABLE
|
2021-08-08 13:41:05 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-05-18 11:12:38 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2021-08-08 13:41:05 +00:00
|
|
|
from homeassistant.util import dt
|
|
|
|
|
2021-08-10 14:29:51 +00:00
|
|
|
from .common import (
|
|
|
|
MOCK_UPTIMEROBOT_CONFIG_ENTRY_DATA,
|
2022-03-18 11:18:19 +00:00
|
|
|
MOCK_UPTIMEROBOT_CONFIG_ENTRY_DATA_KEY_READ_ONLY,
|
2021-08-10 14:29:51 +00:00
|
|
|
MOCK_UPTIMEROBOT_MONITOR,
|
2022-01-26 15:48:15 +00:00
|
|
|
UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY,
|
2021-08-10 14:29:51 +00:00
|
|
|
MockApiResponseKey,
|
|
|
|
mock_uptimerobot_api_response,
|
|
|
|
setup_uptimerobot_integration,
|
|
|
|
)
|
|
|
|
|
2021-08-08 13:41:05 +00:00
|
|
|
from tests.common import MockConfigEntry, async_fire_time_changed
|
|
|
|
|
|
|
|
|
|
|
|
async def test_reauthentication_trigger_in_setup(
|
|
|
|
hass: HomeAssistant, caplog: LogCaptureFixture
|
|
|
|
):
|
|
|
|
"""Test reauthentication trigger."""
|
2021-08-10 14:29:51 +00:00
|
|
|
mock_config_entry = MockConfigEntry(**MOCK_UPTIMEROBOT_CONFIG_ENTRY_DATA)
|
2021-08-08 13:41:05 +00:00
|
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"pyuptimerobot.UptimeRobot.async_get_monitors",
|
|
|
|
side_effect=UptimeRobotAuthenticationException,
|
|
|
|
):
|
|
|
|
|
|
|
|
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
|
|
|
|
|
|
assert mock_config_entry.state == config_entries.ConfigEntryState.SETUP_ERROR
|
|
|
|
assert mock_config_entry.reason == "could not authenticate"
|
|
|
|
|
|
|
|
assert len(flows) == 1
|
|
|
|
flow = flows[0]
|
|
|
|
assert flow["step_id"] == "reauth_confirm"
|
|
|
|
assert flow["handler"] == DOMAIN
|
|
|
|
assert flow["context"]["source"] == config_entries.SOURCE_REAUTH
|
|
|
|
assert flow["context"]["entry_id"] == mock_config_entry.entry_id
|
|
|
|
|
|
|
|
assert (
|
|
|
|
"Config entry 'test@test.test' for uptimerobot integration could not authenticate"
|
|
|
|
in caplog.text
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-03-18 11:18:19 +00:00
|
|
|
async def test_reauthentication_trigger_key_read_only(
|
|
|
|
hass: HomeAssistant, caplog: LogCaptureFixture
|
|
|
|
):
|
|
|
|
"""Test reauthentication trigger."""
|
|
|
|
mock_config_entry = MockConfigEntry(
|
|
|
|
**MOCK_UPTIMEROBOT_CONFIG_ENTRY_DATA_KEY_READ_ONLY
|
|
|
|
)
|
|
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
|
|
|
|
|
|
assert mock_config_entry.state == config_entries.ConfigEntryState.SETUP_ERROR
|
|
|
|
assert (
|
|
|
|
mock_config_entry.reason
|
|
|
|
== "Wrong API key type detected, use the 'main' API key"
|
|
|
|
)
|
|
|
|
|
|
|
|
assert len(flows) == 1
|
|
|
|
flow = flows[0]
|
|
|
|
assert flow["step_id"] == "reauth_confirm"
|
|
|
|
assert flow["handler"] == DOMAIN
|
|
|
|
assert flow["context"]["source"] == config_entries.SOURCE_REAUTH
|
|
|
|
assert flow["context"]["entry_id"] == mock_config_entry.entry_id
|
|
|
|
|
|
|
|
assert (
|
|
|
|
"Config entry 'test@test.test' for uptimerobot integration could not authenticate"
|
|
|
|
in caplog.text
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-08 13:41:05 +00:00
|
|
|
async def test_reauthentication_trigger_after_setup(
|
|
|
|
hass: HomeAssistant, caplog: LogCaptureFixture
|
|
|
|
):
|
|
|
|
"""Test reauthentication trigger."""
|
2021-08-10 14:29:51 +00:00
|
|
|
mock_config_entry = await setup_uptimerobot_integration(hass)
|
2021-08-08 13:41:05 +00:00
|
|
|
|
2022-01-26 15:48:15 +00:00
|
|
|
binary_sensor = hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY)
|
2021-08-08 13:41:05 +00:00
|
|
|
assert mock_config_entry.state == config_entries.ConfigEntryState.LOADED
|
2021-08-10 14:29:51 +00:00
|
|
|
assert binary_sensor.state == STATE_ON
|
2021-08-08 13:41:05 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"pyuptimerobot.UptimeRobot.async_get_monitors",
|
|
|
|
side_effect=UptimeRobotAuthenticationException,
|
|
|
|
):
|
|
|
|
|
2021-08-10 14:29:51 +00:00
|
|
|
async_fire_time_changed(hass, dt.utcnow() + COORDINATOR_UPDATE_INTERVAL)
|
2021-08-08 13:41:05 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
2022-01-26 15:48:15 +00:00
|
|
|
assert (
|
|
|
|
hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state
|
|
|
|
== STATE_UNAVAILABLE
|
|
|
|
)
|
2021-08-08 13:41:05 +00:00
|
|
|
|
|
|
|
assert "Authentication failed while fetching uptimerobot data" in caplog.text
|
|
|
|
|
|
|
|
assert len(flows) == 1
|
|
|
|
flow = flows[0]
|
|
|
|
assert flow["step_id"] == "reauth_confirm"
|
|
|
|
assert flow["handler"] == DOMAIN
|
|
|
|
assert flow["context"]["source"] == config_entries.SOURCE_REAUTH
|
|
|
|
assert flow["context"]["entry_id"] == mock_config_entry.entry_id
|
2021-08-10 14:29:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_integration_reload(hass: HomeAssistant):
|
|
|
|
"""Test integration reload."""
|
|
|
|
mock_entry = await setup_uptimerobot_integration(hass)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"pyuptimerobot.UptimeRobot.async_get_monitors",
|
|
|
|
return_value=mock_uptimerobot_api_response(),
|
|
|
|
):
|
|
|
|
assert await hass.config_entries.async_reload(mock_entry.entry_id)
|
|
|
|
async_fire_time_changed(hass, dt.utcnow() + COORDINATOR_UPDATE_INTERVAL)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
entry = hass.config_entries.async_get_entry(mock_entry.entry_id)
|
|
|
|
assert entry.state == config_entries.ConfigEntryState.LOADED
|
2022-01-26 15:48:15 +00:00
|
|
|
assert hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state == STATE_ON
|
2021-08-10 14:29:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_update_errors(hass: HomeAssistant, caplog: LogCaptureFixture):
|
|
|
|
"""Test errors during updates."""
|
|
|
|
await setup_uptimerobot_integration(hass)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"pyuptimerobot.UptimeRobot.async_get_monitors",
|
|
|
|
side_effect=UptimeRobotException,
|
|
|
|
):
|
|
|
|
async_fire_time_changed(hass, dt.utcnow() + COORDINATOR_UPDATE_INTERVAL)
|
|
|
|
await hass.async_block_till_done()
|
2022-01-26 15:48:15 +00:00
|
|
|
assert (
|
|
|
|
hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state
|
|
|
|
== STATE_UNAVAILABLE
|
|
|
|
)
|
2021-08-10 14:29:51 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"pyuptimerobot.UptimeRobot.async_get_monitors",
|
|
|
|
return_value=mock_uptimerobot_api_response(),
|
|
|
|
):
|
|
|
|
async_fire_time_changed(hass, dt.utcnow() + COORDINATOR_UPDATE_INTERVAL)
|
|
|
|
await hass.async_block_till_done()
|
2022-01-26 15:48:15 +00:00
|
|
|
assert hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state == STATE_ON
|
2021-08-10 14:29:51 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"pyuptimerobot.UptimeRobot.async_get_monitors",
|
|
|
|
return_value=mock_uptimerobot_api_response(key=MockApiResponseKey.ERROR),
|
|
|
|
):
|
|
|
|
async_fire_time_changed(hass, dt.utcnow() + COORDINATOR_UPDATE_INTERVAL)
|
|
|
|
await hass.async_block_till_done()
|
2022-01-26 15:48:15 +00:00
|
|
|
assert (
|
|
|
|
hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state
|
|
|
|
== STATE_UNAVAILABLE
|
|
|
|
)
|
2021-08-10 14:29:51 +00:00
|
|
|
|
|
|
|
assert "Error fetching uptimerobot data: test error from API" in caplog.text
|
|
|
|
|
|
|
|
|
|
|
|
async def test_device_management(hass: HomeAssistant):
|
|
|
|
"""Test that we are adding and removing devices for monitors returned from the API."""
|
|
|
|
mock_entry = await setup_uptimerobot_integration(hass)
|
2022-05-18 11:12:38 +00:00
|
|
|
dev_reg = dr.async_get(hass)
|
2021-08-10 14:29:51 +00:00
|
|
|
|
2022-05-18 11:12:38 +00:00
|
|
|
devices = dr.async_entries_for_config_entry(dev_reg, mock_entry.entry_id)
|
2021-08-10 14:29:51 +00:00
|
|
|
assert len(devices) == 1
|
|
|
|
|
|
|
|
assert devices[0].identifiers == {(DOMAIN, "1234")}
|
2021-08-11 09:27:41 +00:00
|
|
|
assert devices[0].name == "Test monitor"
|
2021-08-10 14:29:51 +00:00
|
|
|
|
2022-01-26 15:48:15 +00:00
|
|
|
assert hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state == STATE_ON
|
|
|
|
assert hass.states.get(f"{UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY}_2") is None
|
2021-08-10 14:29:51 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"pyuptimerobot.UptimeRobot.async_get_monitors",
|
|
|
|
return_value=mock_uptimerobot_api_response(
|
|
|
|
data=[MOCK_UPTIMEROBOT_MONITOR, {**MOCK_UPTIMEROBOT_MONITOR, "id": 12345}]
|
|
|
|
),
|
|
|
|
):
|
|
|
|
async_fire_time_changed(hass, dt.utcnow() + COORDINATOR_UPDATE_INTERVAL)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2022-05-18 11:12:38 +00:00
|
|
|
devices = dr.async_entries_for_config_entry(dev_reg, mock_entry.entry_id)
|
2021-08-10 14:29:51 +00:00
|
|
|
assert len(devices) == 2
|
|
|
|
assert devices[0].identifiers == {(DOMAIN, "1234")}
|
|
|
|
assert devices[1].identifiers == {(DOMAIN, "12345")}
|
|
|
|
|
2022-01-26 15:48:15 +00:00
|
|
|
assert hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state == STATE_ON
|
|
|
|
assert (
|
|
|
|
hass.states.get(f"{UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY}_2").state == STATE_ON
|
|
|
|
)
|
2021-08-10 14:29:51 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"pyuptimerobot.UptimeRobot.async_get_monitors",
|
|
|
|
return_value=mock_uptimerobot_api_response(),
|
|
|
|
):
|
|
|
|
async_fire_time_changed(hass, dt.utcnow() + COORDINATOR_UPDATE_INTERVAL)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2022-05-18 11:12:38 +00:00
|
|
|
devices = dr.async_entries_for_config_entry(dev_reg, mock_entry.entry_id)
|
2021-08-10 14:29:51 +00:00
|
|
|
assert len(devices) == 1
|
|
|
|
assert devices[0].identifiers == {(DOMAIN, "1234")}
|
|
|
|
|
2022-01-26 15:48:15 +00:00
|
|
|
assert hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state == STATE_ON
|
|
|
|
assert hass.states.get(f"{UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY}_2") is None
|