-
-
Notifications
You must be signed in to change notification settings - Fork 38k
Support binary_sensor and device_tracker for HomeKit #13701
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
Changes from 9 commits
05041c9
aaa5e5f
1e4cd78
af89ce6
cacf0e5
c72faea
469b7e9
a80f739
4eb721e
d10c6cf
ef448c7
3fe9284
8548761
3394916
48fe2d1
c77d013
262ea14
fdf93d1
286476f
58f3690
669dbe6
7fa34fe
050d0d9
44436ce
8af2099
43c5ea7
82606ae
260a47d
73cac24
51d8e22
4e08e1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,23 @@ | |
| import logging | ||
|
|
||
| from homeassistant.const import ( | ||
| ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS) | ||
| ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS, | ||
| ATTR_DEVICE_CLASS, STATE_ON, STATE_HOME) | ||
|
|
||
| from . import TYPES | ||
| from .accessories import HomeAccessory, add_preload_service | ||
| from .const import ( | ||
| CATEGORY_SENSOR, SERV_HUMIDITY_SENSOR, SERV_TEMPERATURE_SENSOR, | ||
| CHAR_CURRENT_HUMIDITY, CHAR_CURRENT_TEMPERATURE, PROP_CELSIUS) | ||
| CHAR_CURRENT_HUMIDITY, CHAR_CURRENT_TEMPERATURE, PROP_CELSIUS, | ||
| DEVICE_CLASS_CO2, SERV_CARBON_DIOXIDE_SENSOR, CHAR_CARBON_DIOXIDE_DETECTED, | ||
| DEVICE_CLASS_GAS, SERV_CARBON_MONOXIDE_SENSOR, | ||
| CHAR_CARBON_MONOXIDE_DETECTED, | ||
| DEVICE_CLASS_MOISTURE, SERV_LEAK_SENSOR, CHAR_LEAK_DETECTED, | ||
| DEVICE_CLASS_MOTION, SERV_MOTION_SENSOR, CHAR_MOTION_DETECTED, | ||
| DEVICE_CLASS_OCCUPANCY, SERV_OCCUPANCY_SENSOR, CHAR_OCCUPANCY_DETECTED, | ||
| DEVICE_CLASS_OPENING, SERV_CONTACT_SENSOR, CHAR_CONTACT_SENSOR_STATE, | ||
| DEVICE_CLASS_SMOKE, SERV_SMOKE_SENSOR, CHAR_SMOKE_DETECTED, | ||
| ATTR_HOMEKIT_DEVICE_CLASS) | ||
| from .util import convert_to_float, temperature_to_homekit | ||
|
|
||
|
|
||
|
|
@@ -75,3 +85,53 @@ def update_state(self, entity_id=None, old_state=None, new_state=None): | |
| self.char_humidity.set_value(humidity, should_callback=False) | ||
| _LOGGER.debug('%s: Percent set to %d%%', | ||
| self.entity_id, humidity) | ||
|
|
||
|
|
||
| # Binary sensor service map | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need that.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed |
||
| BINARY_SENSOR_SERVICE_MAP = { | ||
| DEVICE_CLASS_CO2: | ||
| (SERV_CARBON_DIOXIDE_SENSOR, CHAR_CARBON_DIOXIDE_DETECTED), | ||
| DEVICE_CLASS_GAS: | ||
| (SERV_CARBON_MONOXIDE_SENSOR, CHAR_CARBON_MONOXIDE_DETECTED), | ||
| DEVICE_CLASS_MOISTURE: | ||
| (SERV_LEAK_SENSOR, CHAR_LEAK_DETECTED), | ||
| DEVICE_CLASS_MOTION: | ||
| (SERV_MOTION_SENSOR, CHAR_MOTION_DETECTED), | ||
| DEVICE_CLASS_OCCUPANCY: | ||
| (SERV_OCCUPANCY_SENSOR, CHAR_OCCUPANCY_DETECTED), | ||
| DEVICE_CLASS_OPENING: | ||
| (SERV_CONTACT_SENSOR, CHAR_CONTACT_SENSOR_STATE), | ||
| DEVICE_CLASS_SMOKE: | ||
| (SERV_SMOKE_SENSOR, CHAR_SMOKE_DETECTED)} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that an option? BINARY_SENSOR_SERVICE_MAP = {
DEVICE_CLASS_CO2: (SERV_CARBON_DIOXIDE_SENSOR,
CHAR_CARBON_DIOXIDE_DETECTED),
DEVICE_CLASS_GAS: (SERV_CARBON_MONOXIDE_SENSOR,
CHAR_CARBON_MONOXIDE_DETECTED),
DEVICE_CLASS_MOISTURE: (SERV_LEAK_SENSOR, CHAR_LEAK_DETECTED),
DEVICE_CLASS_MOTION: (SERV_MOTION_SENSOR, CHAR_MOTION_DETECTED),
DEVICE_CLASS_OCCUPANCY: (SERV_OCCUPANCY_SENSOR, CHAR_OCCUPANCY_DETECTED),
DEVICE_CLASS_OPENING: (SERV_CONTACT_SENSOR, CHAR_CONTACT_SENSOR_STATE),
DEVICE_CLASS_SMOKE: (SERV_SMOKE_SENSOR, CHAR_SMOKE_DETECTED)}
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
|
|
||
| @TYPES.register('BinarySensor') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expected 2 blank lines, found 1 |
||
| class BinarySensor(HomeAccessory): | ||
| """Generate a BinarySensor accessory as binary sensor.""" | ||
|
|
||
| def __init__(self, hass, entity_id, name, **kwargs): | ||
| """Initialize a BinarySensor accessory object.""" | ||
| super().__init__(name, entity_id, CATEGORY_SENSOR, **kwargs) | ||
|
|
||
| self.hass = hass | ||
| self.entity_id = entity_id | ||
|
|
||
| attributes = hass.states.get(entity_id).attributes | ||
| device_class = attributes.get(ATTR_DEVICE_CLASS) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can combine those two lines and please use device_class = self.hass.states.get(self.entity_id).attributes.get(ATTR_DEVICE_CLASS)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be a little. Update: |
||
| service_char = BINARY_SENSOR_SERVICE_MAP[device_class] \ | ||
| if (device_class in BINARY_SENSOR_SERVICE_MAP) \ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need brackets here. |
||
| else BINARY_SENSOR_SERVICE_MAP[DEVICE_CLASS_OCCUPANCY] | ||
|
|
||
| service = add_preload_service(self, service_char[0]) | ||
| self.char_detected = service.get_characteristic(service_char[1]) | ||
| self.char_detected.value = 0 | ||
|
|
||
| def update_state(self, entity_id=None, old_state=None, new_state=None): | ||
| """Update accessory after state change.""" | ||
| if new_state is None: | ||
| return | ||
|
|
||
| state = new_state.state | ||
| detected = (state == STATE_ON) or (state == STATE_HOME) | ||
| self.char_detected.set_value(detected, should_callback=False) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We just updated HAP-python to 1.1.9. 'Should_callback=False' will no longer be necessary. You probably need to rebase your branch, take take advantage of the update. |
||
| _LOGGER.debug('%s: Set to %d', self.entity_id, detected) | ||
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.
ATTR_HOMEKIT_DEVICE_CLASSis an unused import.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