1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Fix Pylance errors in UptimeRobot tests (#152185)

This commit is contained in:
Simone Chemelli
2025-09-12 18:18:27 +02:00
committed by GitHub
parent d324021a3f
commit 984590c6d1
6 changed files with 43 additions and 40 deletions

View File

@@ -80,7 +80,7 @@ class MockApiResponseKey(str, Enum):
def mock_uptimerobot_api_response(
data: dict[str, Any]
data: list[dict[str, Any]]
| list[UptimeRobotMonitor]
| UptimeRobotAccount
| UptimeRobotApiError
@@ -115,8 +115,10 @@ async def setup_uptimerobot_integration(hass: HomeAssistant) -> MockConfigEntry:
assert await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
assert hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state == STATE_ON
assert hass.states.get(UPTIMEROBOT_SENSOR_TEST_ENTITY).state == STATE_UP
assert (entity := hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY))
assert entity.state == STATE_ON
assert (entity := hass.states.get(UPTIMEROBOT_SENSOR_TEST_ENTITY))
assert entity.state == STATE_UP
assert mock_entry.state is ConfigEntryState.LOADED
return mock_entry

View File

@@ -26,8 +26,7 @@ async def test_presentation(hass: HomeAssistant) -> None:
"""Test the presenstation of UptimeRobot binary_sensors."""
await setup_uptimerobot_integration(hass)
entity = hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY)
assert (entity := hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY))
assert entity.state == STATE_ON
assert entity.attributes["device_class"] == BinarySensorDeviceClass.CONNECTIVITY
assert entity.attributes["attribution"] == ATTRIBUTION
@@ -38,7 +37,7 @@ async def test_unavailable_on_update_failure(hass: HomeAssistant) -> None:
"""Test entity unavailable on update failure."""
await setup_uptimerobot_integration(hass)
entity = hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY)
assert (entity := hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY))
assert entity.state == STATE_ON
with patch(
@@ -48,5 +47,5 @@ async def test_unavailable_on_update_failure(hass: HomeAssistant) -> None:
async_fire_time_changed(hass, dt_util.utcnow() + COORDINATOR_UPDATE_INTERVAL)
await hass.async_block_till_done()
entity = hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY)
assert (entity := hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY))
assert entity.state == STATE_UNAVAILABLE

View File

@@ -80,6 +80,7 @@ async def test_user_key_read_only(hass: HomeAssistant) -> None:
await hass.async_block_till_done()
assert result2["type"] is FlowResultType.FORM
assert result2["errors"]
assert result2["errors"]["base"] == "not_main_key"
@@ -107,6 +108,7 @@ async def test_exception_thrown(hass: HomeAssistant, exception, error_key) -> No
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"]
assert result2["errors"]["base"] == error_key
@@ -125,6 +127,7 @@ async def test_api_error(hass: HomeAssistant, caplog: pytest.LogCaptureFixture)
{CONF_API_KEY: MOCK_UPTIMEROBOT_API_KEY},
)
assert result2["errors"]
assert result2["errors"]["base"] == "unknown"
assert "test error from API." in caplog.text
@@ -227,6 +230,7 @@ async def test_reauthentication_failure(
assert result2["step_id"] == "reauth_confirm"
assert result2["type"] is FlowResultType.FORM
assert result2["errors"]
assert result2["errors"]["base"] == "unknown"
@@ -299,6 +303,7 @@ async def test_reauthentication_failure_account_not_matching(
assert result2["step_id"] == "reauth_confirm"
assert result2["type"] is FlowResultType.FORM
assert result2["errors"]
assert result2["errors"]["base"] == "reauth_failed_matching_account"
@@ -374,6 +379,7 @@ async def test_reconfigure_failed(
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"]
assert result2["errors"]["base"] == "invalid_api_key"
new_key = "u0242ac120003-new"

View File

@@ -102,7 +102,7 @@ async def test_reauthentication_trigger_after_setup(
"""Test reauthentication trigger."""
mock_config_entry = await setup_uptimerobot_integration(hass)
binary_sensor = hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY)
assert (binary_sensor := hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY))
assert mock_config_entry.state is ConfigEntryState.LOADED
assert binary_sensor.state == STATE_ON
@@ -115,10 +115,8 @@ async def test_reauthentication_trigger_after_setup(
await hass.async_block_till_done()
flows = hass.config_entries.flow.async_progress()
assert (
hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state
== STATE_UNAVAILABLE
)
assert (entity := hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY))
assert entity.state == STATE_UNAVAILABLE
assert "Authentication failed while fetching uptimerobot data" in caplog.text
@@ -146,9 +144,10 @@ async def test_integration_reload(
async_fire_time_changed(hass)
await hass.async_block_till_done()
entry = hass.config_entries.async_get_entry(mock_entry.entry_id)
assert (entry := hass.config_entries.async_get_entry(mock_entry.entry_id))
assert entry.state is ConfigEntryState.LOADED
assert hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state == STATE_ON
assert (entity := hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY))
assert entity.state == STATE_ON
async def test_update_errors(
@@ -166,10 +165,8 @@ async def test_update_errors(
freezer.tick(COORDINATOR_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert (
hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state
== STATE_UNAVAILABLE
)
assert (entity := hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY))
assert entity.state == STATE_UNAVAILABLE
with patch(
"pyuptimerobot.UptimeRobot.async_get_monitors",
@@ -178,7 +175,8 @@ async def test_update_errors(
freezer.tick(COORDINATOR_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state == STATE_ON
assert (entity := hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY))
assert entity.state == STATE_ON
with patch(
"pyuptimerobot.UptimeRobot.async_get_monitors",
@@ -187,10 +185,8 @@ async def test_update_errors(
freezer.tick(COORDINATOR_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert (
hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state
== STATE_UNAVAILABLE
)
assert (entity := hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY))
assert entity.state == STATE_UNAVAILABLE
assert "Error fetching uptimerobot data: test error from API" in caplog.text
@@ -209,7 +205,8 @@ async def test_device_management(
assert devices[0].identifiers == {(DOMAIN, "1234")}
assert devices[0].name == "Test monitor"
assert hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state == STATE_ON
assert (entity := hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY))
assert entity.state == STATE_ON
assert hass.states.get(f"{UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY}_2") is None
with patch(
@@ -227,10 +224,10 @@ async def test_device_management(
assert devices[0].identifiers == {(DOMAIN, "1234")}
assert devices[1].identifiers == {(DOMAIN, "12345")}
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
)
assert (entity := hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY))
assert entity.state == STATE_ON
assert (entity2 := hass.states.get(f"{UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY}_2"))
assert entity2.state == STATE_ON
with patch(
"pyuptimerobot.UptimeRobot.async_get_monitors",
@@ -244,5 +241,6 @@ async def test_device_management(
assert len(devices) == 1
assert devices[0].identifiers == {(DOMAIN, "1234")}
assert hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY).state == STATE_ON
assert (entity := hass.states.get(UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY))
assert entity.state == STATE_ON
assert hass.states.get(f"{UPTIMEROBOT_BINARY_SENSOR_TEST_ENTITY}_2") is None

View File

@@ -24,8 +24,7 @@ async def test_presentation(hass: HomeAssistant) -> None:
"""Test the presentation of UptimeRobot sensors."""
await setup_uptimerobot_integration(hass)
entity = hass.states.get(UPTIMEROBOT_SENSOR_TEST_ENTITY)
assert (entity := hass.states.get(UPTIMEROBOT_SENSOR_TEST_ENTITY)) is not None
assert entity.state == STATE_UP
assert entity.attributes["target"] == MOCK_UPTIMEROBOT_MONITOR["url"]
assert entity.attributes["device_class"] == SensorDeviceClass.ENUM
@@ -42,7 +41,7 @@ async def test_unavailable_on_update_failure(hass: HomeAssistant) -> None:
"""Test entity unavailable on update failure."""
await setup_uptimerobot_integration(hass)
entity = hass.states.get(UPTIMEROBOT_SENSOR_TEST_ENTITY)
assert (entity := hass.states.get(UPTIMEROBOT_SENSOR_TEST_ENTITY)) is not None
assert entity.state == STATE_UP
with patch(
@@ -52,5 +51,5 @@ async def test_unavailable_on_update_failure(hass: HomeAssistant) -> None:
async_fire_time_changed(hass, dt_util.utcnow() + COORDINATOR_UPDATE_INTERVAL)
await hass.async_block_till_done()
entity = hass.states.get(UPTIMEROBOT_SENSOR_TEST_ENTITY)
assert (entity := hass.states.get(UPTIMEROBOT_SENSOR_TEST_ENTITY)) is not None
assert entity.state == STATE_UNAVAILABLE

View File

@@ -33,8 +33,7 @@ async def test_presentation(hass: HomeAssistant) -> None:
"""Test the presentation of UptimeRobot switches."""
await setup_uptimerobot_integration(hass)
entity = hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)
assert (entity := hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)) is not None
assert entity.state == STATE_ON
assert entity.attributes["target"] == MOCK_UPTIMEROBOT_MONITOR["url"]
@@ -67,7 +66,7 @@ async def test_switch_off(hass: HomeAssistant) -> None:
blocking=True,
)
entity = hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)
assert (entity := hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)) is not None
assert entity.state == STATE_OFF
@@ -97,7 +96,7 @@ async def test_switch_on(hass: HomeAssistant) -> None:
blocking=True,
)
entity = hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)
assert (entity := hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)) is not None
assert entity.state == STATE_ON
@@ -107,7 +106,7 @@ async def test_authentication_error(
"""Test authentication error turning switch on/off."""
await setup_uptimerobot_integration(hass)
entity = hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)
assert (entity := hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)) is not None
assert entity.state == STATE_ON
with (
@@ -133,7 +132,7 @@ async def test_action_execution_failure(hass: HomeAssistant) -> None:
"""Test turning switch on/off failure."""
await setup_uptimerobot_integration(hass)
entity = hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)
assert (entity := hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)) is not None
assert entity.state == STATE_ON
with (
@@ -161,7 +160,7 @@ async def test_switch_api_failure(hass: HomeAssistant) -> None:
"""Test general exception turning switch on/off."""
await setup_uptimerobot_integration(hass)
entity = hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)
assert (entity := hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)) is not None
assert entity.state == STATE_ON
with patch(