Add support for v1 and v2 HomeKit fans. (#30503)

* Add support for v1 and v2 HomeKit fans.

* Lint fix
This commit is contained in:
Jc2k authored and Paulus Schoutsen committed 2020-01-05 15:56:46 +01:00
1 parent 8625e962ce
commit fffc5a5fbb
7 files changed
+1856

No files matched your search

@@ -0,0 +1,53 @@
"""Test against characteristics captured from the Home Assistant HomeKit bridge running demo platforms."""
from homeassistant.components.fan import (
SUPPORT_DIRECTION,
SUPPORT_OSCILLATE,
SUPPORT_SET_SPEED,
)
from tests.components.homekit_controller.common import (
Helper,
setup_accessories_from_file,
setup_test_accessories,
)
async def test_homeassistant_bridge_fan_setup(hass):
"""Test that a SIMPLEconnect fan can be correctly setup in HA."""
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_fan.json"
)
config_entry, pairing = await setup_test_accessories(hass, accessories)
entity_registry = await hass.helpers.entity_registry.async_get_registry()
# Check that the fan is correctly found and set up
fan_id = "fan.living_room_fan"
fan = entity_registry.async_get(fan_id)
assert fan.unique_id == "homekit-fan.living_room_fan-8"
fan_helper = Helper(
hass, "fan.living_room_fan", pairing, accessories[0], config_entry,
)
fan_state = await fan_helper.poll_and_get_state()
assert fan_state.attributes["friendly_name"] == "Living Room Fan"
assert fan_state.state == "off"
assert fan_state.attributes["supported_features"] == (
SUPPORT_DIRECTION | SUPPORT_SET_SPEED | SUPPORT_OSCILLATE
)
device_registry = await hass.helpers.device_registry.async_get_registry()
device = device_registry.async_get(fan.device_id)
assert device.manufacturer == "Home Assistant"
assert device.name == "Living Room Fan"
assert device.model == "Fan"
assert device.sw_version == "0.104.0.dev0"
bridge = device = device_registry.async_get(device.via_device_id)
assert bridge.manufacturer == "Home Assistant"
assert bridge.name == "Home Assistant Bridge"
assert bridge.model == "Bridge"
assert bridge.sw_version == "0.104.0.dev0"
@@ -0,0 +1,46 @@
"""
Test against characteristics captured from a SIMPLEconnect Fan.
https://github.com/home-assistant/home-assistant/issues/26180
"""
from homeassistant.components.fan import SUPPORT_DIRECTION, SUPPORT_SET_SPEED
from tests.components.homekit_controller.common import (
Helper,
setup_accessories_from_file,
setup_test_accessories,
)
async def test_simpleconnect_fan_setup(hass):
"""Test that a SIMPLEconnect fan can be correctly setup in HA."""
accessories = await setup_accessories_from_file(hass, "simpleconnect_fan.json")
config_entry, pairing = await setup_test_accessories(hass, accessories)
entity_registry = await hass.helpers.entity_registry.async_get_registry()
# Check that the fan is correctly found and set up
fan_id = "fan.simpleconnect_fan_06f674"
fan = entity_registry.async_get(fan_id)
assert fan.unique_id == "homekit-1234567890abcd-8"
fan_helper = Helper(
hass, "fan.simpleconnect_fan_06f674", pairing, accessories[0], config_entry,
)
fan_state = await fan_helper.poll_and_get_state()
assert fan_state.attributes["friendly_name"] == "SIMPLEconnect Fan-06F674"
assert fan_state.state == "off"
assert fan_state.attributes["supported_features"] == (
SUPPORT_DIRECTION | SUPPORT_SET_SPEED
)
device_registry = await hass.helpers.device_registry.async_get_registry()
device = device_registry.async_get(fan.device_id)
assert device.manufacturer == "Hunter Fan"
assert device.name == "SIMPLEconnect Fan-06F674"
assert device.model == "SIMPLEconnect"
assert device.sw_version == ""
assert device.via_device_id is None
@@ -0,0 +1,407 @@
"""Basic checks for HomeKit motion sensors and contact sensors."""
from tests.components.homekit_controller.common import FakeService, setup_test_component
V1_ON = ("fan", "on")
V1_ROTATION_DIRECTION = ("fan", "rotation.direction")
V1_ROTATION_SPEED = ("fan", "rotation.speed")
V2_ACTIVE = ("fanv2", "active")
V2_ROTATION_DIRECTION = ("fanv2", "rotation.direction")
V2_ROTATION_SPEED = ("fanv2", "rotation.speed")
V2_SWING_MODE = ("fanv2", "swing-mode")
def create_fan_service():
"""
Define fan v1 characteristics as per HAP spec.
This service is no longer documented in R2 of the public HAP spec but existing
devices out there use it (like the SIMPLEconnect fan)
"""
service = FakeService("public.hap.service.fan")
cur_state = service.add_characteristic("on")
cur_state.value = 0
cur_state = service.add_characteristic("rotation.direction")
cur_state.value = 0
cur_state = service.add_characteristic("rotation.speed")
cur_state.value = 0
return service
def create_fanv2_service():
"""Define fan v2 characteristics as per HAP spec."""
service = FakeService("public.hap.service.fanv2")
cur_state = service.add_characteristic("active")
cur_state.value = 0
cur_state = service.add_characteristic("rotation.direction")
cur_state.value = 0
cur_state = service.add_characteristic("rotation.speed")
cur_state.value = 0
cur_state = service.add_characteristic("swing-mode")
cur_state.value = 0
return service
async def test_fan_read_state(hass, utcnow):
"""Test that we can read the state of a HomeKit fan accessory."""
sensor = create_fan_service()
helper = await setup_test_component(hass, [sensor])
helper.characteristics[V1_ON].value = False
state = await helper.poll_and_get_state()
assert state.state == "off"
helper.characteristics[V1_ON].value = True
state = await helper.poll_and_get_state()
assert state.state == "on"
async def test_turn_on(hass, utcnow):
"""Test that we can turn a fan on."""
fan = create_fan_service()
helper = await setup_test_component(hass, [fan])
await hass.services.async_call(
"fan",
"turn_on",
{"entity_id": "fan.testdevice", "speed": "high"},
blocking=True,
)
assert helper.characteristics[V1_ON].value == 1
assert helper.characteristics[V1_ROTATION_SPEED].value == 100
await hass.services.async_call(
"fan",
"turn_on",
{"entity_id": "fan.testdevice", "speed": "medium"},
blocking=True,
)
assert helper.characteristics[V1_ON].value == 1
assert helper.characteristics[V1_ROTATION_SPEED].value == 50
await hass.services.async_call(
"fan",
"turn_on",
{"entity_id": "fan.testdevice", "speed": "low"},
blocking=True,
)
assert helper.characteristics[V1_ON].value == 1
assert helper.characteristics[V1_ROTATION_SPEED].value == 25
async def test_turn_off(hass, utcnow):
"""Test that we can turn a fan off."""
fan = create_fan_service()
helper = await setup_test_component(hass, [fan])
helper.characteristics[V1_ON].value = 1
await hass.services.async_call(
"fan", "turn_off", {"entity_id": "fan.testdevice"}, blocking=True,
)
assert helper.characteristics[V1_ON].value == 0
async def test_set_speed(hass, utcnow):
"""Test that we set fan speed."""
fan = create_fan_service()
helper = await setup_test_component(hass, [fan])
helper.characteristics[V1_ON].value = 1
await hass.services.async_call(
"fan",
"set_speed",
{"entity_id": "fan.testdevice", "speed": "high"},
blocking=True,
)
assert helper.characteristics[V1_ROTATION_SPEED].value == 100
await hass.services.async_call(
"fan",
"set_speed",
{"entity_id": "fan.testdevice", "speed": "medium"},
blocking=True,
)
assert helper.characteristics[V1_ROTATION_SPEED].value == 50
await hass.services.async_call(
"fan",
"set_speed",
{"entity_id": "fan.testdevice", "speed": "low"},
blocking=True,
)
assert helper.characteristics[V1_ROTATION_SPEED].value == 25
await hass.services.async_call(
"fan",
"set_speed",
{"entity_id": "fan.testdevice", "speed": "off"},
blocking=True,
)
assert helper.characteristics[V1_ON].value == 0
async def test_speed_read(hass, utcnow):
"""Test that we can read a fans oscillation."""
fan = create_fan_service()
helper = await setup_test_component(hass, [fan])
helper.characteristics[V1_ON].value = 1
helper.characteristics[V1_ROTATION_SPEED].value = 100
state = await helper.poll_and_get_state()
assert state.attributes["speed"] == "high"
helper.characteristics[V1_ROTATION_SPEED].value = 50
state = await helper.poll_and_get_state()
assert state.attributes["speed"] == "medium"
helper.characteristics[V1_ROTATION_SPEED].value = 25
state = await helper.poll_and_get_state()
assert state.attributes["speed"] == "low"
helper.characteristics[V1_ON].value = 0
helper.characteristics[V1_ROTATION_SPEED].value = 0
state = await helper.poll_and_get_state()
assert state.attributes["speed"] == "off"
async def test_set_direction(hass, utcnow):
"""Test that we can set fan spin direction."""
fan = create_fan_service()
helper = await setup_test_component(hass, [fan])
await hass.services.async_call(
"fan",
"set_direction",
{"entity_id": "fan.testdevice", "direction": "reverse"},
blocking=True,
)
assert helper.characteristics[V1_ROTATION_DIRECTION].value == 1
await hass.services.async_call(
"fan",
"set_direction",
{"entity_id": "fan.testdevice", "direction": "forward"},
blocking=True,
)
assert helper.characteristics[V1_ROTATION_DIRECTION].value == 0
async def test_direction_read(hass, utcnow):
"""Test that we can read a fans oscillation."""
fan = create_fan_service()
helper = await setup_test_component(hass, [fan])
helper.characteristics[V1_ROTATION_DIRECTION].value = 0
state = await helper.poll_and_get_state()
assert state.attributes["direction"] == "forward"
helper.characteristics[V1_ROTATION_DIRECTION].value = 1
state = await helper.poll_and_get_state()
assert state.attributes["direction"] == "reverse"
async def test_fanv2_read_state(hass, utcnow):
"""Test that we can read the state of a HomeKit fan accessory."""
sensor = create_fanv2_service()
helper = await setup_test_component(hass, [sensor])
helper.characteristics[V2_ACTIVE].value = False
state = await helper.poll_and_get_state()
assert state.state == "off"
helper.characteristics[V2_ACTIVE].value = True
state = await helper.poll_and_get_state()
assert state.state == "on"
async def test_v2_turn_on(hass, utcnow):
"""Test that we can turn a fan on."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
await hass.services.async_call(
"fan",
"turn_on",
{"entity_id": "fan.testdevice", "speed": "high"},
blocking=True,
)
assert helper.characteristics[V2_ACTIVE].value == 1
assert helper.characteristics[V2_ROTATION_SPEED].value == 100
await hass.services.async_call(
"fan",
"turn_on",
{"entity_id": "fan.testdevice", "speed": "medium"},
blocking=True,
)
assert helper.characteristics[V2_ACTIVE].value == 1
assert helper.characteristics[V2_ROTATION_SPEED].value == 50
await hass.services.async_call(
"fan",
"turn_on",
{"entity_id": "fan.testdevice", "speed": "low"},
blocking=True,
)
assert helper.characteristics[V2_ACTIVE].value == 1
assert helper.characteristics[V2_ROTATION_SPEED].value == 25
async def test_v2_turn_off(hass, utcnow):
"""Test that we can turn a fan off."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
helper.characteristics[V2_ACTIVE].value = 1
await hass.services.async_call(
"fan", "turn_off", {"entity_id": "fan.testdevice"}, blocking=True,
)
assert helper.characteristics[V2_ACTIVE].value == 0
async def test_v2_set_speed(hass, utcnow):
"""Test that we set fan speed."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
helper.characteristics[V2_ACTIVE].value = 1
await hass.services.async_call(
"fan",
"set_speed",
{"entity_id": "fan.testdevice", "speed": "high"},
blocking=True,
)
assert helper.characteristics[V2_ROTATION_SPEED].value == 100
await hass.services.async_call(
"fan",
"set_speed",
{"entity_id": "fan.testdevice", "speed": "medium"},
blocking=True,
)
assert helper.characteristics[V2_ROTATION_SPEED].value == 50
await hass.services.async_call(
"fan",
"set_speed",
{"entity_id": "fan.testdevice", "speed": "low"},
blocking=True,
)
assert helper.characteristics[V2_ROTATION_SPEED].value == 25
await hass.services.async_call(
"fan",
"set_speed",
{"entity_id": "fan.testdevice", "speed": "off"},
blocking=True,
)
assert helper.characteristics[V2_ACTIVE].value == 0
async def test_v2_speed_read(hass, utcnow):
"""Test that we can read a fans oscillation."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
helper.characteristics[V2_ACTIVE].value = 1
helper.characteristics[V2_ROTATION_SPEED].value = 100
state = await helper.poll_and_get_state()
assert state.attributes["speed"] == "high"
helper.characteristics[V2_ROTATION_SPEED].value = 50
state = await helper.poll_and_get_state()
assert state.attributes["speed"] == "medium"
helper.characteristics[V2_ROTATION_SPEED].value = 25
state = await helper.poll_and_get_state()
assert state.attributes["speed"] == "low"
helper.characteristics[V2_ACTIVE].value = 0
helper.characteristics[V2_ROTATION_SPEED].value = 0
state = await helper.poll_and_get_state()
assert state.attributes["speed"] == "off"
async def test_v2_set_direction(hass, utcnow):
"""Test that we can set fan spin direction."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
await hass.services.async_call(
"fan",
"set_direction",
{"entity_id": "fan.testdevice", "direction": "reverse"},
blocking=True,
)
assert helper.characteristics[V2_ROTATION_DIRECTION].value == 1
await hass.services.async_call(
"fan",
"set_direction",
{"entity_id": "fan.testdevice", "direction": "forward"},
blocking=True,
)
assert helper.characteristics[V2_ROTATION_DIRECTION].value == 0
async def test_v2_direction_read(hass, utcnow):
"""Test that we can read a fans oscillation."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
helper.characteristics[V2_ROTATION_DIRECTION].value = 0
state = await helper.poll_and_get_state()
assert state.attributes["direction"] == "forward"
helper.characteristics[V2_ROTATION_DIRECTION].value = 1
state = await helper.poll_and_get_state()
assert state.attributes["direction"] == "reverse"
async def test_v2_oscillate(hass, utcnow):
"""Test that we can control a fans oscillation."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
await hass.services.async_call(
"fan",
"oscillate",
{"entity_id": "fan.testdevice", "oscillating": True},
blocking=True,
)
assert helper.characteristics[V2_SWING_MODE].value == 1
await hass.services.async_call(
"fan",
"oscillate",
{"entity_id": "fan.testdevice", "oscillating": False},
blocking=True,
)
assert helper.characteristics[V2_SWING_MODE].value == 0
async def test_v2_oscillate_read(hass, utcnow):
"""Test that we can read a fans oscillation."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
helper.characteristics[V2_SWING_MODE].value = 0
state = await helper.poll_and_get_state()
assert state.attributes["oscillating"] is False
helper.characteristics[V2_SWING_MODE].value = 1
state = await helper.poll_and_get_state()
assert state.attributes["oscillating"] is True