Skip to content
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

Merged
merged 7 commits into from
Apr 9, 2018
Merged

Support binary_sensor and device_tracker in HomeKit. #13735

merged 7 commits into from
Apr 9, 2018

Conversation

Yonsm
Copy link

@Yonsm Yonsm commented Apr 7, 2018

Description:

Map binary_sensor and device_tracker to HomeKit Binary Sensor, based on 'device_class' attributes:

'co2': 'CarbonDioxideSensor'
'gas': 'CarbonMonoxideSensor'
'moisture': 'LeakSensor'
'motion': 'MotionSensor'
'occupancy': 'OccupancySensor'
'opening': 'ContactSensor'
'smoke': 'SmokeSensor'

If there is no any matched device_class, e.g. device_tracker, then default sensor type is OccupancySensor.

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox.

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#5143

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)

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)

@cdce8p cdce8p self-assigned this Apr 7, 2018
Copy link
Member

@cdce8p cdce8p left a 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
Copy link
Member

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.

Copy link
Author

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)

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)
Copy link
Member

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)
Copy link
Member

@cdce8p cdce8p Apr 8, 2018

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.

Copy link
Author

Choose a reason for hiding this comment

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

done

Yonsm added 5 commits April 9, 2018 13:28
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
Yonsm and others added 2 commits April 9, 2018 14:07
PASSED: tox -e py36 -- tests/components/homekit/test_type_sensors.py
PASSED: script/lint (in my submit)
@cdce8p cdce8p merged commit cb51553 into home-assistant:dev Apr 9, 2018
@cdce8p
Copy link
Member

cdce8p commented Apr 9, 2018

Thanks 👍
Looking forward to more PR's

@Yonsm
Copy link
Author

Yonsm commented Apr 9, 2018

Thank you for your effective advice and guidance.

@Yonsm Yonsm deleted the homekit_binary_sensor branch April 9, 2018 14:16
@shuaiger
Copy link
Contributor

shuaiger commented Apr 9, 2018

cong~ merged!

@nicx
Copy link
Contributor

nicx commented Apr 10, 2018

@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 :)

@cdce8p
Copy link
Member

cdce8p commented Apr 10, 2018

@nicx There is a plan to add device_classes to normal sensors which would allow better types for HomeKit. I just don't know if I get to it this week.

@nicx
Copy link
Contributor

nicx commented Apr 10, 2018

@cdce8p great to hear, I can wait for this 👍 keep up this great work! 🥇

@spacesuitdiver
Copy link

Great work @Yonsm and @cdce8p! One more thing (cough I feel I say this too often), it would be nice to support "door" as a device_class for ContactSensor as well?

@spacesuitdiver
Copy link

Or maybe it's best to default to ContactSensor since this allows the most flexibility in Homekit and is probably the likely use of a binary_sensor?

@MartinHjelmare
Copy link
Member

Please don't discuss feature requests in closed PRs.

@spacesuitdiver
Copy link

@MartinHjelmare seemed like an omission, what SHOULD I do instead?

@MartinHjelmare
Copy link
Member

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!

@balloob balloob mentioned this pull request Apr 27, 2018
Adminiuga pushed a commit to Adminiuga/home-assistant that referenced this pull request Jun 25, 2018
…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
@home-assistant home-assistant locked and limited conversation to collaborators Jul 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants