1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-26 04:46:08 +00:00

Improve SRP Energy coordinator (#99010)

* Improve SRP Energy coordinator

* Use time instead of asyncio
This commit is contained in:
Joost Lekkerkerker
2023-08-25 11:19:40 +02:00
committed by GitHub
parent 11c5e3534a
commit da9fc495ca
2 changed files with 51 additions and 32 deletions

View File

@@ -1,7 +1,7 @@
"""Tests for the srp_energy sensor platform."""
import time
from unittest.mock import patch
import pytest
from requests.models import HTTPError
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
@@ -46,10 +46,8 @@ async def test_srp_entity(hass: HomeAssistant, init_integration) -> None:
assert usage_state.attributes.get(ATTR_ICON) == "mdi:flash"
@pytest.mark.parametrize("error", [TimeoutError, HTTPError])
async def test_srp_entity_update_failed(
hass: HomeAssistant,
error: Exception,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the SrpEntity."""
@@ -59,7 +57,30 @@ async def test_srp_entity_update_failed(
) as srp_energy_mock:
client = srp_energy_mock.return_value
client.validate.return_value = True
client.usage.side_effect = error
client.usage.side_effect = HTTPError
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
usage_state = hass.states.get("sensor.home_energy_usage")
assert usage_state is None
async def test_srp_entity_timeout(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the SrpEntity timing out."""
with patch(
"homeassistant.components.srp_energy.SrpEnergyClient", autospec=True
) as srp_energy_mock, patch(
"homeassistant.components.srp_energy.coordinator.TIMEOUT", 0
):
client = srp_energy_mock.return_value
client.validate.return_value = True
client.usage = lambda _, __, ___: time.sleep(1)
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)