Skip to content

Commit

Permalink
Try to fix flaky Risco test (#44788)
Browse files Browse the repository at this point in the history
  • Loading branch information
OnFreund authored Jan 4, 2021
1 parent 3a32e16 commit e9f7e67
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
12 changes: 9 additions & 3 deletions homeassistant/components/risco/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/risco/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ async def async_added_to_hass(self):
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
Expand Down
10 changes: 5 additions & 5 deletions tests/components/risco/test_alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
31 changes: 15 additions & 16 deletions tests/components/risco/test_sensor.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion tests/components/risco/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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)
Expand All @@ -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()
Expand Down

0 comments on commit e9f7e67

Please sign in to comment.