Correct type hints on MQTT tests (#128299)

This commit is contained in:
Jan Bouwhuis
2024-10-16 13:49:18 +02:00
committed by GitHub
parent 9f2bdca9ad
commit ac6d893758
23 changed files with 127 additions and 120 deletions

View File

@ -99,7 +99,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers.json import json_dumps
from homeassistant.util.json import JsonValueType, json_loads
from homeassistant.util.json import json_loads
from .test_common import (
help_custom_config,
@ -172,11 +172,11 @@ COLOR_MODES_CONFIG = {
class JsonValidator:
"""Helper to compare JSON."""
def __init__(self, jsondata: JsonValueType) -> None:
def __init__(self, jsondata: bytes | str) -> None:
"""Initialize JSON validator."""
self.jsondata = jsondata
def __eq__(self, other: JsonValueType) -> bool:
def __eq__(self, other: bytes | str) -> bool: # type:ignore[override]
"""Compare JSON data."""
return json_loads(self.jsondata) == json_loads(other)
@ -1108,7 +1108,7 @@ async def test_sending_mqtt_commands_and_optimistic(
mqtt_mock.reset_mock()
await common.async_turn_on(
hass, "light.test", brightness=50, xy_color=[0.123, 0.123]
hass, "light.test", brightness=50, xy_color=(0.123, 0.123)
)
mqtt_mock.async_publish.assert_called_once_with(
"test_light_rgb/set",
@ -1128,7 +1128,7 @@ async def test_sending_mqtt_commands_and_optimistic(
assert state.attributes["rgb_color"] == (0, 123, 255)
assert state.attributes["xy_color"] == (0.14, 0.131)
await common.async_turn_on(hass, "light.test", brightness=50, hs_color=[359, 78])
await common.async_turn_on(hass, "light.test", brightness=50, hs_color=(359, 78))
mqtt_mock.async_publish.assert_called_once_with(
"test_light_rgb/set",
JsonValidator(
@ -1148,7 +1148,7 @@ async def test_sending_mqtt_commands_and_optimistic(
assert state.attributes["rgb_color"] == (255, 56, 59)
assert state.attributes["xy_color"] == (0.654, 0.301)
await common.async_turn_on(hass, "light.test", rgb_color=[255, 128, 0])
await common.async_turn_on(hass, "light.test", rgb_color=(255, 128, 0))
mqtt_mock.async_publish.assert_called_once_with(
"test_light_rgb/set",
JsonValidator(
@ -1265,7 +1265,7 @@ async def test_sending_mqtt_commands_and_optimistic2(
assert state.state == STATE_OFF
# Set hs color
await common.async_turn_on(hass, "light.test", brightness=75, hs_color=[359, 78])
await common.async_turn_on(hass, "light.test", brightness=75, hs_color=(359, 78))
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes["brightness"] == 75
@ -1286,7 +1286,7 @@ async def test_sending_mqtt_commands_and_optimistic2(
mqtt_mock.async_publish.reset_mock()
# Set rgb color
await common.async_turn_on(hass, "light.test", rgb_color=[255, 128, 0])
await common.async_turn_on(hass, "light.test", rgb_color=(255, 128, 0))
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes["brightness"] == 75
@ -1305,7 +1305,7 @@ async def test_sending_mqtt_commands_and_optimistic2(
mqtt_mock.async_publish.reset_mock()
# Set rgbw color
await common.async_turn_on(hass, "light.test", rgbw_color=[255, 128, 0, 123])
await common.async_turn_on(hass, "light.test", rgbw_color=(255, 128, 0, 123))
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes["brightness"] == 75
@ -1326,7 +1326,7 @@ async def test_sending_mqtt_commands_and_optimistic2(
mqtt_mock.async_publish.reset_mock()
# Set rgbww color
await common.async_turn_on(hass, "light.test", rgbww_color=[255, 128, 0, 45, 32])
await common.async_turn_on(hass, "light.test", rgbww_color=(255, 128, 0, 45, 32))
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes["brightness"] == 75
@ -1348,7 +1348,7 @@ async def test_sending_mqtt_commands_and_optimistic2(
# Set xy color
await common.async_turn_on(
hass, "light.test", brightness=50, xy_color=[0.123, 0.223]
hass, "light.test", brightness=50, xy_color=(0.123, 0.223)
)
state = hass.states.get("light.test")
assert state.state == STATE_ON
@ -1435,10 +1435,10 @@ async def test_sending_hs_color(
mqtt_mock.reset_mock()
await common.async_turn_on(
hass, "light.test", brightness=50, xy_color=[0.123, 0.123]
hass, "light.test", brightness=50, xy_color=(0.123, 0.123)
)
await common.async_turn_on(hass, "light.test", brightness=50, hs_color=[359, 78])
await common.async_turn_on(hass, "light.test", rgb_color=[255, 128, 0])
await common.async_turn_on(hass, "light.test", brightness=50, hs_color=(359, 78))
await common.async_turn_on(hass, "light.test", rgb_color=(255, 128, 0))
mqtt_mock.async_publish.assert_has_calls(
[
@ -1497,11 +1497,11 @@ async def test_sending_rgb_color_no_brightness(
assert state.state == STATE_UNKNOWN
await common.async_turn_on(
hass, "light.test", brightness=50, xy_color=[0.123, 0.123]
hass, "light.test", brightness=50, xy_color=(0.123, 0.123)
)
await common.async_turn_on(hass, "light.test", brightness=50, hs_color=[359, 78])
await common.async_turn_on(hass, "light.test", brightness=50, hs_color=(359, 78))
await common.async_turn_on(
hass, "light.test", rgb_color=[255, 128, 0], brightness=255
hass, "light.test", rgb_color=(255, 128, 0), brightness=255
)
mqtt_mock.async_publish.assert_has_calls(
@ -1555,17 +1555,17 @@ async def test_sending_rgb_color_no_brightness2(
assert state.state == STATE_UNKNOWN
await common.async_turn_on(
hass, "light.test", brightness=50, xy_color=[0.123, 0.123]
hass, "light.test", brightness=50, xy_color=(0.123, 0.123)
)
await common.async_turn_on(hass, "light.test", brightness=50, hs_color=[359, 78])
await common.async_turn_on(hass, "light.test", brightness=50, hs_color=(359, 78))
await common.async_turn_on(
hass, "light.test", rgb_color=[255, 128, 0], brightness=255
hass, "light.test", rgb_color=(255, 128, 0), brightness=255
)
await common.async_turn_on(
hass, "light.test", rgbw_color=[128, 64, 32, 16], brightness=128
hass, "light.test", rgbw_color=(128, 64, 32, 16), brightness=128
)
await common.async_turn_on(
hass, "light.test", rgbww_color=[128, 64, 32, 16, 8], brightness=64
hass, "light.test", rgbww_color=(128, 64, 32, 16, 8), brightness=64
)
mqtt_mock.async_publish.assert_has_calls(
@ -1635,11 +1635,11 @@ async def test_sending_rgb_color_with_brightness(
assert state.state == STATE_UNKNOWN
await common.async_turn_on(
hass, "light.test", brightness=50, xy_color=[0.123, 0.123]
hass, "light.test", brightness=50, xy_color=(0.123, 0.123)
)
await common.async_turn_on(hass, "light.test", brightness=255, hs_color=[359, 78])
await common.async_turn_on(hass, "light.test", brightness=255, hs_color=(359, 78))
await common.async_turn_on(hass, "light.test", brightness=1)
await common.async_turn_on(hass, "light.test", rgb_color=[255, 128, 0])
await common.async_turn_on(hass, "light.test", rgb_color=(255, 128, 0))
mqtt_mock.async_publish.assert_has_calls(
[
@ -1705,11 +1705,11 @@ async def test_sending_rgb_color_with_scaled_brightness(
assert state.state == STATE_UNKNOWN
await common.async_turn_on(
hass, "light.test", brightness=50, xy_color=[0.123, 0.123]
hass, "light.test", brightness=50, xy_color=(0.123, 0.123)
)
await common.async_turn_on(hass, "light.test", brightness=255, hs_color=[359, 78])
await common.async_turn_on(hass, "light.test", brightness=255, hs_color=(359, 78))
await common.async_turn_on(hass, "light.test", brightness=1)
await common.async_turn_on(hass, "light.test", rgb_color=[255, 128, 0])
await common.async_turn_on(hass, "light.test", rgb_color=(255, 128, 0))
mqtt_mock.async_publish.assert_has_calls(
[
@ -1820,10 +1820,10 @@ async def test_sending_xy_color(
assert state.state == STATE_UNKNOWN
await common.async_turn_on(
hass, "light.test", brightness=50, xy_color=[0.123, 0.123]
hass, "light.test", brightness=50, xy_color=(0.123, 0.123)
)
await common.async_turn_on(hass, "light.test", brightness=50, hs_color=[359, 78])
await common.async_turn_on(hass, "light.test", rgb_color=[255, 128, 0])
await common.async_turn_on(hass, "light.test", brightness=50, hs_color=(359, 78))
await common.async_turn_on(hass, "light.test", rgb_color=(255, 128, 0))
mqtt_mock.async_publish.assert_has_calls(
[
@ -2629,7 +2629,7 @@ async def test_publishing_with_custom_encoding(
) -> None:
"""Test publishing MQTT payload with different encoding."""
domain = light.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG)
config: dict[str, Any] = copy.deepcopy(DEFAULT_CONFIG)
if topic == "effect_command_topic":
config[mqtt.DOMAIN][domain]["effect_list"] = ["random", "color_loop"]
@ -2680,7 +2680,7 @@ async def test_encoding_subscribable_topics(
init_payload: tuple[str, str] | None,
) -> None:
"""Test handling of incoming encoded payload."""
config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][light.DOMAIN])
config: dict[str, Any] = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][light.DOMAIN])
config["color_mode"] = True
config["supported_color_modes"] = [
"color_temp",