Google Assistant: Add support for open/close binary sensors (#22674)

* Google Assistant: Add support for binary sensors

* Update test
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2019-04-03 10:20:56 -07:00
1 parent 3872ac9bf9
commit 14da2fd8c9
4 files changed
+207 -73

No files matched your search

@@ -9,7 +9,7 @@ from homeassistant.util.decorator import Registry
from homeassistant.core import callback
from homeassistant.const import (
CLOUD_NEVER_EXPOSED_ENTITIES, CONF_NAME, STATE_UNAVAILABLE,
ATTR_SUPPORTED_FEATURES, ATTR_ENTITY_ID,
ATTR_SUPPORTED_FEATURES, ATTR_ENTITY_ID, ATTR_DEVICE_CLASS,
)
from homeassistant.components import (
camera,
@@ -92,10 +92,11 @@ class _GoogleEntity:
state = self.state
domain = state.domain
features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
device_class = state.attributes.get(ATTR_DEVICE_CLASS)
self._traits = [Trait(self.hass, state, self.config)
for Trait in trait.TRAITS
if Trait.supported(domain, features)]
if Trait.supported(domain, features, device_class)]
return self._traits
async def sync_serialize(self):
@@ -2,6 +2,7 @@
import logging
from homeassistant.components import (
binary_sensor,
camera,
cover,
group,
@@ -127,7 +128,7 @@ class BrightnessTrait(_Trait):
]
@staticmethod
def supported(domain, features):
def supported(domain, features, device_class):
"""Test if state is supported."""
if domain == light.DOMAIN:
return features & light.SUPPORT_BRIGHTNESS
@@ -193,7 +194,7 @@ class CameraStreamTrait(_Trait):
stream_info = None
@staticmethod
def supported(domain, features):
def supported(domain, features, device_class):
"""Test if state is supported."""
if domain == camera.DOMAIN:
return features & camera.SUPPORT_STREAM
@@ -236,7 +237,7 @@ class OnOffTrait(_Trait):
]
@staticmethod
def supported(domain, features):
def supported(domain, features, device_class):
"""Test if state is supported."""
return domain in (
group.DOMAIN,
@@ -285,7 +286,7 @@ class ColorSpectrumTrait(_Trait):
]
@staticmethod
def supported(domain, features):
def supported(domain, features, device_class):
"""Test if state is supported."""
if domain != light.DOMAIN:
return False
@@ -341,7 +342,7 @@ class ColorTemperatureTrait(_Trait):
]
@staticmethod
def supported(domain, features):
def supported(domain, features, device_class):
"""Test if state is supported."""
if domain != light.DOMAIN:
return False
@@ -414,7 +415,7 @@ class SceneTrait(_Trait):
]
@staticmethod
def supported(domain, features):
def supported(domain, features, device_class):
"""Test if state is supported."""
return domain in (scene.DOMAIN, script.DOMAIN)
@@ -450,7 +451,7 @@ class DockTrait(_Trait):
]
@staticmethod
def supported(domain, features):
def supported(domain, features, device_class):
"""Test if state is supported."""
return domain == vacuum.DOMAIN
@@ -484,7 +485,7 @@ class StartStopTrait(_Trait):
]
@staticmethod
def supported(domain, features):
def supported(domain, features, device_class):
"""Test if state is supported."""
return domain == vacuum.DOMAIN
@@ -554,7 +555,7 @@ class TemperatureSettingTrait(_Trait):
google_to_hass = {value: key for key, value in hass_to_google.items()}
@staticmethod
def supported(domain, features):
def supported(domain, features, device_class):
"""Test if state is supported."""
if domain != climate.DOMAIN:
return False
@@ -739,7 +740,7 @@ class LockUnlockTrait(_Trait):
]
@staticmethod
def supported(domain, features):
def supported(domain, features, device_class):
"""Test if state is supported."""
return domain == lock.DOMAIN
@@ -790,7 +791,7 @@ class FanSpeedTrait(_Trait):
}
@staticmethod
def supported(domain, features):
def supported(domain, features, device_class):
"""Test if state is supported."""
if domain != fan.DOMAIN:
return False
@@ -941,7 +942,7 @@ class ModesTrait(_Trait):
}
@staticmethod
def supported(domain, features):
def supported(domain, features, device_class):
"""Test if state is supported."""
if domain != media_player.DOMAIN:
return False
@@ -1042,13 +1043,25 @@ class OpenCloseTrait(_Trait):
]
@staticmethod
def supported(domain, features):
def supported(domain, features, device_class):
"""Test if state is supported."""
return domain == cover.DOMAIN
if domain == cover.DOMAIN:
return True
return domain == binary_sensor.DOMAIN and device_class in (
binary_sensor.DEVICE_CLASS_DOOR,
binary_sensor.DEVICE_CLASS_GARAGE_DOOR,
binary_sensor.DEVICE_CLASS_LOCK,
binary_sensor.DEVICE_CLASS_OPENING,
binary_sensor.DEVICE_CLASS_WINDOW,
)
def sync_attributes(self):
"""Return opening direction."""
return {}
attrs = {}
if self.state.domain == binary_sensor.DOMAIN:
attrs['queryOnlyOpenClose'] = True
return attrs
def query_attributes(self):
"""Return state query attributes."""
@@ -1073,6 +1086,12 @@ class OpenCloseTrait(_Trait):
else:
response['openPercent'] = 0
elif domain == binary_sensor.DOMAIN:
if self.state.state == STATE_ON:
response['openPercent'] = 100
else:
response['openPercent'] = 0
return response
async def execute(self, command, data, params):