Fix tests for Python 3.8 (#34672)

This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-04-25 14:32:55 -07:00
1 parent d53cb951e7
commit 0d700f6a63
40 files changed
+248 -285

No files matched your search

+24 -26
View File
@@ -3,8 +3,8 @@ from datetime import datetime, timedelta
import json
import ssl
import unittest
from unittest import mock
from asynctest import CoroutineMock, MagicMock, call, mock_open, patch
import pytest
import voluptuous as vol
@@ -31,7 +31,6 @@ from tests.common import (
async_mock_mqtt_component,
fire_mqtt_message,
get_test_home_assistant,
mock_coro,
mock_device_registry,
mock_mqtt_component,
mock_registry,
@@ -56,9 +55,9 @@ def entity_reg(hass):
@pytest.fixture
def mock_mqtt():
"""Make sure connection is established."""
with mock.patch("homeassistant.components.mqtt.MQTT") as mock_mqtt:
mock_mqtt.return_value.async_connect.return_value = mock_coro(True)
mock_mqtt.return_value.async_disconnect.return_value = mock_coro(True)
with patch("homeassistant.components.mqtt.MQTT") as mock_mqtt:
mock_mqtt.return_value.async_connect = CoroutineMock(return_value=True)
mock_mqtt.return_value.async_disconnect = CoroutineMock(return_value=True)
yield mock_mqtt
@@ -67,7 +66,7 @@ async def async_mock_mqtt_client(hass, config=None):
if config is None:
config = {mqtt.CONF_BROKER: "mock-broker"}
with mock.patch("paho.mqtt.client.Client") as mock_client:
with patch("paho.mqtt.client.Client") as mock_client:
mock_client().connect.return_value = 0
mock_client().subscribe.return_value = (0, 0)
mock_client().unsubscribe.return_value = (0, 0)
@@ -583,12 +582,12 @@ class TestMQTTCallbacks(unittest.TestCase):
# Fake that the client is connected
self.hass.data["mqtt"].connected = True
calls_a = mock.MagicMock()
calls_a = MagicMock()
mqtt.subscribe(self.hass, "test/state", calls_a)
self.hass.block_till_done()
assert calls_a.called
calls_b = mock.MagicMock()
calls_b = MagicMock()
mqtt.subscribe(self.hass, "test/state", calls_b)
self.hass.block_till_done()
assert calls_b.called
@@ -639,9 +638,9 @@ class TestMQTTCallbacks(unittest.TestCase):
self.hass.block_till_done()
expected = [
mock.call("test/state", 2),
mock.call("test/state", 0),
mock.call("test/state", 1),
call("test/state", 2),
call("test/state", 0),
call("test/state", 1),
]
assert self.hass.data["mqtt"]._mqttc.subscribe.mock_calls == expected
@@ -653,7 +652,7 @@ class TestMQTTCallbacks(unittest.TestCase):
self.hass.data["mqtt"]._mqtt_on_connect(None, None, None, 0)
self.hass.block_till_done()
expected.append(mock.call("test/state", 1))
expected.append(call("test/state", 1))
assert self.hass.data["mqtt"]._mqttc.subscribe.mock_calls == expected
@@ -661,9 +660,9 @@ async def test_setup_embedded_starts_with_no_config(hass):
"""Test setting up embedded server with no config."""
client_config = ("localhost", 1883, "user", "pass", None, "3.1.1")
with mock.patch(
with patch(
"homeassistant.components.mqtt.server.async_start",
return_value=mock_coro(return_value=(True, client_config)),
return_value=(True, client_config),
) as _start:
await async_mock_mqtt_client(hass, {})
assert _start.call_count == 1
@@ -673,11 +672,10 @@ async def test_setup_embedded_with_embedded(hass):
"""Test setting up embedded server with no config."""
client_config = ("localhost", 1883, "user", "pass", None, "3.1.1")
with mock.patch(
with patch(
"homeassistant.components.mqtt.server.async_start",
return_value=mock_coro(return_value=(True, client_config)),
return_value=(True, client_config),
) as _start:
_start.return_value = mock_coro(return_value=(True, client_config))
await async_mock_mqtt_client(hass, {"embedded": None})
assert _start.call_count == 1
@@ -686,7 +684,7 @@ async def test_setup_fails_if_no_connect_broker(hass):
"""Test for setup failure if connection to broker is missing."""
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"})
with mock.patch("paho.mqtt.client.Client") as mock_client:
with patch("paho.mqtt.client.Client") as mock_client:
mock_client().connect = lambda *args: 1
assert not await mqtt.async_setup_entry(hass, entry)
@@ -695,8 +693,8 @@ async def test_setup_raises_ConfigEntryNotReady_if_no_connect_broker(hass):
"""Test for setup failure if connection to broker is missing."""
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"})
with mock.patch("paho.mqtt.client.Client") as mock_client:
mock_client().connect = mock.Mock(side_effect=OSError("Connection error"))
with patch("paho.mqtt.client.Client") as mock_client:
mock_client().connect = MagicMock(side_effect=OSError("Connection error"))
with pytest.raises(ConfigEntryNotReady):
await mqtt.async_setup_entry(hass, entry)
@@ -808,7 +806,7 @@ async def test_mqtt_subscribes_topics_on_connect(hass):
mqtt.Subscription("still/pending", None, 1),
]
hass.add_job = mock.MagicMock()
hass.add_job = MagicMock()
hass.data["mqtt"]._mqtt_on_connect(None, None, 0, 0)
await hass.async_block_till_done()
@@ -874,7 +872,7 @@ async def test_dump_service(hass):
"""Test that we can dump a topic."""
await async_mock_mqtt_component(hass)
mock_open = mock.mock_open()
mopen = mock_open()
await hass.services.async_call(
"mqtt", "dump", {"topic": "bla/#", "duration": 3}, blocking=True
@@ -882,11 +880,11 @@ async def test_dump_service(hass):
async_fire_mqtt_message(hass, "bla/1", "test1")
async_fire_mqtt_message(hass, "bla/2", "test2")
with mock.patch("homeassistant.components.mqtt.open", mock_open):
with patch("homeassistant.components.mqtt.open", mopen):
async_fire_time_changed(hass, utcnow() + timedelta(seconds=3))
await hass.async_block_till_done()
writes = mock_open.return_value.write.mock_calls
writes = mopen.return_value.write.mock_calls
assert len(writes) == 2
assert writes[0][1][0] == "bla/1,test1\n"
assert writes[1][1][0] == "bla/2,test2\n"
@@ -1251,7 +1249,7 @@ async def test_debug_info_wildcard(hass, mqtt_mock):
]
start_dt = datetime(2019, 1, 1, 0, 0, 0)
with mock.patch("homeassistant.util.dt.utcnow") as dt_utcnow:
with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
dt_utcnow.return_value = start_dt
async_fire_mqtt_message(hass, "sensor/abc", "123")
@@ -1293,7 +1291,7 @@ async def test_debug_info_filter_same(hass, mqtt_mock):
dt1 = datetime(2019, 1, 1, 0, 0, 0)
dt2 = datetime(2019, 1, 1, 0, 0, 1)
with mock.patch("homeassistant.util.dt.utcnow") as dt_utcnow:
with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
dt_utcnow.return_value = dt1
async_fire_mqtt_message(hass, "sensor/abc", "123")
async_fire_mqtt_message(hass, "sensor/abc", "123")
+24 -26
View File
@@ -153,8 +153,7 @@ light:
payload_off: "off"
"""
from unittest import mock
from unittest.mock import patch
from asynctest import call, patch
from homeassistant.components import light, mqtt
from homeassistant.components.mqtt.discovery import async_start
@@ -188,7 +187,6 @@ from tests.common import (
MockConfigEntry,
assert_setup_component,
async_fire_mqtt_message,
mock_coro,
)
from tests.components.light import common
@@ -673,7 +671,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
)
with patch(
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
return_value=mock_coro(fake_state),
return_value=fake_state,
):
with assert_setup_component(1, light.DOMAIN):
assert await async_setup_component(hass, light.DOMAIN, config)
@@ -716,12 +714,12 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
mqtt_mock.async_publish.assert_has_calls(
[
mock.call("test_light_rgb/set", "on", 2, False),
mock.call("test_light_rgb/rgb/set", "255,128,0", 2, False),
mock.call("test_light_rgb/brightness/set", 50, 2, False),
mock.call("test_light_rgb/hs/set", "359.0,78.0", 2, False),
mock.call("test_light_rgb/white_value/set", 80, 2, False),
mock.call("test_light_rgb/xy/set", "0.14,0.131", 2, False),
call("test_light_rgb/set", "on", 2, False),
call("test_light_rgb/rgb/set", "255,128,0", 2, False),
call("test_light_rgb/brightness/set", 50, 2, False),
call("test_light_rgb/hs/set", "359.0,78.0", 2, False),
call("test_light_rgb/white_value/set", 80, 2, False),
call("test_light_rgb/xy/set", "0.14,0.131", 2, False),
],
any_order=True,
)
@@ -760,8 +758,8 @@ async def test_sending_mqtt_rgb_command_with_template(hass, mqtt_mock):
mqtt_mock.async_publish.assert_has_calls(
[
mock.call("test_light_rgb/set", "on", 0, False),
mock.call("test_light_rgb/rgb/set", "#ff803f", 0, False),
call("test_light_rgb/set", "on", 0, False),
call("test_light_rgb/rgb/set", "#ff803f", 0, False),
],
any_order=True,
)
@@ -795,8 +793,8 @@ async def test_sending_mqtt_color_temp_command_with_template(hass, mqtt_mock):
mqtt_mock.async_publish.assert_has_calls(
[
mock.call("test_light_color_temp/set", "on", 0, False),
mock.call("test_light_color_temp/color_temp/set", "10", 0, False),
call("test_light_color_temp/set", "on", 0, False),
call("test_light_color_temp/color_temp/set", "10", 0, False),
],
any_order=True,
)
@@ -980,8 +978,8 @@ async def test_on_command_first(hass, mqtt_mock):
# test_light/bright: 50
mqtt_mock.async_publish.assert_has_calls(
[
mock.call("test_light/set", "ON", 0, False),
mock.call("test_light/bright", 50, 0, False),
call("test_light/set", "ON", 0, False),
call("test_light/bright", 50, 0, False),
],
any_order=True,
)
@@ -1015,8 +1013,8 @@ async def test_on_command_last(hass, mqtt_mock):
# test_light/set: 'ON'
mqtt_mock.async_publish.assert_has_calls(
[
mock.call("test_light/bright", 50, 0, False),
mock.call("test_light/set", "ON", 0, False),
call("test_light/bright", 50, 0, False),
call("test_light/set", "ON", 0, False),
],
any_order=True,
)
@@ -1072,8 +1070,8 @@ async def test_on_command_brightness(hass, mqtt_mock):
mqtt_mock.async_publish.assert_has_calls(
[
mock.call("test_light/rgb", "255,128,0", 0, False),
mock.call("test_light/bright", 50, 0, False),
call("test_light/rgb", "255,128,0", 0, False),
call("test_light/bright", 50, 0, False),
],
any_order=True,
)
@@ -1102,8 +1100,8 @@ async def test_on_command_rgb(hass, mqtt_mock):
# test_light/set: 'ON'
mqtt_mock.async_publish.assert_has_calls(
[
mock.call("test_light/rgb", "127,127,127", 0, False),
mock.call("test_light/set", "ON", 0, False),
call("test_light/rgb", "127,127,127", 0, False),
call("test_light/set", "ON", 0, False),
],
any_order=True,
)
@@ -1138,8 +1136,8 @@ async def test_on_command_rgb_template(hass, mqtt_mock):
# test_light/set: 'ON'
mqtt_mock.async_publish.assert_has_calls(
[
mock.call("test_light/rgb", "127/127/127", 0, False),
mock.call("test_light/set", "ON", 0, False),
call("test_light/rgb", "127/127/127", 0, False),
call("test_light/set", "ON", 0, False),
],
any_order=True,
)
@@ -1174,8 +1172,8 @@ async def test_effect(hass, mqtt_mock):
# test_light/set: 'ON'
mqtt_mock.async_publish.assert_has_calls(
[
mock.call("test_light/effect/set", "rainbow", 0, False),
mock.call("test_light/set", "ON", 0, False),
call("test_light/effect/set", "rainbow", 0, False),
call("test_light/set", "ON", 0, False),
],
any_order=True,
)
+19 -19
View File
@@ -88,8 +88,8 @@ light:
brightness_scale: 99
"""
import json
from unittest import mock
from unittest.mock import patch
from asynctest import call, patch
from homeassistant.components import light
from homeassistant.const import (
@@ -123,7 +123,7 @@ from .test_common import (
help_test_update_with_json_attrs_not_dict,
)
from tests.common import async_fire_mqtt_message, mock_coro
from tests.common import async_fire_mqtt_message
from tests.components.light import common
DEFAULT_CONFIG = {
@@ -323,7 +323,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
with patch(
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
return_value=mock_coro(fake_state),
return_value=fake_state,
):
assert await async_setup_component(
hass,
@@ -397,7 +397,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
mqtt_mock.async_publish.assert_has_calls(
[
mock.call(
call(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"r": 0, "g": 123, "b": 255,'
@@ -407,7 +407,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
2,
False,
),
mock.call(
call(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"r": 255, "g": 56, "b": 59,'
@@ -417,7 +417,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
2,
False,
),
mock.call(
call(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"r": 255, "g": 128, "b": 0,'
@@ -471,7 +471,7 @@ async def test_sending_hs_color(hass, mqtt_mock):
mqtt_mock.async_publish.assert_has_calls(
[
mock.call(
call(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"h": 210.824, "s": 100.0},'
@@ -480,7 +480,7 @@ async def test_sending_hs_color(hass, mqtt_mock):
0,
False,
),
mock.call(
call(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"h": 359.0, "s": 78.0},'
@@ -489,7 +489,7 @@ async def test_sending_hs_color(hass, mqtt_mock):
0,
False,
),
mock.call(
call(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"h": 30.118, "s": 100.0},'
@@ -532,19 +532,19 @@ async def test_sending_rgb_color_no_brightness(hass, mqtt_mock):
mqtt_mock.async_publish.assert_has_calls(
[
mock.call(
call(
"test_light_rgb/set",
JsonValidator('{"state": "ON", "color": {"r": 0, "g": 24, "b": 50}}'),
0,
False,
),
mock.call(
call(
"test_light_rgb/set",
JsonValidator('{"state": "ON", "color": {"r": 50, "g": 11, "b": 11}}'),
0,
False,
),
mock.call(
call(
"test_light_rgb/set",
JsonValidator('{"state": "ON", "color": {"r": 255, "g": 128, "b": 0}}'),
0,
@@ -585,7 +585,7 @@ async def test_sending_rgb_color_with_brightness(hass, mqtt_mock):
mqtt_mock.async_publish.assert_has_calls(
[
mock.call(
call(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"r": 0, "g": 123, "b": 255},'
@@ -594,7 +594,7 @@ async def test_sending_rgb_color_with_brightness(hass, mqtt_mock):
0,
False,
),
mock.call(
call(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"r": 255, "g": 56, "b": 59},'
@@ -603,7 +603,7 @@ async def test_sending_rgb_color_with_brightness(hass, mqtt_mock):
0,
False,
),
mock.call(
call(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"r": 255, "g": 128, "b": 0},'
@@ -647,7 +647,7 @@ async def test_sending_xy_color(hass, mqtt_mock):
mqtt_mock.async_publish.assert_has_calls(
[
mock.call(
call(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"x": 0.14, "y": 0.131},'
@@ -656,7 +656,7 @@ async def test_sending_xy_color(hass, mqtt_mock):
0,
False,
),
mock.call(
call(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"x": 0.654, "y": 0.301},'
@@ -665,7 +665,7 @@ async def test_sending_xy_color(hass, mqtt_mock):
0,
False,
),
mock.call(
call(
"test_light_rgb/set",
JsonValidator(
'{"state": "ON", "color": {"x": 0.611, "y": 0.375},'
+3 -3
View File
@@ -26,7 +26,7 @@ If your light doesn't support white value feature, omit `white_value_template`.
If your light doesn't support RGB feature, omit `(red|green|blue)_template`.
"""
from unittest.mock import patch
from asynctest import patch
from homeassistant.components import light
from homeassistant.const import (
@@ -60,7 +60,7 @@ from .test_common import (
help_test_update_with_json_attrs_not_dict,
)
from tests.common import assert_setup_component, async_fire_mqtt_message, mock_coro
from tests.common import assert_setup_component, async_fire_mqtt_message
from tests.components.light import common
DEFAULT_CONFIG = {
@@ -287,7 +287,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
with patch(
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
return_value=mock_coro(fake_state),
return_value=fake_state,
):
with assert_setup_component(1, light.DOMAIN):
assert await async_setup_component(