mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Only generate device trigger for sensor with unit (#27152)
This commit is contained in:
parent
cda7692f24
commit
89ebc17fb1
@ -5,6 +5,7 @@ import homeassistant.components.automation.numeric_state as numeric_state_automa
|
||||
from homeassistant.components.device_automation import TRIGGER_BASE_SCHEMA
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
CONF_ABOVE,
|
||||
CONF_BELOW,
|
||||
CONF_ENTITY_ID,
|
||||
@ -113,8 +114,14 @@ async def async_get_triggers(hass, device_id):
|
||||
for entry in entries:
|
||||
device_class = DEVICE_CLASS_NONE
|
||||
state = hass.states.get(entry.entity_id)
|
||||
if state:
|
||||
device_class = state.attributes.get(ATTR_DEVICE_CLASS)
|
||||
unit_of_measurement = (
|
||||
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) if state else None
|
||||
)
|
||||
|
||||
if not state or not unit_of_measurement:
|
||||
continue
|
||||
|
||||
device_class = state.attributes.get(ATTR_DEVICE_CLASS)
|
||||
|
||||
templates = ENTITY_TRIGGERS.get(
|
||||
device_class, ENTITY_TRIGGERS[DEVICE_CLASS_NONE]
|
||||
|
@ -2,7 +2,7 @@
|
||||
from datetime import timedelta
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.sensor import DOMAIN, DEVICE_CLASSES
|
||||
from homeassistant.components.sensor import DOMAIN
|
||||
from homeassistant.components.sensor.device_trigger import ENTITY_TRIGGERS
|
||||
from homeassistant.const import STATE_UNKNOWN, CONF_PLATFORM
|
||||
from homeassistant.setup import async_setup_component
|
||||
@ -19,6 +19,7 @@ from tests.common import (
|
||||
async_get_device_automations,
|
||||
async_get_device_automation_capabilities,
|
||||
)
|
||||
from tests.testing_config.custom_components.test.sensor import DEVICE_CLASSES
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@ -70,6 +71,7 @@ async def test_get_triggers(hass, device_reg, entity_reg):
|
||||
}
|
||||
for device_class in DEVICE_CLASSES
|
||||
for trigger in ENTITY_TRIGGERS[device_class]
|
||||
if device_class != "none"
|
||||
]
|
||||
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
|
||||
assert triggers == expected_triggers
|
||||
|
@ -3,10 +3,24 @@ Provide a mock sensor platform.
|
||||
|
||||
Call init before using it in your tests to ensure clean test data.
|
||||
"""
|
||||
from homeassistant.components.sensor import DEVICE_CLASSES
|
||||
import homeassistant.components.sensor as sensor
|
||||
from tests.common import MockEntity
|
||||
|
||||
|
||||
DEVICE_CLASSES = list(sensor.DEVICE_CLASSES)
|
||||
DEVICE_CLASSES.append("none")
|
||||
|
||||
UNITS_OF_MEASUREMENT = {
|
||||
sensor.DEVICE_CLASS_BATTERY: "%", # % of battery that is left
|
||||
sensor.DEVICE_CLASS_HUMIDITY: "%", # % of humidity in the air
|
||||
sensor.DEVICE_CLASS_ILLUMINANCE: "lm", # current light level (lx/lm)
|
||||
sensor.DEVICE_CLASS_SIGNAL_STRENGTH: "dB", # signal strength (dB/dBm)
|
||||
sensor.DEVICE_CLASS_TEMPERATURE: "C", # temperature (C/F)
|
||||
sensor.DEVICE_CLASS_TIMESTAMP: "hh:mm:ss", # timestamp (ISO8601)
|
||||
sensor.DEVICE_CLASS_PRESSURE: "hPa", # pressure (hPa/mbar)
|
||||
sensor.DEVICE_CLASS_POWER: "kW", # power (W/kW)
|
||||
}
|
||||
|
||||
ENTITIES = {}
|
||||
|
||||
|
||||
@ -22,6 +36,7 @@ def init(empty=False):
|
||||
name=f"{device_class} sensor",
|
||||
unique_id=f"unique_{device_class}",
|
||||
device_class=device_class,
|
||||
unit_of_measurement=UNITS_OF_MEASUREMENT.get(device_class),
|
||||
)
|
||||
for device_class in DEVICE_CLASSES
|
||||
}
|
||||
@ -42,3 +57,8 @@ class MockSensor(MockEntity):
|
||||
def device_class(self):
|
||||
"""Return the class of this sensor."""
|
||||
return self._handle("device_class")
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit_of_measurement of this sensor."""
|
||||
return self._handle("unit_of_measurement")
|
||||
|
Loading…
x
Reference in New Issue
Block a user