-
-
Notifications
You must be signed in to change notification settings - Fork 28.8k
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
Support binary_sensor and device_tracker in HomeKit. #13735
Conversation
from homeassistant.const import ( | ||
ATTR_UNIT_OF_MEASUREMENT, STATE_UNKNOWN, TEMP_CELSIUS, TEMP_FAHRENHEIT) | ||
ATTR_UNIT_OF_MEASUREMENT, ATTR_DEVICE_CLASS, STATE_UNKNOWN, STATE_ON, STATE_OFF, STATE_HOME, STATE_NOT_HOME, TEMP_CELSIUS, TEMP_FAHRENHEIT) |
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.
line too long (143 > 79 characters)
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.
It's looking good so far. Unfortunately I can't test it until tomorrow evening (CET).
Since you dynamically assign the service and char, can you add tests to verify that the right ones where selected. You can get the service / char name with service.display_name
or char.display_name
. Those must be equal to the HomeKit char / service constants.
|
||
self.hass.states.set(entity_id, STATE_UNKNOWN, | ||
{ATTR_DEVICE_CLASS: "opening"}) | ||
self.hass.block_till_done() # Ensure state.attributes |
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.
You don't need that comment.
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.
done
@@ -1,11 +1,13 @@ | |||
"""Test different accessory types: Sensors.""" | |||
import unittest | |||
|
|||
from homeassistant.components.homekit.const import PROP_CELSIUS | |||
from homeassistant.components.homekit.const import (PROP_CELSIUS, | |||
SERV_CONTACT_SENSOR, CHAR_CONTACT_SENSOR_STATE) |
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.
continuation line under-indented for visual indent
self.hass.states.set(entity_id, STATE_NOT_HOME, | ||
{ATTR_DEVICE_CLASS: "opening"}) | ||
self.hass.block_till_done() | ||
self.assertEqual(acc.char_detected.value, 0) |
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.
Please add this to the end. That way you improve the coverage.
self.hass.states.remove(entity_id)
self.hass.block_till_done()
self.assertEqual(acc.get_service(SERV_CONTACT_SENSOR).display_name, | ||
SERV_CONTACT_SENSOR) | ||
self.assertEqual(acc.char_detected.display_name, | ||
CHAR_CONTACT_SENSOR_STATE) |
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.
Instead of L85 - L88, why not do something like that:
from homeassistant.components.homekit.type_sensors import BINARY_SENSOR_SERVICE_MAP
class TestHomekitSensors(unittest.TestCase):
#####
#####
def test_binary(self):
#####
#####
def test_binary_device_classes(self):
entity_id = 'binary_sensor.demo'
for device_class, (service, char) in BINARY_SENSOR_SERVICE_MAP.items():
self.hass.states.set(entity_id, STATE_OFF,
{ATTR_DEVICE_CLASS: device_class})
self.hass.block_till_done()
acc = BinarySensor(self.hass, entity_id, 'Binary Sensor', aid=2)
self.assertEqual(acc.get_service(service).display_name, service)
self.assertEqual(acc.char_detected.display_name, char)
That way you check if every service and char can be added successfully.
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.
done
Passed: tox -e py36 -- tests/components/homekit/test_type_sensors.py -x tox -e py36 -- tests/components/homekit/test_get_accessories.py -x
1. Passed: tox -e py36 -- tests/components/homekit/test_type_sensors.py -x 2. And remove ‘# Ensure state.attributes’ comment
PASSED: tox -e py36 -- tests/components/homekit/test_type_sensors.py PASSED: script/lint (in my submit)
Thanks 👍 |
Thank you for your effective advice and guidance. |
cong~ merged! |
@Yonsm great, thanks a lot! @cdce8p maybe its possible to add a default mapping to binary_sensors with device_class "opening" for all sensors which returns states like "open/tilted/closed"? something similar to the existing temperature sensors. with that change I don't need to create extra template binary_sensors anymore :) |
@nicx There is a plan to add |
@cdce8p great to hear, I can wait for this 👍 keep up this great work! 🥇 |
Or maybe it's best to default to |
Please don't discuss feature requests in closed PRs. |
@MartinHjelmare seemed like an omission, what SHOULD I do instead? |
If you want to suggest an enhancement please open a feature request in the Feature Requests section of our community forum. Merged PRs should not be used for enhancement discussion or bug reports. If you've found a bug it's ok to make a review with inline comments and link to an issue that reports the bug. Thanks! |
…3735) * Support binary_sensor and device_tracker for HomeKit * Add test for get_accessory and binary sensor * Test service.display_name and char_detected.display_name * Split test to improve speed
Description:
Map binary_sensor and device_tracker to HomeKit Binary Sensor, based on 'device_class' attributes:
If there is no any matched device_class, e.g. device_tracker, then default sensor type is OccupancySensor.
Checklist:
tox
.Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#5143