Add fan platform to ozw component (#35249)
* Add fan platform * Add fan discovery schema * Use constants for dispatcher signal * Move fan platform to ozw * Fix fan discovery schema * Add previous speed to handle value 255 * Make fixture reading more robust * Add fan tests * Remove not needed fixture info * Validate speed Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
1 parent
59c112a3f2
commit
ca1e643bdc
8 files changed
+341
-1
No files matched your search
@@ -32,7 +32,10 @@ async def setup_ozw(hass, entry=None, fixture=None):
|
||||
|
||||
if fixture is not None:
|
||||
for line in fixture.split("\n"):
|
||||
topic, payload = line.strip().split(",", 1)
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
topic, payload = line.split(",", 1)
|
||||
receive_message(Mock(topic=topic, payload=payload))
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -15,6 +15,12 @@ def generic_data_fixture():
|
||||
return load_fixture("ozw/generic_network_dump.csv")
|
||||
|
||||
|
||||
@pytest.fixture(name="fan_data", scope="session")
|
||||
def fan_data_fixture():
|
||||
"""Load fan MQTT data and return it."""
|
||||
return load_fixture("ozw/fan_network_dump.csv")
|
||||
|
||||
|
||||
@pytest.fixture(name="light_data", scope="session")
|
||||
def light_data_fixture():
|
||||
"""Load light dimmer MQTT data and return it."""
|
||||
@@ -47,6 +53,17 @@ def sent_messages_fixture():
|
||||
yield sent_messages
|
||||
|
||||
|
||||
@pytest.fixture(name="fan_msg")
|
||||
async def fan_msg_fixture(hass):
|
||||
"""Return a mock MQTT msg with a fan actuator message."""
|
||||
fan_json = json.loads(
|
||||
await hass.async_add_executor_job(load_fixture, "ozw/fan.json")
|
||||
)
|
||||
message = MQTTMessage(topic=fan_json["topic"], payload=fan_json["payload"])
|
||||
message.encode()
|
||||
return message
|
||||
|
||||
|
||||
@pytest.fixture(name="light_msg")
|
||||
async def light_msg_fixture(hass):
|
||||
"""Return a mock MQTT msg with a light actuator message."""
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
"""Test Z-Wave Lights."""
|
||||
from homeassistant.components.ozw.fan import SPEED_TO_VALUE
|
||||
|
||||
from .common import setup_ozw
|
||||
|
||||
|
||||
async def test_fan(hass, fan_data, fan_msg, sent_messages, caplog):
|
||||
"""Test fan."""
|
||||
receive_message = await setup_ozw(hass, fixture=fan_data)
|
||||
|
||||
# Test loaded
|
||||
state = hass.states.get("fan.in_wall_smart_fan_control_level")
|
||||
assert state is not None
|
||||
assert state.state == "on"
|
||||
|
||||
# Test turning off
|
||||
await hass.services.async_call(
|
||||
"fan",
|
||||
"turn_off",
|
||||
{"entity_id": "fan.in_wall_smart_fan_control_level"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert len(sent_messages) == 1
|
||||
msg = sent_messages[-1]
|
||||
assert msg["topic"] == "OpenZWave/1/command/setvalue/"
|
||||
assert msg["payload"] == {"Value": 0, "ValueIDKey": 172589073}
|
||||
|
||||
# Feedback on state
|
||||
fan_msg.decode()
|
||||
fan_msg.payload["Value"] = 0
|
||||
fan_msg.encode()
|
||||
receive_message(fan_msg)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("fan.in_wall_smart_fan_control_level")
|
||||
assert state is not None
|
||||
assert state.state == "off"
|
||||
|
||||
# Test turning on
|
||||
new_speed = "medium"
|
||||
await hass.services.async_call(
|
||||
"fan",
|
||||
"turn_on",
|
||||
{"entity_id": "fan.in_wall_smart_fan_control_level", "speed": new_speed},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert len(sent_messages) == 2
|
||||
msg = sent_messages[-1]
|
||||
assert msg["topic"] == "OpenZWave/1/command/setvalue/"
|
||||
assert msg["payload"] == {
|
||||
"Value": SPEED_TO_VALUE[new_speed],
|
||||
"ValueIDKey": 172589073,
|
||||
}
|
||||
|
||||
# Feedback on state
|
||||
fan_msg.decode()
|
||||
fan_msg.payload["Value"] = SPEED_TO_VALUE[new_speed]
|
||||
fan_msg.encode()
|
||||
receive_message(fan_msg)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("fan.in_wall_smart_fan_control_level")
|
||||
assert state is not None
|
||||
assert state.state == "on"
|
||||
assert state.attributes["speed"] == new_speed
|
||||
|
||||
# Test turn on without speed
|
||||
await hass.services.async_call(
|
||||
"fan",
|
||||
"turn_on",
|
||||
{"entity_id": "fan.in_wall_smart_fan_control_level"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert len(sent_messages) == 3
|
||||
msg = sent_messages[-1]
|
||||
assert msg["topic"] == "OpenZWave/1/command/setvalue/"
|
||||
assert msg["payload"] == {
|
||||
"Value": 255,
|
||||
"ValueIDKey": 172589073,
|
||||
}
|
||||
|
||||
# Feedback on state
|
||||
fan_msg.decode()
|
||||
fan_msg.payload["Value"] = SPEED_TO_VALUE[new_speed]
|
||||
fan_msg.encode()
|
||||
receive_message(fan_msg)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("fan.in_wall_smart_fan_control_level")
|
||||
assert state is not None
|
||||
assert state.state == "on"
|
||||
assert state.attributes["speed"] == new_speed
|
||||
|
||||
# Test set speed to off
|
||||
new_speed = "off"
|
||||
await hass.services.async_call(
|
||||
"fan",
|
||||
"set_speed",
|
||||
{"entity_id": "fan.in_wall_smart_fan_control_level", "speed": new_speed},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert len(sent_messages) == 4
|
||||
msg = sent_messages[-1]
|
||||
assert msg["topic"] == "OpenZWave/1/command/setvalue/"
|
||||
assert msg["payload"] == {
|
||||
"Value": SPEED_TO_VALUE[new_speed],
|
||||
"ValueIDKey": 172589073,
|
||||
}
|
||||
|
||||
# Feedback on state
|
||||
fan_msg.decode()
|
||||
fan_msg.payload["Value"] = SPEED_TO_VALUE[new_speed]
|
||||
fan_msg.encode()
|
||||
receive_message(fan_msg)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("fan.in_wall_smart_fan_control_level")
|
||||
assert state is not None
|
||||
assert state.state == "off"
|
||||
|
||||
# Test invalid speed
|
||||
new_speed = "invalid"
|
||||
await hass.services.async_call(
|
||||
"fan",
|
||||
"set_speed",
|
||||
{"entity_id": "fan.in_wall_smart_fan_control_level", "speed": new_speed},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert len(sent_messages) == 4
|
||||
assert "Invalid speed received: invalid" in caplog.text
|
||||
Reference in new issue
Block a user