Improve Alexa interface selection for binary sensors (#29120)

* Improve Alexa interface selection for binary sensors

This allows the sensor to work correctly as a contact or motion sensor
in alexa, if the user overrides its display category as such.

* add tests
This commit is contained in:
Abílio Costa 2019-12-03 00:02:17 +00:00 committed by Paulus Schoutsen
parent 9160d43a08
commit 9811d63d78
3 changed files with 67 additions and 1 deletions

View File

@ -543,6 +543,10 @@ class BinarySensorCapabilities(AlexaEntity):
if CONF_DISPLAY_CATEGORIES in entity_conf:
if entity_conf[CONF_DISPLAY_CATEGORIES] == DisplayCategory.DOORBELL:
yield AlexaDoorbellEventSource(self.entity)
elif entity_conf[CONF_DISPLAY_CATEGORIES] == DisplayCategory.CONTACT_SENSOR:
yield AlexaContactSensor(self.hass, self.entity)
elif entity_conf[CONF_DISPLAY_CATEGORIES] == DisplayCategory.MOTION_SENSOR:
yield AlexaMotionSensor(self.hass, self.entity)
yield AlexaEndpointHealth(self.hass, self.entity)
yield Alexa(self.hass)

View File

@ -13,7 +13,11 @@ TEST_TOKEN_URL = "https://api.amazon.com/auth/o2/token"
class MockConfig(config.AbstractConfig):
"""Mock Alexa config."""
entity_config = {"binary_sensor.test_doorbell": {"display_categories": "DOORBELL"}}
entity_config = {
"binary_sensor.test_doorbell": {"display_categories": "DOORBELL"},
"binary_sensor.test_contact_forced": {"display_categories": "CONTACT_SENSOR"},
"binary_sensor.test_motion_forced": {"display_categories": "MOTION_SENSOR"},
}
@property
def supports_auth(self):

View File

@ -1473,6 +1473,35 @@ async def test_contact_sensor(hass):
properties.assert_equal("Alexa.EndpointHealth", "connectivity", {"value": "OK"})
async def test_forced_contact_sensor(hass):
"""Test contact sensor discovery with specified display_category."""
device = (
"binary_sensor.test_contact_forced",
"on",
{"friendly_name": "Test Contact Sensor With DisplayCategory"},
)
appliance = await discovery_test(device, hass)
assert appliance["endpointId"] == "binary_sensor#test_contact_forced"
assert appliance["displayCategories"][0] == "CONTACT_SENSOR"
assert appliance["friendlyName"] == "Test Contact Sensor With DisplayCategory"
capabilities = assert_endpoint_capabilities(
appliance, "Alexa.ContactSensor", "Alexa.EndpointHealth", "Alexa"
)
contact_sensor_capability = get_capability(capabilities, "Alexa.ContactSensor")
assert contact_sensor_capability is not None
properties = contact_sensor_capability["properties"]
assert properties["retrievable"] is True
assert {"name": "detectionState"} in properties["supported"]
properties = await reported_properties(hass, "binary_sensor#test_contact_forced")
properties.assert_equal("Alexa.ContactSensor", "detectionState", "DETECTED")
properties.assert_equal("Alexa.EndpointHealth", "connectivity", {"value": "OK"})
async def test_motion_sensor(hass):
"""Test motion sensor discovery."""
device = (
@ -1500,6 +1529,35 @@ async def test_motion_sensor(hass):
properties.assert_equal("Alexa.MotionSensor", "detectionState", "DETECTED")
async def test_forced_motion_sensor(hass):
"""Test motion sensor discovery with specified display_category."""
device = (
"binary_sensor.test_motion_forced",
"on",
{"friendly_name": "Test Motion Sensor With DisplayCategory"},
)
appliance = await discovery_test(device, hass)
assert appliance["endpointId"] == "binary_sensor#test_motion_forced"
assert appliance["displayCategories"][0] == "MOTION_SENSOR"
assert appliance["friendlyName"] == "Test Motion Sensor With DisplayCategory"
capabilities = assert_endpoint_capabilities(
appliance, "Alexa.MotionSensor", "Alexa.EndpointHealth", "Alexa"
)
motion_sensor_capability = get_capability(capabilities, "Alexa.MotionSensor")
assert motion_sensor_capability is not None
properties = motion_sensor_capability["properties"]
assert properties["retrievable"] is True
assert {"name": "detectionState"} in properties["supported"]
properties = await reported_properties(hass, "binary_sensor#test_motion_forced")
properties.assert_equal("Alexa.MotionSensor", "detectionState", "DETECTED")
properties.assert_equal("Alexa.EndpointHealth", "connectivity", {"value": "OK"})
async def test_doorbell_sensor(hass):
"""Test doorbell sensor discovery."""
device = (