Skip to content

Commit

Permalink
First pass fixing tests that raise exceptions caught by asyncio.gather
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Aug 11, 2019
1 parent ce0edf8 commit 0eb6f96
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 14 deletions.
6 changes: 3 additions & 3 deletions homeassistant/components/demo/__init__.py
Expand Up @@ -176,9 +176,6 @@ async def demo_start_listener(_event):

async def finish_setup(hass, config):
"""Finish set up once demo platforms are set up."""
lights = sorted(hass.states.async_entity_ids("light"))
switches = sorted(hass.states.async_entity_ids("switch"))

# Set up history graph
await bootstrap.async_setup_component(
hass,
Expand All @@ -195,6 +192,9 @@ async def finish_setup(hass, config):
},
)

lights = sorted(hass.states.async_entity_ids("light"))
switches = sorted(hass.states.async_entity_ids("switch"))

# Set up scripts
await bootstrap.async_setup_component(
hass,
Expand Down
20 changes: 20 additions & 0 deletions homeassistant/components/vacuum/__init__.py
Expand Up @@ -321,6 +321,14 @@ async def async_start_pause(self, **kwargs):
"""
await self.hass.async_add_executor_job(partial(self.start_pause, **kwargs))

async def async_pause(self):
"""Not supported."""
pass

async def async_start(self):
"""Not supported."""
pass


class StateVacuumDevice(_BaseVacuum):
"""Representation of a vacuum cleaner robot that supports states."""
Expand Down Expand Up @@ -375,3 +383,15 @@ async def async_pause(self):
This method must be run in the event loop.
"""
await self.hass.async_add_executor_job(self.pause)

async def async_turn_on(self, **kwargs):
"""Not supported."""
pass

async def async_turn_off(self, **kwargs):
"""Not supported."""
pass

async def async_toggle(self, **kwargs):
"""Not supported."""
pass
3 changes: 2 additions & 1 deletion homeassistant/config.py
Expand Up @@ -322,7 +322,8 @@ def _load_hass_yaml_config() -> Dict:
config = load_yaml_config_file(path)
return config

config = await hass.async_add_executor_job(_load_hass_yaml_config)
# Not using async_add_executor_job because this is an internal method.
config = await hass.loop.run_in_executor(None, _load_hass_yaml_config)
core_config = config.get(CONF_CORE, {})
await merge_packages_config(hass, config, core_config.get(CONF_PACKAGES, {}))
return config
Expand Down
2 changes: 2 additions & 0 deletions tests/common.py
Expand Up @@ -971,6 +971,8 @@ async def flush_store(store):
if store._data is None:
return

store._async_cleanup_stop_listener()
store._async_cleanup_delay_listener()
await store._async_handle_write_data()


Expand Down
2 changes: 1 addition & 1 deletion tests/components/alexa/test_state_report.py
Expand Up @@ -5,7 +5,7 @@

async def test_report_state(hass, aioclient_mock):
"""Test proactive state reports."""
aioclient_mock.post(TEST_URL, json={"data": "is irrelevant"})
aioclient_mock.post(TEST_URL, json={"data": "is irrelevant"}, status=202)

hass.states.async_set(
"binary_sensor.test_contact",
Expand Down
8 changes: 8 additions & 0 deletions tests/components/axis/test_config_flow.py
Expand Up @@ -39,6 +39,8 @@ def mock_constructor(loop, host, username, password, port, web_proto):
mock_device.side_effect = mock_constructor
mock_device.vapix.params.system_serialnumber = "serialnumber"
mock_device.vapix.params.prodnbr = "prodnbr"
mock_device.vapix.params.prodtype = "prodtype"
mock_device.vapix.params.firmware_version = "firmware_version"

result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN, context={"source": "user"}
Expand Down Expand Up @@ -220,6 +222,10 @@ def mock_constructor(loop, host, username, password, port, web_proto):
return mock_device

mock_device.side_effect = mock_constructor
mock_device.vapix.params.system_serialnumber = "serialnumber"
mock_device.vapix.params.prodnbr = "prodnbr"
mock_device.vapix.params.prodtype = "prodtype"
mock_device.vapix.params.firmware_version = "firmware_version"

result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN,
Expand Down Expand Up @@ -338,6 +344,8 @@ def mock_constructor(loop, host, username, password, port, web_proto):
mock_device.side_effect = mock_constructor
mock_device.vapix.params.system_serialnumber = "serialnumber"
mock_device.vapix.params.prodnbr = "prodnbr"
mock_device.vapix.params.prodtype = "prodtype"
mock_device.vapix.params.firmware_version = "firmware_version"

result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN,
Expand Down
13 changes: 7 additions & 6 deletions tests/components/cloud/test_alexa_config.py
Expand Up @@ -52,16 +52,17 @@ def patch_sync_helper():
to_update = []
to_remove = []

async def sync_helper(to_upd, to_rem):
to_update.extend([ent_id for ent_id in to_upd if ent_id not in to_update])
to_remove.extend([ent_id for ent_id in to_rem if ent_id not in to_remove])
return True

with patch("homeassistant.components.cloud.alexa_config.SYNC_DELAY", 0), patch(
"homeassistant.components.cloud.alexa_config.AlexaConfig._sync_helper",
side_effect=mock_coro,
) as mock_helper:
side_effect=sync_helper,
):
yield to_update, to_remove

actual_to_update, actual_to_remove = mock_helper.mock_calls[0][1]
to_update.extend(actual_to_update)
to_remove.extend(actual_to_remove)


async def test_alexa_update_expose_trigger_sync(hass, cloud_prefs):
"""Test Alexa config responds to updating exposed entities."""
Expand Down
1 change: 1 addition & 0 deletions tests/components/deconz/test_binary_sensor.py
Expand Up @@ -115,6 +115,7 @@ async def test_add_new_sensor(hass):
sensor.name = "name"
sensor.type = "ZHAPresence"
sensor.BINARY = True
sensor.uniqueid = "1"
sensor.register_async_callback = Mock()
async_dispatcher_send(hass, gateway.async_event_new_device("sensor"), [sensor])
await hass.async_block_till_done()
Expand Down
1 change: 1 addition & 0 deletions tests/components/deconz/test_climate.py
Expand Up @@ -178,6 +178,7 @@ async def test_add_new_climate_device(hass):
sensor = Mock()
sensor.name = "name"
sensor.type = "ZHAThermostat"
sensor.uniqueid = "1"
sensor.register_async_callback = Mock()
async_dispatcher_send(hass, gateway.async_event_new_device("sensor"), [sensor])
await hass.async_block_till_done()
Expand Down
1 change: 1 addition & 0 deletions tests/components/deconz/test_cover.py
Expand Up @@ -132,6 +132,7 @@ async def test_add_new_cover(hass):
cover = Mock()
cover.name = "name"
cover.type = "Level controllable output"
cover.uniqueid = "1"
cover.register_async_callback = Mock()
async_dispatcher_send(hass, gateway.async_event_new_device("light"), [cover])
await hass.async_block_till_done()
Expand Down
1 change: 1 addition & 0 deletions tests/components/deconz/test_light.py
Expand Up @@ -190,6 +190,7 @@ async def test_add_new_light(hass):
gateway = await setup_gateway(hass, {})
light = Mock()
light.name = "name"
light.uniqueid = "1"
light.register_async_callback = Mock()
async_dispatcher_send(hass, gateway.async_event_new_device("light"), [light])
await hass.async_block_till_done()
Expand Down
1 change: 1 addition & 0 deletions tests/components/deconz/test_sensor.py
Expand Up @@ -158,6 +158,7 @@ async def test_add_new_sensor(hass):
sensor = Mock()
sensor.name = "name"
sensor.type = "ZHATemperature"
sensor.uniqueid = "1"
sensor.BINARY = False
sensor.register_async_callback = Mock()
async_dispatcher_send(hass, gateway.async_event_new_device("sensor"), [sensor])
Expand Down
1 change: 1 addition & 0 deletions tests/components/deconz/test_switch.py
Expand Up @@ -140,6 +140,7 @@ async def test_add_new_switch(hass):
switch = Mock()
switch.name = "name"
switch.type = "Smart plug"
switch.uniqueid = "1"
switch.register_async_callback = Mock()
async_dispatcher_send(hass, gateway.async_event_new_device("light"), [switch])
await hass.async_block_till_done()
Expand Down
16 changes: 14 additions & 2 deletions tests/components/homematicip_cloud/test_init.py
Expand Up @@ -68,7 +68,13 @@ async def test_setup_entry_successful(hass):
)
entry.add_to_hass(hass)
with patch.object(hmipc, "HomematicipHAP") as mock_hap:
mock_hap.return_value.async_setup.return_value = mock_coro(True)
instance = mock_hap.return_value
instance.async_setup.return_value = mock_coro(True)
instance.home.id = "1"
instance.home.modelType = "mock-type"
instance.home.name = "mock-name"
instance.home.currentAPVersion = "mock-ap-version"

assert (
await async_setup_component(
hass,
Expand Down Expand Up @@ -129,7 +135,13 @@ async def test_unload_entry(hass):
entry.add_to_hass(hass)

with patch.object(hmipc, "HomematicipHAP") as mock_hap:
mock_hap.return_value.async_setup.return_value = mock_coro(True)
instance = mock_hap.return_value
instance.async_setup.return_value = mock_coro(True)
instance.home.id = "1"
instance.home.modelType = "mock-type"
instance.home.name = "mock-name"
instance.home.currentAPVersion = "mock-ap-version"

assert await async_setup_component(hass, hmipc.DOMAIN, {}) is True

assert len(mock_hap.return_value.mock_calls) >= 1
Expand Down
2 changes: 2 additions & 0 deletions tests/components/smartthings/test_config_flow.py
Expand Up @@ -6,6 +6,7 @@
from pysmartthings import APIResponseError

from homeassistant import data_entry_flow
from homeassistant.setup import async_setup_component
from homeassistant.components import cloud
from homeassistant.components.smartthings import smartapp
from homeassistant.components.smartthings.config_flow import SmartThingsFlowHandler
Expand Down Expand Up @@ -288,6 +289,7 @@ async def test_multiple_config_entry_created_when_installed(
hass, app, locations, installed_apps, smartthings_mock
):
"""Test a config entries are created for multiple installs."""
assert await async_setup_component(hass, "persistent_notification", {})
flow = SmartThingsFlowHandler()
flow.hass = hass
flow.access_token = str(uuid4())
Expand Down
3 changes: 3 additions & 0 deletions tests/components/smartthings/test_init.py
Expand Up @@ -6,6 +6,7 @@
from pysmartthings import InstalledAppStatus, OAuthToken
import pytest

from homeassistant.setup import async_setup_component
from homeassistant.components import cloud, smartthings
from homeassistant.components.smartthings.const import (
CONF_CLOUDHOOK_URL,
Expand All @@ -25,6 +26,7 @@

async def test_migration_creates_new_flow(hass, smartthings_mock, config_entry):
"""Test migration deletes app and creates new flow."""
assert await async_setup_component(hass, "persistent_notification", {})
config_entry.version = 1
config_entry.add_to_hass(hass)

Expand All @@ -50,6 +52,7 @@ async def test_unrecoverable_api_errors_create_new_flow(
403 (forbidden/not found): Occurs when the app or installed app could
not be retrieved/found (likely deleted?)
"""
assert await async_setup_component(hass, "persistent_notification", {})
config_entry.add_to_hass(hass)
smartthings_mock.app.side_effect = ClientResponseError(None, None, status=401)

Expand Down
5 changes: 4 additions & 1 deletion tests/components/upnp/test_init.py
Expand Up @@ -18,7 +18,10 @@ class MockDevice(Device):

def __init__(self, udn):
"""Initializer."""
super().__init__(MagicMock())
device = MagicMock()
device.manufacturer = "mock-manuf"
device.name = "mock-name"
super().__init__(device)
self._udn = udn
self.added_port_mappings = []
self.removed_port_mappings = []
Expand Down

0 comments on commit 0eb6f96

Please sign in to comment.