From deb913570721dd434197eb7702baae514523e916 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 25 May 2021 16:06:17 -0500 Subject: [PATCH] Handle clamped fan maxValue in homekit_controller (#51088) --- .../components/homekit_controller/fan.py | 3 +- .../specific_devices/test_haa_fan.py | 48 ++++ .../fixtures/homekit_controller/haa_fan.json | 257 ++++++++++++++++++ 3 files changed, 307 insertions(+), 1 deletion(-) create mode 100644 tests/components/homekit_controller/specific_devices/test_haa_fan.py create mode 100644 tests/fixtures/homekit_controller/haa_fan.json diff --git a/homeassistant/components/homekit_controller/fan.py b/homeassistant/components/homekit_controller/fan.py index 591050f5fd9..89f24a66a94 100644 --- a/homeassistant/components/homekit_controller/fan.py +++ b/homeassistant/components/homekit_controller/fan.py @@ -84,7 +84,8 @@ class BaseHomeKitFan(HomeKitEntity, FanEntity): def speed_count(self): """Speed count for the fan.""" return round( - 100 / max(1, self.service[CharacteristicsTypes.ROTATION_SPEED].minStep or 0) + min(self.service[CharacteristicsTypes.ROTATION_SPEED].maxValue or 100, 100) + / max(1, self.service[CharacteristicsTypes.ROTATION_SPEED].minStep or 0) ) async def async_set_direction(self, direction): diff --git a/tests/components/homekit_controller/specific_devices/test_haa_fan.py b/tests/components/homekit_controller/specific_devices/test_haa_fan.py new file mode 100644 index 00000000000..9e04434d830 --- /dev/null +++ b/tests/components/homekit_controller/specific_devices/test_haa_fan.py @@ -0,0 +1,48 @@ +"""Make sure that a H.A.A. fan can be setup.""" + +from homeassistant.helpers import device_registry as dr, entity_registry as er + +from tests.components.homekit_controller.common import ( + Helper, + setup_accessories_from_file, + setup_test_accessories, +) + + +async def test_haa_fan_setup(hass): + """Test that a H.A.A. fan can be correctly setup in HA.""" + accessories = await setup_accessories_from_file(hass, "haa_fan.json") + config_entry, pairing = await setup_test_accessories(hass, accessories) + + entity_registry = er.async_get(hass) + device_registry = dr.async_get(hass) + + # Check that the switch entity is handled correctly + + entry = entity_registry.async_get("switch.haa_c718b3") + assert entry.unique_id == "homekit-C718B3-2-8" + + helper = Helper(hass, "switch.haa_c718b3", pairing, accessories[0], config_entry) + state = await helper.poll_and_get_state() + assert state.attributes["friendly_name"] == "HAA-C718B3" + + device = device_registry.async_get(entry.device_id) + assert device.manufacturer == "José A. Jiménez Campos" + assert device.name == "HAA-C718B3" + assert device.sw_version == "5.0.18" + assert device.via_device_id is not None + + # Assert the fan is detected + entry = entity_registry.async_get("fan.haa_c718b3") + assert entry.unique_id == "homekit-C718B3-1-8" + + helper = Helper( + hass, + "fan.haa_c718b3", + pairing, + accessories[0], + config_entry, + ) + state = await helper.poll_and_get_state() + assert state.attributes["friendly_name"] == "HAA-C718B3" + assert round(state.attributes["percentage_step"], 2) == 33.33 diff --git a/tests/fixtures/homekit_controller/haa_fan.json b/tests/fixtures/homekit_controller/haa_fan.json new file mode 100644 index 00000000000..b06ccdf9644 --- /dev/null +++ b/tests/fixtures/homekit_controller/haa_fan.json @@ -0,0 +1,257 @@ +[ + { + "aid": 1, + "services": [ + { + "iid": 1, + "type": "0000003E-0000-1000-8000-0026BB765291", + "primary": false, + "hidden": false, + "characteristics": [ + { + "aid": 2, + "iid": 2, + "type": "00000023-0000-1000-8000-0026BB765291", + "perms": [ + "pr" + ], + "format": "string", + "value": "HAA-C718B3" + }, + { + "aid": 2, + "iid": 3, + "type": "00000020-0000-1000-8000-0026BB765291", + "perms": [ + "pr" + ], + "format": "string", + "value": "Jos\u00e9 A. Jim\u00e9nez Campos" + }, + { + "aid": 1, + "iid": 4, + "type": "00000030-0000-1000-8000-0026BB765291", + "perms": [ + "pr" + ], + "format": "string", + "value": "C718B3-1" + }, + { + "aid": 2, + "iid": 5, + "type": "00000021-0000-1000-8000-0026BB765291", + "perms": [ + "pr" + ], + "format": "string", + "value": "RavenSystem HAA" + }, + { + "aid": 2, + "iid": 6, + "type": "00000052-0000-1000-8000-0026BB765291", + "perms": [ + "pr" + ], + "format": "string", + "value": "5.0.18" + }, + { + "aid": 2, + "iid": 7, + "type": "00000014-0000-1000-8000-0026BB765291", + "perms": [ + "pw" + ], + "format": "bool" + } + ] + }, + { + "iid": 8, + "type": "00000040-0000-1000-8000-0026BB765291", + "primary": true, + "hidden": false, + "characteristics": [ + { + "aid": 1, + "iid": 9, + "type": "00000025-0000-1000-8000-0026BB765291", + "perms": [ + "pr", + "pw", + "ev" + ], + "ev": true, + "format": "bool", + "value": false + }, + { + "aid": 1, + "iid": 10, + "type": "00000029-0000-1000-8000-0026BB765291", + "perms": [ + "pr", + "pw", + "ev" + ], + "ev": true, + "format": "float", + "unit": "percentage", + "minValue": 0, + "maxValue": 3, + "minStep": 1, + "value": 3 + } + ] + }, + { + "iid": 1000, + "type": "000000A2-0000-1000-8000-0026BB765291", + "primary": false, + "hidden": true, + "characteristics": [ + { + "aid": 1, + "iid": 1001, + "type": "00000037-0000-1000-8000-0026BB765291", + "perms": [ + "pr" + ], + "format": "string", + "value": "1.1.0" + } + ] + }, + { + "iid": 1010, + "type": "F0000100-0218-2017-81BF-AF2B7C833922", + "primary": false, + "hidden": true, + "characteristics": [ + { + "aid": 1, + "iid": 1011, + "type": "F0000101-0218-2017-81BF-AF2B7C833922", + "perms": [ + "pr", + "pw", + "hd" + ], + "description": "Update", + "format": "string", + "value": "" + }, + { + "aid": 1, + "iid": 1012, + "type": "F0000102-0218-2017-81BF-AF2B7C833922", + "perms": [ + "pr", + "pw", + "hd" + ], + "description": "Setup", + "format": "string", + "value": "" + } + ] + } + ] + }, + { + "aid": 2, + "services": [ + { + "iid": 1, + "type": "0000003E-0000-1000-8000-0026BB765291", + "primary": false, + "hidden": false, + "characteristics": [ + { + "aid": 2, + "iid": 2, + "type": "00000023-0000-1000-8000-0026BB765291", + "perms": [ + "pr" + ], + "format": "string", + "value": "HAA-C718B3" + }, + { + "aid": 2, + "iid": 3, + "type": "00000020-0000-1000-8000-0026BB765291", + "perms": [ + "pr" + ], + "format": "string", + "value": "Jos\u00e9 A. Jim\u00e9nez Campos" + }, + { + "aid": 2, + "iid": 4, + "type": "00000030-0000-1000-8000-0026BB765291", + "perms": [ + "pr" + ], + "format": "string", + "value": "C718B3-2" + }, + { + "aid": 2, + "iid": 5, + "type": "00000021-0000-1000-8000-0026BB765291", + "perms": [ + "pr" + ], + "format": "string", + "value": "RavenSystem HAA" + }, + { + "aid": 2, + "iid": 6, + "type": "00000052-0000-1000-8000-0026BB765291", + "perms": [ + "pr" + ], + "format": "string", + "value": "5.0.18" + }, + { + "aid": 2, + "iid": 7, + "type": "00000014-0000-1000-8000-0026BB765291", + "perms": [ + "pw" + ], + "format": "bool" + } + ] + }, + { + "iid": 8, + "type": "00000049-0000-1000-8000-0026BB765291", + "primary": true, + "hidden": false, + "characteristics": [ + { + "aid": 2, + "iid": 9, + "type": "00000025-0000-1000-8000-0026BB765291", + "perms": [ + "pr", + "pw", + "ev" + ], + "ev": true, + "format": "bool", + "value": false + } + ] + } + ] + } +] \ No newline at end of file