Add humidifier support to homekit (#37207)

* Add humidifier support to homekit

* spell

* dependencies

* lint

* add linked humidity sensor for humidifiers

* Apply suggestions from code review

Co-authored-by: J. Nick Koston <nick@koston.org>

* apply suggestions from code review

* pylint

* Fix tests

* Update homeassistant/components/homekit/type_humidifiers.py

Co-authored-by: J. Nick Koston <nick@koston.org>

* Update tests/components/homekit/test_homekit.py

Co-authored-by: J. Nick Koston <nick@koston.org>

* Apply suggestions from code review

Co-authored-by: J. Nick Koston <nick@koston.org>

* apply suggestions from code review

* lint

* pylint

* push

* test for unavailable linker sensor

* black

* valid values key case

* black

* Update homeassistant/components/homekit/type_humidifiers.py

Co-authored-by: J. Nick Koston <nick@koston.org>

* black

* coverage

* Set current humidity to 0 if linked sensor removed or unavailable

* use last known humidity instead

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Shulyaka 2020-07-02 20:53:11 +03:00 committed by GitHub
parent 8bce9be590
commit 4ec71c58bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 780 additions and 2 deletions

View file

@ -42,13 +42,16 @@ from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
ATTR_UNIT_OF_MEASUREMENT,
CONF_IP_ADDRESS,
CONF_NAME,
CONF_PORT,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_HUMIDITY,
EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP,
STATE_ON,
UNIT_PERCENTAGE,
)
from homeassistant.core import State
from homeassistant.helpers import device_registry
@ -1094,3 +1097,80 @@ async def test_homekit_finds_linked_motion_sensors(
"linked_motion_sensor": "binary_sensor.camera_motion_sensor",
},
)
async def test_homekit_finds_linked_humidity_sensors(
hass, hk_driver, debounce_patcher, device_reg, entity_reg
):
"""Test HomeKit start method."""
entry = await async_init_integration(hass)
homekit = HomeKit(
hass,
None,
None,
None,
{},
{"humidifier.humidifier": {}},
DEFAULT_SAFE_MODE,
advertise_ip=None,
entry_id=entry.entry_id,
)
homekit.driver = hk_driver
homekit._filter = Mock(return_value=True)
homekit.bridge = HomeBridge(hass, hk_driver, "mock_bridge")
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
sw_version="0.16.1",
model="Smart Brainy Clever Humidifier",
manufacturer="Home Assistant",
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
humidity_sensor = entity_reg.async_get_or_create(
"sensor",
"humidifier",
"humidity_sensor",
device_id=device_entry.id,
device_class=DEVICE_CLASS_HUMIDITY,
)
humidifier = entity_reg.async_get_or_create(
"humidifier", "humidifier", "demo", device_id=device_entry.id
)
hass.states.async_set(
humidity_sensor.entity_id,
"42",
{
ATTR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY,
ATTR_UNIT_OF_MEASUREMENT: UNIT_PERCENTAGE,
},
)
hass.states.async_set(humidifier.entity_id, STATE_ON)
def _mock_get_accessory(*args, **kwargs):
return [None, "acc", None]
with patch.object(homekit.bridge, "add_accessory"), patch(
f"{PATH_HOMEKIT}.show_setup_message"
), patch(f"{PATH_HOMEKIT}.get_accessory") as mock_get_acc, patch(
"pyhap.accessory_driver.AccessoryDriver.start"
):
await homekit.async_start()
await hass.async_block_till_done()
mock_get_acc.assert_called_with(
hass,
hk_driver,
ANY,
ANY,
{
"manufacturer": "Home Assistant",
"model": "Smart Brainy Clever Humidifier",
"sw_version": "0.16.1",
"linked_humidity_sensor": "sensor.humidifier_humidity_sensor",
},
)