Try to fix flaky Risco test (#44788)

This commit is contained in:
On Freund 2021-01-04 16:23:47 +02:00 committed by GitHub
parent 3a32e16f4d
commit e9f7e67f4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 26 deletions

View File

@ -60,10 +60,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
EVENTS_COORDINATOR: events_coordinator,
}
for component in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, component)
async def start_platforms():
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_setup(entry, component)
for component in PLATFORMS
]
)
await events_coordinator.async_refresh()
hass.async_create_task(start_platforms())
return True

View File

@ -73,7 +73,6 @@ class RiscoSensor(CoordinatorEntity):
self.async_on_remove(
self.coordinator.async_add_listener(self._refresh_from_coordinator)
)
await self.coordinator.async_request_refresh()
def _refresh_from_coordinator(self):
events = self.coordinator.data

View File

@ -167,7 +167,7 @@ async def _check_state(hass, alarm, property, state, entity_id, partition_id):
async def test_states(hass, two_part_alarm):
"""Test the various alarm states."""
await setup_risco(hass, CUSTOM_MAPPING_OPTIONS)
await setup_risco(hass, [], CUSTOM_MAPPING_OPTIONS)
assert hass.states.get(FIRST_ENTITY_ID).state == STATE_UNKNOWN
for partition_id, entity_id in {0: FIRST_ENTITY_ID, 1: SECOND_ENTITY_ID}.items():
@ -249,7 +249,7 @@ async def _call_alarm_service(hass, service, entity_id, **kwargs):
async def test_sets_custom_mapping(hass, two_part_alarm):
"""Test settings the various modes when mapping some states."""
await setup_risco(hass, CUSTOM_MAPPING_OPTIONS)
await setup_risco(hass, [], CUSTOM_MAPPING_OPTIONS)
registry = await hass.helpers.entity_registry.async_get_registry()
entity = registry.async_get(FIRST_ENTITY_ID)
@ -275,7 +275,7 @@ async def test_sets_custom_mapping(hass, two_part_alarm):
async def test_sets_full_custom_mapping(hass, two_part_alarm):
"""Test settings the various modes when mapping all states."""
await setup_risco(hass, FULL_CUSTOM_MAPPING)
await setup_risco(hass, [], FULL_CUSTOM_MAPPING)
registry = await hass.helpers.entity_registry.async_get_registry()
entity = registry.async_get(FIRST_ENTITY_ID)
@ -309,7 +309,7 @@ async def test_sets_full_custom_mapping(hass, two_part_alarm):
async def test_sets_with_correct_code(hass, two_part_alarm):
"""Test settings the various modes when code is required."""
await setup_risco(hass, {**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS})
await setup_risco(hass, [], {**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS})
code = {"code": 1234}
await _test_service_call(
@ -351,7 +351,7 @@ async def test_sets_with_correct_code(hass, two_part_alarm):
async def test_sets_with_incorrect_code(hass, two_part_alarm):
"""Test settings the various modes when code is required and incorrect."""
await setup_risco(hass, {**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS})
await setup_risco(hass, [], {**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS})
code = {"code": 4321}
await _test_no_service_call(

View File

@ -1,17 +1,19 @@
"""Tests for the Risco event sensors."""
from unittest.mock import MagicMock, patch
from datetime import timedelta
from unittest.mock import MagicMock, PropertyMock, patch
from homeassistant.components.risco import (
LAST_EVENT_TIMESTAMP_KEY,
CannotConnectError,
UnauthorizedError,
)
from homeassistant.components.risco.const import DOMAIN, EVENTS_COORDINATOR
from homeassistant.components.risco.const import DOMAIN
from homeassistant.util import dt
from .util import TEST_CONFIG, setup_risco
from .util import TEST_CONFIG, TEST_SITE_UUID, setup_risco
from .util import two_zone_alarm # noqa: F401
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, async_fire_time_changed
ENTITY_IDS = {
"Alarm": "sensor.risco_test_site_name_alarm_events",
@ -171,31 +173,28 @@ async def test_setup(hass, two_zone_alarm): # noqa: F811
assert not registry.async_is_registered(id)
with patch(
"homeassistant.components.risco.RiscoAPI.get_events",
return_value=TEST_EVENTS,
"homeassistant.components.risco.RiscoAPI.site_uuid",
new_callable=PropertyMock(return_value=TEST_SITE_UUID),
), patch(
"homeassistant.components.risco.Store.async_save",
) as save_mock:
entry = await setup_risco(hass)
await hass.async_block_till_done()
await setup_risco(hass, TEST_EVENTS)
for id in ENTITY_IDS.values():
assert registry.async_is_registered(id)
save_mock.assert_awaited_once_with(
{LAST_EVENT_TIMESTAMP_KEY: TEST_EVENTS[0].time}
)
for category, entity_id in ENTITY_IDS.items():
_check_state(hass, category, entity_id)
for id in ENTITY_IDS.values():
assert registry.async_is_registered(id)
for category, entity_id in ENTITY_IDS.items():
_check_state(hass, category, entity_id)
coordinator = hass.data[DOMAIN][entry.entry_id][EVENTS_COORDINATOR]
with patch(
"homeassistant.components.risco.RiscoAPI.get_events", return_value=[]
) as events_mock, patch(
"homeassistant.components.risco.Store.async_load",
return_value={LAST_EVENT_TIMESTAMP_KEY: TEST_EVENTS[0].time},
):
await coordinator.async_refresh()
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=65))
await hass.async_block_till_done()
events_mock.assert_awaited_once_with(TEST_EVENTS[0].time, 10)

View File

@ -17,7 +17,7 @@ TEST_SITE_UUID = "test-site-uuid"
TEST_SITE_NAME = "test-site-name"
async def setup_risco(hass, options={}):
async def setup_risco(hass, events=[], options={}):
"""Set up a Risco integration for testing."""
config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG, options=options)
config_entry.add_to_hass(hass)
@ -33,6 +33,9 @@ async def setup_risco(hass, options={}):
new_callable=PropertyMock(return_value=TEST_SITE_NAME),
), patch(
"homeassistant.components.risco.RiscoAPI.close"
), patch(
"homeassistant.components.risco.RiscoAPI.get_events",
return_value=events,
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()