Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion homeassistant/components/homekit_controller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
'garage-door-opener': 'cover',
'window': 'cover',
'window-covering': 'cover',
'lock-mechanism': 'lock'
'lock-mechanism': 'lock',
'motion': 'binary_sensor',
}

HOMEKIT_IGNORE = [
Expand Down
55 changes: 55 additions & 0 deletions homeassistant/components/homekit_controller/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -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:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check isn't necessary. It will be handled by the base entity class.

return False
return self._on
29 changes: 29 additions & 0 deletions tests/components/homekit_controller/test_binary_sensor.py
Original file line number Diff line number Diff line change
@@ -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'