-
-
Notifications
You must be signed in to change notification settings - Fork 37.3k
Add support for HomeKit motion sensor devices #20555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
homeassistant/components/homekit_controller/binary_sensor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
| return False | ||
| return self._on | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.