Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions homeassistant/components/sensor/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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,
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion tests/components/sensor/test_device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,6 +19,7 @@
async_get_device_automations,
async_get_device_automation_capabilities,
)
from tests.testing_config.custom_components.test.sensor import DEVICE_CLASSES


@pytest.fixture
Expand Down Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion tests/testing_config/custom_components/test/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@

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 = {}


Expand All @@ -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
}
Expand All @@ -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")