core/tests/components/zwave_js/test_fan.py

173 lines
4.7 KiB
Python

"""Test the Z-Wave JS fan platform."""
import pytest
from zwave_js_server.event import Event
from homeassistant.components.fan import ATTR_SPEED, SPEED_MEDIUM
FAN_ENTITY = "fan.in_wall_smart_fan_control_current_value"
async def test_fan(hass, client, in_wall_smart_fan_control, integration):
"""Test the fan entity."""
node = in_wall_smart_fan_control
state = hass.states.get(FAN_ENTITY)
assert state
assert state.state == "off"
# Test turn on setting speed
await hass.services.async_call(
"fan",
"turn_on",
{"entity_id": FAN_ENTITY, "speed": SPEED_MEDIUM},
blocking=True,
)
assert len(client.async_send_command.call_args_list) == 1
args = client.async_send_command.call_args[0][0]
assert args["command"] == "node.set_value"
assert args["nodeId"] == 17
assert args["valueId"] == {
"commandClassName": "Multilevel Switch",
"commandClass": 38,
"endpoint": 0,
"property": "targetValue",
"propertyName": "targetValue",
"metadata": {
"label": "Target value",
"max": 99,
"min": 0,
"type": "number",
"readable": True,
"writeable": True,
"label": "Target value",
},
}
assert args["value"] == 66
client.async_send_command.reset_mock()
# Test setting unknown speed
with pytest.raises(ValueError):
await hass.services.async_call(
"fan",
"set_speed",
{"entity_id": FAN_ENTITY, "speed": 99},
blocking=True,
)
client.async_send_command.reset_mock()
# Test turn on no speed
await hass.services.async_call(
"fan",
"turn_on",
{"entity_id": FAN_ENTITY},
blocking=True,
)
assert len(client.async_send_command.call_args_list) == 1
args = client.async_send_command.call_args[0][0]
assert args["command"] == "node.set_value"
assert args["nodeId"] == 17
assert args["valueId"] == {
"commandClassName": "Multilevel Switch",
"commandClass": 38,
"endpoint": 0,
"property": "targetValue",
"propertyName": "targetValue",
"metadata": {
"label": "Target value",
"max": 99,
"min": 0,
"type": "number",
"readable": True,
"writeable": True,
"label": "Target value",
},
}
assert args["value"] == 255
client.async_send_command.reset_mock()
# Test turning off
await hass.services.async_call(
"fan",
"turn_off",
{"entity_id": FAN_ENTITY},
blocking=True,
)
assert len(client.async_send_command.call_args_list) == 1
args = client.async_send_command.call_args[0][0]
assert args["command"] == "node.set_value"
assert args["nodeId"] == 17
assert args["valueId"] == {
"commandClassName": "Multilevel Switch",
"commandClass": 38,
"endpoint": 0,
"property": "targetValue",
"propertyName": "targetValue",
"metadata": {
"label": "Target value",
"max": 99,
"min": 0,
"type": "number",
"readable": True,
"writeable": True,
"label": "Target value",
},
}
assert args["value"] == 0
client.async_send_command.reset_mock()
# Test speed update from value updated event
event = Event(
type="value updated",
data={
"source": "node",
"event": "value updated",
"nodeId": 17,
"args": {
"commandClassName": "Multilevel Switch",
"commandClass": 38,
"endpoint": 0,
"property": "currentValue",
"newValue": 99,
"prevValue": 0,
"propertyName": "currentValue",
},
},
)
node.receive_event(event)
state = hass.states.get(FAN_ENTITY)
assert state.state == "on"
assert state.attributes[ATTR_SPEED] == "high"
client.async_send_command.reset_mock()
event = Event(
type="value updated",
data={
"source": "node",
"event": "value updated",
"nodeId": 17,
"args": {
"commandClassName": "Multilevel Switch",
"commandClass": 38,
"endpoint": 0,
"property": "currentValue",
"newValue": 0,
"prevValue": 0,
"propertyName": "currentValue",
},
},
)
node.receive_event(event)
state = hass.states.get(FAN_ENTITY)
assert state.state == "off"
assert state.attributes[ATTR_SPEED] == "off"