Add above and below to sensor condition extra_fields (#27364)
* Add above and below to sensor condition extra_fields * Change unit_of_measurement to suffix in extra_fields * Check if sensor has unit when getting capabilities * Improve tests
This commit is contained in:
1 parent
d6654eaecb
commit
475b43500a
5 files changed
+155
-2
No files matched your search
@@ -156,7 +156,11 @@ async def _async_get_device_automation_capabilities(hass, automation_type, autom
|
||||
# The device automation has no capabilities
|
||||
return {}
|
||||
|
||||
capabilities = await getattr(platform, function_name)(hass, automation)
|
||||
try:
|
||||
capabilities = await getattr(platform, function_name)(hass, automation)
|
||||
except InvalidDeviceAutomationConfig:
|
||||
return {}
|
||||
|
||||
capabilities = capabilities.copy()
|
||||
|
||||
extra_fields = capabilities.get("extra_fields")
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
from typing import Dict, List
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.device_automation.exceptions import (
|
||||
InvalidDeviceAutomationConfig,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
@@ -141,3 +144,27 @@ def async_condition_from_config(
|
||||
numeric_state_config[condition.CONF_BELOW] = config[CONF_BELOW]
|
||||
|
||||
return condition.async_numeric_state_from_config(numeric_state_config)
|
||||
|
||||
|
||||
async def async_get_condition_capabilities(hass, config):
|
||||
"""List condition capabilities."""
|
||||
state = hass.states.get(config[CONF_ENTITY_ID])
|
||||
unit_of_measurement = (
|
||||
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) if state else None
|
||||
)
|
||||
|
||||
if not state or not unit_of_measurement:
|
||||
raise InvalidDeviceAutomationConfig
|
||||
|
||||
return {
|
||||
"extra_fields": vol.Schema(
|
||||
{
|
||||
vol.Optional(
|
||||
CONF_ABOVE, description={"suffix": unit_of_measurement}
|
||||
): vol.Coerce(float),
|
||||
vol.Optional(
|
||||
CONF_BELOW, description={"suffix": unit_of_measurement}
|
||||
): vol.Coerce(float),
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -3,6 +3,9 @@ import voluptuous as vol
|
||||
|
||||
import homeassistant.components.automation.numeric_state as numeric_state_automation
|
||||
from homeassistant.components.device_automation import TRIGGER_BASE_SCHEMA
|
||||
from homeassistant.components.device_automation.exceptions import (
|
||||
InvalidDeviceAutomationConfig,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
@@ -146,9 +149,12 @@ async def async_get_trigger_capabilities(hass, config):
|
||||
"""List trigger capabilities."""
|
||||
state = hass.states.get(config[CONF_ENTITY_ID])
|
||||
unit_of_measurement = (
|
||||
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) if state else ""
|
||||
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) if state else None
|
||||
)
|
||||
|
||||
if not state or not unit_of_measurement:
|
||||
raise InvalidDeviceAutomationConfig
|
||||
|
||||
return {
|
||||
"extra_fields": vol.Schema(
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user