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
82ed84ba43
commit
3cedee3fea
5 files changed
+155
-2
No files matched your search
@@ -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),
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user