Use a future for mock coro (#34989)

This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-04-30 16:31:00 -07:00
1 parent ba7391528f
commit 76f392476b
26 files changed
+154 -262

No files matched your search

+8 -7
View File
@@ -16,7 +16,8 @@ from homeassistant.components.melissa import DATA_MELISSA, climate as melissa
from homeassistant.components.melissa.climate import MelissaClimate
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS
from tests.common import load_fixture, mock_coro_func
from tests.async_mock import AsyncMock
from tests.common import load_fixture
_SERIAL = "12345678"
@@ -24,17 +25,17 @@ _SERIAL = "12345678"
def melissa_mock():
"""Use this to mock the melissa api."""
api = Mock()
api.async_fetch_devices = mock_coro_func(
api.async_fetch_devices = AsyncMock(
return_value=json.loads(load_fixture("melissa_fetch_devices.json"))
)
api.async_status = mock_coro_func(
api.async_status = AsyncMock(
return_value=json.loads(load_fixture("melissa_status.json"))
)
api.async_cur_settings = mock_coro_func(
api.async_cur_settings = AsyncMock(
return_value=json.loads(load_fixture("melissa_cur_settings.json"))
)
api.async_send = mock_coro_func(return_value=True)
api.async_send = AsyncMock(return_value=True)
api.STATE_OFF = 0
api.STATE_ON = 1
@@ -276,7 +277,7 @@ async def test_send(hass):
await thermostat.async_send({"fan": api.FAN_MEDIUM})
await hass.async_block_till_done()
assert SPEED_MEDIUM == thermostat.fan_mode
api.async_send.return_value = mock_coro_func(return_value=False)
api.async_send.return_value = AsyncMock(return_value=False)
thermostat._cur_settings = None
await thermostat.async_send({"fan": api.FAN_LOW})
await hass.async_block_till_done()
@@ -296,7 +297,7 @@ async def test_update(hass):
await thermostat.async_update()
assert SPEED_LOW == thermostat.fan_mode
assert HVAC_MODE_HEAT == thermostat.state
api.async_status = mock_coro_func(exception=KeyError("boom"))
api.async_status = AsyncMock(side_effect=KeyError("boom"))
await thermostat.async_update()
mocked_warning.assert_called_once_with(
"Unable to update entity %s", thermostat.entity_id
+7 -8
View File
@@ -1,23 +1,22 @@
"""The test for the Melissa Climate component."""
from homeassistant.components import melissa
from tests.common import MockDependency, mock_coro_func
from tests.async_mock import AsyncMock, patch
VALID_CONFIG = {"melissa": {"username": "********", "password": "********"}}
async def test_setup(hass):
"""Test setting up the Melissa component."""
with MockDependency("melissa") as mocked_melissa:
melissa.melissa = mocked_melissa
mocked_melissa.AsyncMelissa().async_connect = mock_coro_func()
with patch("melissa.AsyncMelissa") as mocked_melissa, patch.object(
melissa, "async_load_platform"
):
mocked_melissa.return_value.async_connect = AsyncMock()
await melissa.async_setup(hass, VALID_CONFIG)
mocked_melissa.AsyncMelissa.assert_called_with(
username="********", password="********"
)
mocked_melissa.assert_called_with(username="********", password="********")
assert melissa.DATA_MELISSA in hass.data
assert isinstance(
hass.data[melissa.DATA_MELISSA], type(mocked_melissa.AsyncMelissa())
hass.data[melissa.DATA_MELISSA], type(mocked_melissa.return_value),
)