1
mirror of https://github.com/home-assistant/core synced 2024-09-06 10:29:55 +02:00

Add support for HomeKit motion sensor devices (#20555)

This commit is contained in:
Jc2k 2019-01-29 04:30:56 +00:00 committed by Paulus Schoutsen
parent cc74035c3b
commit e22802a4d4
3 changed files with 86 additions and 1 deletions

View File

@ -28,7 +28,8 @@ HOMEKIT_ACCESSORY_DISPATCH = {
'garage-door-opener': 'cover',
'window': 'cover',
'window-covering': 'cover',
'lock-mechanism': 'lock'
'lock-mechanism': 'lock',
'motion': 'binary_sensor',
}
HOMEKIT_IGNORE = [

View File

@ -0,0 +1,55 @@
"""
Support for Homekit motion sensors.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/binary_sensor.homekit_controller/
"""
import logging
from homeassistant.components.homekit_controller import (HomeKitEntity,
KNOWN_ACCESSORIES)
from homeassistant.components.binary_sensor import BinarySensorDevice
DEPENDENCIES = ['homekit_controller']
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up Homekit motion sensor support."""
if discovery_info is not None:
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
add_entities([HomeKitMotionSensor(accessory, discovery_info)], True)
class HomeKitMotionSensor(HomeKitEntity, BinarySensorDevice):
"""Representation of a Homekit sensor."""
def __init__(self, *args):
"""Initialise the entity."""
super().__init__(*args)
self._on = False
def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
# pylint: disable=import-error
from homekit.model.characteristics import CharacteristicsTypes
return [
CharacteristicsTypes.MOTION_DETECTED,
]
def _update_motion_detected(self, value):
self._on = value
@property
def device_class(self):
"""Define this binary_sensor as a motion sensor."""
return 'motion'
@property
def is_on(self):
"""Has motion been detected."""
if not self.available:
return False
return self._on

View File

@ -0,0 +1,29 @@
"""Basic checks for HomeKitLock."""
from tests.components.homekit_controller.common import (
FakeService, setup_test_component)
MOTION_DETECTED = ('motion', 'motion-detected')
def create_sensor_motion_service():
"""Define motion characteristics as per page 225 of HAP spec."""
service = FakeService('public.hap.service.sensor.motion')
cur_state = service.add_characteristic('motion-detected')
cur_state.value = 0
return service
async def test_sensor_read_state(hass, utcnow):
"""Test that we can read the state of a HomeKit motion sensor accessory."""
sensor = create_sensor_motion_service()
helper = await setup_test_component(hass, [sensor])
helper.characteristics[MOTION_DETECTED].value = False
state = await helper.poll_and_get_state()
assert state.state == 'off'
helper.characteristics[MOTION_DETECTED].value = True
state = await helper.poll_and_get_state()
assert state.state == 'on'