mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Add support for homekit controller sensors (#21535)
Adds support for homekit devices with temperature, humidity, and light level characteristics (such as the iHome iSS50)
This commit is contained in:
@@ -134,10 +134,12 @@ class FakeService(AbstractService):
|
||||
return char
|
||||
|
||||
|
||||
async def setup_test_component(hass, services, capitalize=False):
|
||||
async def setup_test_component(hass, services, capitalize=False, suffix=None):
|
||||
"""Load a fake homekit accessory based on a homekit accessory model.
|
||||
|
||||
If capitalize is True, property names will be in upper case.
|
||||
|
||||
If suffix is set, entityId will include the suffix
|
||||
"""
|
||||
domain = None
|
||||
for service in services:
|
||||
@@ -174,4 +176,5 @@ async def setup_test_component(hass, services, capitalize=False):
|
||||
fire_service_discovered(hass, SERVICE_HOMEKIT, discovery_info)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return Helper(hass, '.'.join((domain, 'testdevice')), pairing, accessory)
|
||||
entity = 'testdevice' if suffix is None else 'testdevice_{}'.format(suffix)
|
||||
return Helper(hass, '.'.join((domain, entity)), pairing, accessory)
|
||||
|
||||
79
tests/components/homekit_controller/test_sensor.py
Normal file
79
tests/components/homekit_controller/test_sensor.py
Normal file
@@ -0,0 +1,79 @@
|
||||
"""Basic checks for HomeKit sensor."""
|
||||
from tests.components.homekit_controller.common import (
|
||||
FakeService, setup_test_component)
|
||||
|
||||
TEMPERATURE = ('temperature', 'temperature.current')
|
||||
HUMIDITY = ('humidity', 'relative-humidity.current')
|
||||
LIGHT_LEVEL = ('light', 'light-level.current')
|
||||
|
||||
|
||||
def create_temperature_sensor_service():
|
||||
"""Define temperature characteristics."""
|
||||
service = FakeService('public.hap.service.sensor.temperature')
|
||||
|
||||
cur_state = service.add_characteristic('temperature.current')
|
||||
cur_state.value = 0
|
||||
|
||||
return service
|
||||
|
||||
|
||||
def create_humidity_sensor_service():
|
||||
"""Define humidity characteristics."""
|
||||
service = FakeService('public.hap.service.sensor.humidity')
|
||||
|
||||
cur_state = service.add_characteristic('relative-humidity.current')
|
||||
cur_state.value = 0
|
||||
|
||||
return service
|
||||
|
||||
|
||||
def create_light_level_sensor_service():
|
||||
"""Define light level characteristics."""
|
||||
service = FakeService('public.hap.service.sensor.light')
|
||||
|
||||
cur_state = service.add_characteristic('light-level.current')
|
||||
cur_state.value = 0
|
||||
|
||||
return service
|
||||
|
||||
|
||||
async def test_temperature_sensor_read_state(hass, utcnow):
|
||||
"""Test reading the state of a HomeKit temperature sensor accessory."""
|
||||
sensor = create_temperature_sensor_service()
|
||||
helper = await setup_test_component(hass, [sensor], suffix="temperature")
|
||||
|
||||
helper.characteristics[TEMPERATURE].value = 10
|
||||
state = await helper.poll_and_get_state()
|
||||
assert state.state == '10'
|
||||
|
||||
helper.characteristics[TEMPERATURE].value = 20
|
||||
state = await helper.poll_and_get_state()
|
||||
assert state.state == '20'
|
||||
|
||||
|
||||
async def test_humidity_sensor_read_state(hass, utcnow):
|
||||
"""Test reading the state of a HomeKit humidity sensor accessory."""
|
||||
sensor = create_humidity_sensor_service()
|
||||
helper = await setup_test_component(hass, [sensor], suffix="humidity")
|
||||
|
||||
helper.characteristics[HUMIDITY].value = 10
|
||||
state = await helper.poll_and_get_state()
|
||||
assert state.state == '10'
|
||||
|
||||
helper.characteristics[HUMIDITY].value = 20
|
||||
state = await helper.poll_and_get_state()
|
||||
assert state.state == '20'
|
||||
|
||||
|
||||
async def test_light_level_sensor_read_state(hass, utcnow):
|
||||
"""Test reading the state of a HomeKit temperature sensor accessory."""
|
||||
sensor = create_light_level_sensor_service()
|
||||
helper = await setup_test_component(hass, [sensor], suffix="light_level")
|
||||
|
||||
helper.characteristics[LIGHT_LEVEL].value = 10
|
||||
state = await helper.poll_and_get_state()
|
||||
assert state.state == '10'
|
||||
|
||||
helper.characteristics[LIGHT_LEVEL].value = 20
|
||||
state = await helper.poll_and_get_state()
|
||||
assert state.state == '20'
|
||||
Reference in New Issue
Block a user