Skip to content

Commit ac6d893

Browse files
authored
Correct type hints on MQTT tests (#128299)
1 parent 9f2bdca commit ac6d893

23 files changed

+127
-120
lines changed

tests/components/light/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async def async_turn_on(
9999
flash: str | None = None,
100100
effect: str | None = None,
101101
color_name: str | None = None,
102-
white: bool | None = None,
102+
white: int | None = None,
103103
) -> None:
104104
"""Turn all or specified light on."""
105105
data = {

tests/components/mqtt/test_binary_sensor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ async def test_skip_restoring_state_with_over_due_expire_trigger(
11331133

11341134
freezer.move_to("2022-02-02 12:02:00+01:00")
11351135
domain = binary_sensor.DOMAIN
1136-
config3 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][domain])
1136+
config3: ConfigType = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][domain])
11371137
config3["name"] = "test3"
11381138
config3["expire_after"] = 10
11391139
config3["state_topic"] = "test-topic3"

tests/components/mqtt/test_client.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""The tests for the MQTT client."""
22

33
import asyncio
4-
from datetime import datetime, timedelta
4+
from datetime import timedelta
55
import socket
66
import ssl
7+
import time
78
from typing import Any
89
from unittest.mock import MagicMock, Mock, call, patch
910

@@ -296,10 +297,13 @@ async def test_subscribe_mqtt_config_entry_disabled(
296297
mqtt_mock.connected = True
297298

298299
mqtt_config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
299-
assert mqtt_config_entry.state is ConfigEntryState.LOADED
300+
301+
mqtt_config_entry_state = mqtt_config_entry.state
302+
assert mqtt_config_entry_state is ConfigEntryState.LOADED
300303

301304
assert await hass.config_entries.async_unload(mqtt_config_entry.entry_id)
302-
assert mqtt_config_entry.state is ConfigEntryState.NOT_LOADED
305+
mqtt_config_entry_state = mqtt_config_entry.state
306+
assert mqtt_config_entry_state is ConfigEntryState.NOT_LOADED
303307

304308
await hass.config_entries.async_set_disabled_by(
305309
mqtt_config_entry.entry_id, ConfigEntryDisabler.USER
@@ -1279,7 +1283,7 @@ def _callback(args) -> None:
12791283
callbacks.append(args)
12801284

12811285
msg = ReceiveMessage(
1282-
"some-topic", b"test-payload", 1, False, "some-topic", datetime.now()
1286+
"some-topic", b"test-payload", 1, False, "some-topic", time.monotonic()
12831287
)
12841288
mock_debouncer.clear()
12851289
await mqtt.async_subscribe(hass, "some-topic", _callback)

tests/components/mqtt/test_climate.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ async def test_set_operation_bad_attr_and_state(
202202
state = hass.states.get(ENTITY_CLIMATE)
203203
assert state.state == "off"
204204
with pytest.raises(vol.Invalid) as excinfo:
205-
await common.async_set_hvac_mode(hass, None, ENTITY_CLIMATE)
205+
await common.async_set_hvac_mode(hass, None, ENTITY_CLIMATE) # type:ignore[arg-type]
206206
assert (
207207
"expected HVACMode or one of 'off', 'heat', 'cool', 'heat_cool', 'auto', 'dry',"
208208
" 'fan_only' for dictionary value @ data['hvac_mode']" in str(excinfo.value)
@@ -220,10 +220,9 @@ async def test_set_operation(
220220

221221
state = hass.states.get(ENTITY_CLIMATE)
222222
assert state.state == "off"
223-
await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE)
223+
await common.async_set_hvac_mode(hass, HVACMode.COOL, ENTITY_CLIMATE)
224224
state = hass.states.get(ENTITY_CLIMATE)
225225
assert state.state == "cool"
226-
assert state.state == "cool"
227226
mqtt_mock.async_publish.assert_called_once_with("mode-topic", "cool", 0, False)
228227

229228

@@ -245,7 +244,7 @@ async def test_set_operation_pessimistic(
245244
state = hass.states.get(ENTITY_CLIMATE)
246245
assert state.state == STATE_UNKNOWN
247246

248-
await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE)
247+
await common.async_set_hvac_mode(hass, HVACMode.COOL, ENTITY_CLIMATE)
249248
state = hass.states.get(ENTITY_CLIMATE)
250249
assert state.state == STATE_UNKNOWN
251250

@@ -287,7 +286,7 @@ async def test_set_operation_optimistic(
287286
state = hass.states.get(ENTITY_CLIMATE)
288287
assert state.state == "off"
289288

290-
await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE)
289+
await common.async_set_hvac_mode(hass, HVACMode.COOL, ENTITY_CLIMATE)
291290
state = hass.states.get(ENTITY_CLIMATE)
292291
assert state.state == "cool"
293292

@@ -316,13 +315,13 @@ async def test_set_operation_with_power_command(
316315

317316
state = hass.states.get(ENTITY_CLIMATE)
318317
assert state.state == "off"
319-
await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE)
318+
await common.async_set_hvac_mode(hass, HVACMode.COOL, ENTITY_CLIMATE)
320319
state = hass.states.get(ENTITY_CLIMATE)
321320
assert state.state == "cool"
322321
mqtt_mock.async_publish.assert_has_calls([call("mode-topic", "cool", 0, False)])
323322
mqtt_mock.async_publish.reset_mock()
324323

325-
await common.async_set_hvac_mode(hass, "off", ENTITY_CLIMATE)
324+
await common.async_set_hvac_mode(hass, HVACMode.OFF, ENTITY_CLIMATE)
326325
state = hass.states.get(ENTITY_CLIMATE)
327326
assert state.state == "off"
328327
mqtt_mock.async_publish.assert_has_calls([call("mode-topic", "off", 0, False)])
@@ -358,12 +357,12 @@ async def test_turn_on_and_off_optimistic_with_power_command(
358357

359358
state = hass.states.get(ENTITY_CLIMATE)
360359
assert state.state == "off"
361-
await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE)
360+
await common.async_set_hvac_mode(hass, HVACMode.COOL, ENTITY_CLIMATE)
362361
state = hass.states.get(ENTITY_CLIMATE)
363362
assert state.state == "cool"
364363
mqtt_mock.async_publish.assert_has_calls([call("mode-topic", "cool", 0, False)])
365364
mqtt_mock.async_publish.reset_mock()
366-
await common.async_set_hvac_mode(hass, "off", ENTITY_CLIMATE)
365+
await common.async_set_hvac_mode(hass, HVACMode.OFF, ENTITY_CLIMATE)
367366
state = hass.states.get(ENTITY_CLIMATE)
368367
assert state.state == "off"
369368

@@ -374,7 +373,7 @@ async def test_turn_on_and_off_optimistic_with_power_command(
374373
mqtt_mock.async_publish.assert_has_calls([call("power-command", "ON", 0, False)])
375374
mqtt_mock.async_publish.reset_mock()
376375

377-
await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE)
376+
await common.async_set_hvac_mode(hass, HVACMode.COOL, ENTITY_CLIMATE)
378377
state = hass.states.get(ENTITY_CLIMATE)
379378
assert state.state == "cool"
380379
await common.async_turn_off(hass, ENTITY_CLIMATE)
@@ -433,7 +432,7 @@ async def test_turn_on_and_off_without_power_command(
433432
else:
434433
mqtt_mock.async_publish.assert_has_calls([])
435434

436-
await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE)
435+
await common.async_set_hvac_mode(hass, HVACMode.COOL, ENTITY_CLIMATE)
437436
state = hass.states.get(ENTITY_CLIMATE)
438437
assert state.state == "cool"
439438
mqtt_mock.async_publish.reset_mock()
@@ -460,7 +459,7 @@ async def test_set_fan_mode_bad_attr(
460459
state = hass.states.get(ENTITY_CLIMATE)
461460
assert state.attributes.get("fan_mode") == "low"
462461
with pytest.raises(vol.Invalid) as excinfo:
463-
await common.async_set_fan_mode(hass, None, ENTITY_CLIMATE)
462+
await common.async_set_fan_mode(hass, None, ENTITY_CLIMATE) # type:ignore[arg-type]
464463
assert "string value is None for dictionary value @ data['fan_mode']" in str(
465464
excinfo.value
466465
)
@@ -555,7 +554,7 @@ async def test_set_swing_mode_bad_attr(
555554
state = hass.states.get(ENTITY_CLIMATE)
556555
assert state.attributes.get("swing_mode") == "off"
557556
with pytest.raises(vol.Invalid) as excinfo:
558-
await common.async_set_swing_mode(hass, None, ENTITY_CLIMATE)
557+
await common.async_set_swing_mode(hass, None, ENTITY_CLIMATE) # type:ignore[arg-type]
559558
assert "string value is None for dictionary value @ data['swing_mode']" in str(
560559
excinfo.value
561560
)
@@ -649,7 +648,7 @@ async def test_set_target_temperature(
649648

650649
state = hass.states.get(ENTITY_CLIMATE)
651650
assert state.attributes.get("temperature") == 21
652-
await common.async_set_hvac_mode(hass, "heat", ENTITY_CLIMATE)
651+
await common.async_set_hvac_mode(hass, HVACMode.HEAT, ENTITY_CLIMATE)
653652
state = hass.states.get(ENTITY_CLIMATE)
654653
assert state.state == "heat"
655654
mqtt_mock.async_publish.assert_called_once_with("mode-topic", "heat", 0, False)
@@ -712,7 +711,7 @@ async def test_set_target_temperature_pessimistic(
712711

713712
state = hass.states.get(ENTITY_CLIMATE)
714713
assert state.attributes.get("temperature") is None
715-
await common.async_set_hvac_mode(hass, "heat", ENTITY_CLIMATE)
714+
await common.async_set_hvac_mode(hass, HVACMode.HEAT, ENTITY_CLIMATE)
716715
await common.async_set_temperature(hass, temperature=35, entity_id=ENTITY_CLIMATE)
717716
state = hass.states.get(ENTITY_CLIMATE)
718717
assert state.attributes.get("temperature") is None
@@ -744,7 +743,7 @@ async def test_set_target_temperature_optimistic(
744743

745744
state = hass.states.get(ENTITY_CLIMATE)
746745
assert state.attributes.get("temperature") == 21
747-
await common.async_set_hvac_mode(hass, "heat", ENTITY_CLIMATE)
746+
await common.async_set_hvac_mode(hass, HVACMode.HEAT, ENTITY_CLIMATE)
748747
await common.async_set_temperature(hass, temperature=17, entity_id=ENTITY_CLIMATE)
749748
state = hass.states.get(ENTITY_CLIMATE)
750749
assert state.attributes.get("temperature") == 17
@@ -1547,14 +1546,14 @@ async def test_set_and_templates(
15471546
assert state.attributes.get("preset_mode") == PRESET_ECO
15481547

15491548
# Mode
1550-
await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE)
1549+
await common.async_set_hvac_mode(hass, HVACMode.COOL, ENTITY_CLIMATE)
15511550
mqtt_mock.async_publish.assert_any_call("mode-topic", "mode: cool", 0, False)
15521551
assert mqtt_mock.async_publish.call_count == 1
15531552
mqtt_mock.async_publish.reset_mock()
15541553
state = hass.states.get(ENTITY_CLIMATE)
15551554
assert state.state == "cool"
15561555

1557-
await common.async_set_hvac_mode(hass, "off", ENTITY_CLIMATE)
1556+
await common.async_set_hvac_mode(hass, HVACMode.OFF, ENTITY_CLIMATE)
15581557
mqtt_mock.async_publish.assert_any_call("mode-topic", "mode: off", 0, False)
15591558
assert mqtt_mock.async_publish.call_count == 1
15601559
mqtt_mock.async_publish.reset_mock()

tests/components/mqtt/test_common.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@
7272

7373
type _MqttMessageType = list[tuple[str, str]]
7474
type _AttributesType = list[tuple[str, Any]]
75-
type _StateDataType = list[tuple[_MqttMessageType, str | None, _AttributesType | None]]
75+
type _StateDataType = (
76+
list[tuple[_MqttMessageType, str, _AttributesType | None]]
77+
| list[tuple[_MqttMessageType, str, None]]
78+
)
7679

7780

7881
def help_all_subscribe_calls(mqtt_client_mock: MqttMockPahoClient) -> list[Any]:
@@ -106,7 +109,7 @@ def help_custom_config(
106109
)
107110
base.update(instance)
108111
entity_instances.append(base)
109-
config[mqtt.DOMAIN][mqtt_entity_domain]: list[ConfigType] = entity_instances
112+
config[mqtt.DOMAIN][mqtt_entity_domain] = entity_instances
110113
return config
111114

112115

@@ -1360,11 +1363,11 @@ async def help_test_entity_debug_info_message(
13601363
mqtt_mock_entry: MqttMockHAClientGenerator,
13611364
domain: str,
13621365
config: ConfigType,
1363-
service: str,
1366+
service: str | None,
13641367
command_topic: str | None = None,
13651368
command_payload: str | None = None,
13661369
state_topic: str | object | None = _SENTINEL,
1367-
state_payload: str | None = None,
1370+
state_payload: bytes | str | None = None,
13681371
service_parameters: dict[str, Any] | None = None,
13691372
) -> None:
13701373
"""Test debug_info.

tests/components/mqtt/test_config_flow.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,6 @@ async def test_hassio_confirm(
455455
mock_finish_setup: MagicMock,
456456
) -> None:
457457
"""Test we can finish a config flow."""
458-
mock_try_connection.return_value = True
459-
460458
result = await hass.config_entries.flow.async_init(
461459
"mqtt",
462460
data=HassioServiceInfo(
@@ -1027,7 +1025,6 @@ async def test_bad_certificate(
10271025
test_input.pop(mqtt.CONF_CLIENT_KEY)
10281026

10291027
mqtt_mock = await mqtt_mock_entry()
1030-
mock_try_connection.return_value = True
10311028
config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
10321029
# Add at least one advanced option to get the full form
10331030
hass.config_entries.async_update_entry(
@@ -1276,7 +1273,7 @@ async def test_invalid_discovery_prefix(
12761273

12771274
def get_default(schema: vol.Schema, key: str) -> Any | None:
12781275
"""Get default value for key in voluptuous schema."""
1279-
for schema_key in schema:
1276+
for schema_key in schema: # type:ignore[attr-defined]
12801277
if schema_key == key:
12811278
if schema_key.default == vol.UNDEFINED:
12821279
return None
@@ -1286,7 +1283,7 @@ def get_default(schema: vol.Schema, key: str) -> Any | None:
12861283

12871284
def get_suggested(schema: vol.Schema, key: str) -> Any | None:
12881285
"""Get suggested value for key in voluptuous schema."""
1289-
for schema_key in schema:
1286+
for schema_key in schema: # type:ignore[attr-defined]
12901287
if schema_key == key:
12911288
if (
12921289
schema_key.description is None

tests/components/mqtt/test_device_trigger.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async def test_get_triggers(
4545
await hass.async_block_till_done()
4646

4747
device_entry = device_registry.async_get_device(identifiers={("mqtt", "0AFFD2")})
48-
expected_triggers = [
48+
expected_triggers: list[dict[str, Any]] = [
4949
{
5050
"platform": "device",
5151
"domain": DOMAIN,
@@ -165,7 +165,7 @@ async def test_discover_bad_triggers(
165165
await hass.async_block_till_done()
166166

167167
device_entry = device_registry.async_get_device(identifiers={("mqtt", "0AFFD2")})
168-
expected_triggers = [
168+
expected_triggers: list[dict[str, Any]] = [
169169
{
170170
"platform": "device",
171171
"domain": DOMAIN,
@@ -226,7 +226,7 @@ async def test_update_remove_triggers(
226226

227227
device_entry = device_registry.async_get_device(identifiers={("mqtt", "0AFFD2")})
228228
assert device_entry.name == "milk"
229-
expected_triggers1 = [
229+
expected_triggers1: list[dict[str, Any]] = [
230230
{
231231
"platform": "device",
232232
"domain": DOMAIN,
@@ -1263,7 +1263,7 @@ async def test_entity_device_info_update(
12631263
"""Test device registry update."""
12641264
await mqtt_mock_entry()
12651265

1266-
config = {
1266+
config: dict[str, Any] = {
12671267
"automation_type": "trigger",
12681268
"topic": "test-topic",
12691269
"type": "foo",

tests/components/mqtt/test_fan.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ async def test_encoding_subscribable_topics(
14861486
attribute_value: Any,
14871487
) -> None:
14881488
"""Test handling of incoming encoded payload."""
1489-
config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][fan.DOMAIN])
1489+
config: dict[str, Any] = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][fan.DOMAIN])
14901490
config[ATTR_PRESET_MODES] = ["eco", "auto"]
14911491
config[CONF_PRESET_MODE_COMMAND_TOPIC] = "fan/some_preset_mode_command_topic"
14921492
config[CONF_PERCENTAGE_COMMAND_TOPIC] = "fan/some_percentage_command_topic"
@@ -2201,7 +2201,7 @@ async def test_publishing_with_custom_encoding(
22012201
) -> None:
22022202
"""Test publishing MQTT payload with different encoding."""
22032203
domain = fan.DOMAIN
2204-
config = copy.deepcopy(DEFAULT_CONFIG)
2204+
config: dict[str, Any] = copy.deepcopy(DEFAULT_CONFIG)
22052205
if topic == "preset_mode_command_topic":
22062206
config[mqtt.DOMAIN][domain]["preset_modes"] = ["auto", "eco"]
22072207

tests/components/mqtt/test_humidifier.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,9 @@ async def test_encoding_subscribable_topics(
862862
attribute_value: Any,
863863
) -> None:
864864
"""Test handling of incoming encoded payload."""
865-
config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][humidifier.DOMAIN])
865+
config: dict[str, Any] = copy.deepcopy(
866+
DEFAULT_CONFIG[mqtt.DOMAIN][humidifier.DOMAIN]
867+
)
866868
config["modes"] = ["eco", "auto"]
867869
config[CONF_MODE_COMMAND_TOPIC] = "humidifier/some_mode_command_topic"
868870
await help_test_encoding_subscribable_topics(
@@ -1473,7 +1475,7 @@ async def test_publishing_with_custom_encoding(
14731475
) -> None:
14741476
"""Test publishing MQTT payload with different encoding."""
14751477
domain = humidifier.DOMAIN
1476-
config = copy.deepcopy(DEFAULT_CONFIG)
1478+
config: dict[str, Any] = copy.deepcopy(DEFAULT_CONFIG)
14771479
if topic == "mode_command_topic":
14781480
config[mqtt.DOMAIN][domain]["modes"] = ["auto", "eco"]
14791481

tests/components/mqtt/test_init.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ async def test_value_template_fails(hass: HomeAssistant) -> None:
230230
)
231231
with pytest.raises(MqttValueTemplateException) as exc:
232232
val_tpl.async_render_with_possible_json_value(
233-
'{"some_var": null }', default=100
233+
'{"some_var": null }', default="100"
234234
)
235235
assert str(exc.value) == (
236236
"TypeError: unsupported operand type(s) for *: 'NoneType' and 'int' "
@@ -835,7 +835,7 @@ async def test_receiving_message_with_non_utf8_topic_gets_logged(
835835
msg.payload = b"Payload"
836836
msg.qos = 2
837837
msg.retain = True
838-
msg.timestamp = time.monotonic()
838+
msg.timestamp = time.monotonic() # type:ignore[assignment]
839839

840840
mqtt_data: MqttData = hass.data["mqtt"]
841841
assert mqtt_data.client
@@ -1489,7 +1489,7 @@ async def test_debug_info_non_mqtt(
14891489
"""Test we get empty debug_info for a device with non MQTT entities."""
14901490
await mqtt_mock_entry()
14911491
domain = "sensor"
1492-
setup_test_component_platform(hass, domain, mock_sensor_entities)
1492+
setup_test_component_platform(hass, domain, mock_sensor_entities.values())
14931493

14941494
config_entry = MockConfigEntry(domain="test", data={})
14951495
config_entry.add_to_hass(hass)

0 commit comments

Comments
 (0)