mirror of
https://github.com/home-assistant/core.git
synced 2026-03-25 08:48:19 +01:00
Compare commits
12 Commits
chore/bump
...
trigger_gu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b3c8fd7249 | ||
|
|
4fc68b0adf | ||
|
|
5bbf0d2dec | ||
|
|
7f453b56ad | ||
|
|
3616a52b37 | ||
|
|
0128372258 | ||
|
|
21863cd9d7 | ||
|
|
d67caec5c1 | ||
|
|
8286014ae1 | ||
|
|
1ff8d2279a | ||
|
|
5dcbc1d5d9 | ||
|
|
3068653cc7 |
2
CODEOWNERS
generated
2
CODEOWNERS
generated
@@ -1703,6 +1703,8 @@ build.json @home-assistant/supervisor
|
||||
/tests/components/tellduslive/ @fredrike
|
||||
/homeassistant/components/teltonika/ @karlbeecken
|
||||
/tests/components/teltonika/ @karlbeecken
|
||||
/homeassistant/components/temperature/ @home-assistant/core
|
||||
/tests/components/temperature/ @home-assistant/core
|
||||
/homeassistant/components/template/ @Petro31 @home-assistant/core
|
||||
/tests/components/template/ @Petro31 @home-assistant/core
|
||||
/homeassistant/components/tesla_fleet/ @Bre77
|
||||
|
||||
@@ -247,6 +247,7 @@ DEFAULT_INTEGRATIONS = {
|
||||
"humidity",
|
||||
"motion",
|
||||
"occupancy",
|
||||
"temperature",
|
||||
"window",
|
||||
}
|
||||
DEFAULT_INTEGRATIONS_RECOVERY_MODE = {
|
||||
|
||||
@@ -169,6 +169,7 @@ _EXPERIMENTAL_TRIGGER_PLATFORMS = {
|
||||
"select",
|
||||
"siren",
|
||||
"switch",
|
||||
"temperature",
|
||||
"text",
|
||||
"update",
|
||||
"vacuum",
|
||||
|
||||
@@ -21,6 +21,6 @@
|
||||
"bluetooth-auto-recovery==1.5.3",
|
||||
"bluetooth-data-tools==1.28.4",
|
||||
"dbus-fast==3.1.2",
|
||||
"habluetooth==5.10.2"
|
||||
"habluetooth==5.11.1"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -5,14 +5,14 @@ from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers.automation import DomainSpec
|
||||
from homeassistant.helpers.trigger import (
|
||||
ENTITY_STATE_TRIGGER_SCHEMA,
|
||||
EntityTriggerBase,
|
||||
StringEntityTriggerBase,
|
||||
Trigger,
|
||||
)
|
||||
|
||||
from . import DOMAIN
|
||||
|
||||
|
||||
class ButtonPressedTrigger(EntityTriggerBase):
|
||||
class ButtonPressedTrigger(StringEntityTriggerBase):
|
||||
"""Trigger for button entity presses."""
|
||||
|
||||
_domain_specs = {DOMAIN: DomainSpec()}
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN
|
||||
from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers.trigger import EntityTriggerBase, Trigger
|
||||
from homeassistant.helpers.trigger import StringEntityTriggerBase, Trigger
|
||||
|
||||
from .const import ATTR_IS_CLOSED, DOMAIN, CoverDeviceClass
|
||||
from .models import CoverDomainSpec
|
||||
|
||||
|
||||
class CoverTriggerBase(EntityTriggerBase[CoverDomainSpec]):
|
||||
class CoverTriggerBase(StringEntityTriggerBase[CoverDomainSpec]):
|
||||
"""Base trigger for cover state changes."""
|
||||
|
||||
def _get_value(self, state: State) -> str | bool | None:
|
||||
|
||||
@@ -617,8 +617,10 @@ class OvenProgramId(MieleEnum, missing_to_none=True):
|
||||
pyrolytic = 323
|
||||
descale = 326
|
||||
evaporate_water = 327
|
||||
rinse = 333
|
||||
shabbat_program = 335
|
||||
yom_tov = 336
|
||||
hydroclean = 341
|
||||
drying = 357, 2028
|
||||
heat_crockery = 358
|
||||
prove_dough = 359, 2023
|
||||
@@ -723,7 +725,7 @@ class OvenProgramId(MieleEnum, missing_to_none=True):
|
||||
belgian_sponge_cake = 624
|
||||
goose_unstuffed = 625
|
||||
rack_of_lamb_with_vegetables = 634
|
||||
yorkshire_pudding = 635
|
||||
yorkshire_pudding = 635, 2352
|
||||
meat_loaf = 636
|
||||
defrost_meat = 647
|
||||
defrost_vegetables = 654
|
||||
@@ -1123,7 +1125,7 @@ class OvenProgramId(MieleEnum, missing_to_none=True):
|
||||
wholegrain_rice = 3376
|
||||
parboiled_rice_steam_cooking = 3380
|
||||
parboiled_rice_rapid_steam_cooking = 3381
|
||||
basmati_rice_steam_cooking = 3383
|
||||
basmati_rice_steam_cooking = 3382, 3383
|
||||
basmati_rice_rapid_steam_cooking = 3384
|
||||
jasmine_rice_steam_cooking = 3386
|
||||
jasmine_rice_rapid_steam_cooking = 3387
|
||||
@@ -1131,7 +1133,7 @@ class OvenProgramId(MieleEnum, missing_to_none=True):
|
||||
huanghuanian_rapid_steam_cooking = 3390
|
||||
simiao_steam_cooking = 3392
|
||||
simiao_rapid_steam_cooking = 3393
|
||||
long_grain_rice_general_steam_cooking = 3395
|
||||
long_grain_rice_general_steam_cooking = 3394, 3395
|
||||
long_grain_rice_general_rapid_steam_cooking = 3396
|
||||
chongming_steam_cooking = 3398
|
||||
chongming_rapid_steam_cooking = 3399
|
||||
|
||||
@@ -560,6 +560,7 @@
|
||||
"hot_water": "Hot water",
|
||||
"huanghuanian_rapid_steam_cooking": "Huanghuanian (rapid steam cooking)",
|
||||
"huanghuanian_steam_cooking": "Huanghuanian (steam cooking)",
|
||||
"hydroclean": "HydroClean",
|
||||
"hygiene": "Hygiene",
|
||||
"intensive": "Intensive",
|
||||
"intensive_bake": "Intensive bake",
|
||||
|
||||
@@ -13,5 +13,5 @@
|
||||
"integration_type": "device",
|
||||
"iot_class": "local_push",
|
||||
"loggers": ["oralb_ble"],
|
||||
"requirements": ["oralb-ble==1.0.2"]
|
||||
"requirements": ["oralb-ble==1.1.0"]
|
||||
}
|
||||
|
||||
@@ -20,12 +20,18 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import (
|
||||
RoborockB01Q10UpdateCoordinator,
|
||||
RoborockConfigEntry,
|
||||
RoborockDataUpdateCoordinator,
|
||||
RoborockDataUpdateCoordinatorA01,
|
||||
RoborockWashingMachineUpdateCoordinator,
|
||||
)
|
||||
from .entity import RoborockCoordinatedEntityA01, RoborockEntity, RoborockEntityV1
|
||||
from .entity import (
|
||||
RoborockCoordinatedEntityA01,
|
||||
RoborockCoordinatedEntityB01Q10,
|
||||
RoborockEntity,
|
||||
RoborockEntityV1,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -97,6 +103,14 @@ ZEO_BUTTON_DESCRIPTIONS = [
|
||||
]
|
||||
|
||||
|
||||
Q10_BUTTON_DESCRIPTIONS = [
|
||||
ButtonEntityDescription(
|
||||
key="empty_dustbin",
|
||||
translation_key="empty_dustbin",
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: RoborockConfigEntry,
|
||||
@@ -139,6 +153,15 @@ async def async_setup_entry(
|
||||
if isinstance(coordinator, RoborockWashingMachineUpdateCoordinator)
|
||||
for description in ZEO_BUTTON_DESCRIPTIONS
|
||||
),
|
||||
(
|
||||
RoborockQ10EmptyDustbinButtonEntity(
|
||||
coordinator,
|
||||
description,
|
||||
)
|
||||
for coordinator in config_entry.runtime_data.b01_q10
|
||||
if isinstance(coordinator, RoborockB01Q10UpdateCoordinator)
|
||||
for description in Q10_BUTTON_DESCRIPTIONS
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
@@ -233,3 +256,37 @@ class RoborockButtonEntityA01(RoborockCoordinatedEntityA01, ButtonEntity):
|
||||
) from err
|
||||
finally:
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
|
||||
class RoborockQ10EmptyDustbinButtonEntity(
|
||||
RoborockCoordinatedEntityB01Q10, ButtonEntity
|
||||
):
|
||||
"""A class to define Q10 empty dustbin button entity."""
|
||||
|
||||
entity_description: ButtonEntityDescription
|
||||
coordinator: RoborockB01Q10UpdateCoordinator
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: RoborockB01Q10UpdateCoordinator,
|
||||
entity_description: ButtonEntityDescription,
|
||||
) -> None:
|
||||
"""Create a Q10 empty dustbin button entity."""
|
||||
self.entity_description = entity_description
|
||||
super().__init__(
|
||||
f"{entity_description.key}_{coordinator.duid_slug}",
|
||||
coordinator,
|
||||
)
|
||||
|
||||
async def async_press(self, **kwargs: Any) -> None:
|
||||
"""Press the button to empty dustbin."""
|
||||
try:
|
||||
await self.coordinator.api.vacuum.empty_dustbin()
|
||||
except RoborockException as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="command_failed",
|
||||
translation_placeholders={
|
||||
"command": "empty_dustbin",
|
||||
},
|
||||
) from err
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
"loggers": ["roborock"],
|
||||
"quality_scale": "silver",
|
||||
"requirements": [
|
||||
"python-roborock==4.26.1",
|
||||
"python-roborock==4.26.3",
|
||||
"vacuum-map-parser-roborock==0.1.4"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -84,6 +84,9 @@
|
||||
}
|
||||
},
|
||||
"button": {
|
||||
"empty_dustbin": {
|
||||
"name": "Empty dustbin"
|
||||
},
|
||||
"pause": {
|
||||
"name": "Pause"
|
||||
},
|
||||
|
||||
@@ -5,14 +5,14 @@ from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers.automation import DomainSpec
|
||||
from homeassistant.helpers.trigger import (
|
||||
ENTITY_STATE_TRIGGER_SCHEMA,
|
||||
EntityTriggerBase,
|
||||
StringEntityTriggerBase,
|
||||
Trigger,
|
||||
)
|
||||
|
||||
from . import DOMAIN
|
||||
|
||||
|
||||
class SceneActivatedTrigger(EntityTriggerBase):
|
||||
class SceneActivatedTrigger(StringEntityTriggerBase):
|
||||
"""Trigger for scene entity activations."""
|
||||
|
||||
_domain_specs = {DOMAIN: DomainSpec()}
|
||||
|
||||
@@ -6,14 +6,14 @@ from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers.automation import DomainSpec
|
||||
from homeassistant.helpers.trigger import (
|
||||
ENTITY_STATE_TRIGGER_SCHEMA,
|
||||
EntityTriggerBase,
|
||||
StringEntityTriggerBase,
|
||||
Trigger,
|
||||
)
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
|
||||
class SelectionChangedTrigger(EntityTriggerBase):
|
||||
class SelectionChangedTrigger(StringEntityTriggerBase):
|
||||
"""Trigger for select entity when its selection changes."""
|
||||
|
||||
_domain_specs = {DOMAIN: DomainSpec(), INPUT_SELECT_DOMAIN: DomainSpec()}
|
||||
|
||||
17
homeassistant/components/temperature/__init__.py
Normal file
17
homeassistant/components/temperature/__init__.py
Normal file
@@ -0,0 +1,17 @@
|
||||
"""Integration for temperature triggers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
DOMAIN = "temperature"
|
||||
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
|
||||
|
||||
__all__ = []
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up the component."""
|
||||
return True
|
||||
10
homeassistant/components/temperature/icons.json
Normal file
10
homeassistant/components/temperature/icons.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"triggers": {
|
||||
"changed": {
|
||||
"trigger": "mdi:thermometer"
|
||||
},
|
||||
"crossed_threshold": {
|
||||
"trigger": "mdi:thermometer"
|
||||
}
|
||||
}
|
||||
}
|
||||
8
homeassistant/components/temperature/manifest.json
Normal file
8
homeassistant/components/temperature/manifest.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"domain": "temperature",
|
||||
"name": "Temperature",
|
||||
"codeowners": ["@home-assistant/core"],
|
||||
"documentation": "https://www.home-assistant.io/integrations/temperature",
|
||||
"integration_type": "system",
|
||||
"quality_scale": "internal"
|
||||
}
|
||||
76
homeassistant/components/temperature/strings.json
Normal file
76
homeassistant/components/temperature/strings.json
Normal file
@@ -0,0 +1,76 @@
|
||||
{
|
||||
"common": {
|
||||
"trigger_behavior_description": "The behavior of the targeted entities to trigger on.",
|
||||
"trigger_behavior_name": "Behavior"
|
||||
},
|
||||
"selector": {
|
||||
"number_or_entity": {
|
||||
"choices": {
|
||||
"entity": "Entity",
|
||||
"number": "Number"
|
||||
}
|
||||
},
|
||||
"trigger_behavior": {
|
||||
"options": {
|
||||
"any": "Any",
|
||||
"first": "First",
|
||||
"last": "Last"
|
||||
}
|
||||
},
|
||||
"trigger_threshold_type": {
|
||||
"options": {
|
||||
"above": "Above",
|
||||
"below": "Below",
|
||||
"between": "Between",
|
||||
"outside": "Outside"
|
||||
}
|
||||
}
|
||||
},
|
||||
"title": "Temperature",
|
||||
"triggers": {
|
||||
"changed": {
|
||||
"description": "Triggers when the temperature changes.",
|
||||
"fields": {
|
||||
"above": {
|
||||
"description": "Only trigger when temperature is above this value.",
|
||||
"name": "Above"
|
||||
},
|
||||
"below": {
|
||||
"description": "Only trigger when temperature is below this value.",
|
||||
"name": "Below"
|
||||
},
|
||||
"unit": {
|
||||
"description": "All values will be converted to this unit when evaluating the trigger.",
|
||||
"name": "Unit of measurement"
|
||||
}
|
||||
},
|
||||
"name": "Temperature changed"
|
||||
},
|
||||
"crossed_threshold": {
|
||||
"description": "Triggers when the temperature crosses a threshold.",
|
||||
"fields": {
|
||||
"behavior": {
|
||||
"description": "[%key:component::temperature::common::trigger_behavior_description%]",
|
||||
"name": "[%key:component::temperature::common::trigger_behavior_name%]"
|
||||
},
|
||||
"lower_limit": {
|
||||
"description": "The lower limit of the threshold.",
|
||||
"name": "Lower limit"
|
||||
},
|
||||
"threshold_type": {
|
||||
"description": "The type of threshold to use.",
|
||||
"name": "Threshold type"
|
||||
},
|
||||
"unit": {
|
||||
"description": "[%key:component::temperature::triggers::changed::fields::unit::description%]",
|
||||
"name": "[%key:component::temperature::triggers::changed::fields::unit::name%]"
|
||||
},
|
||||
"upper_limit": {
|
||||
"description": "The upper limit of the threshold.",
|
||||
"name": "Upper limit"
|
||||
}
|
||||
},
|
||||
"name": "Temperature crossed threshold"
|
||||
}
|
||||
}
|
||||
}
|
||||
83
homeassistant/components/temperature/trigger.py
Normal file
83
homeassistant/components/temperature/trigger.py
Normal file
@@ -0,0 +1,83 @@
|
||||
"""Provides triggers for temperature."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.components.climate import (
|
||||
ATTR_CURRENT_TEMPERATURE as CLIMATE_ATTR_CURRENT_TEMPERATURE,
|
||||
DOMAIN as CLIMATE_DOMAIN,
|
||||
)
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorDeviceClass
|
||||
from homeassistant.components.water_heater import (
|
||||
ATTR_CURRENT_TEMPERATURE as WATER_HEATER_ATTR_CURRENT_TEMPERATURE,
|
||||
DOMAIN as WATER_HEATER_DOMAIN,
|
||||
)
|
||||
from homeassistant.components.weather import (
|
||||
ATTR_WEATHER_TEMPERATURE,
|
||||
ATTR_WEATHER_TEMPERATURE_UNIT,
|
||||
DOMAIN as WEATHER_DOMAIN,
|
||||
)
|
||||
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, UnitOfTemperature
|
||||
from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers.automation import NumericalDomainSpec
|
||||
from homeassistant.helpers.trigger import (
|
||||
EntityNumericalStateChangedTriggerWithUnitBase,
|
||||
EntityNumericalStateCrossedThresholdTriggerWithUnitBase,
|
||||
EntityNumericalStateTriggerWithUnitBase,
|
||||
Trigger,
|
||||
)
|
||||
from homeassistant.util.unit_conversion import TemperatureConverter
|
||||
|
||||
TEMPERATURE_DOMAIN_SPECS = {
|
||||
CLIMATE_DOMAIN: NumericalDomainSpec(
|
||||
value_source=CLIMATE_ATTR_CURRENT_TEMPERATURE,
|
||||
),
|
||||
SENSOR_DOMAIN: NumericalDomainSpec(
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
),
|
||||
WATER_HEATER_DOMAIN: NumericalDomainSpec(
|
||||
value_source=WATER_HEATER_ATTR_CURRENT_TEMPERATURE
|
||||
),
|
||||
WEATHER_DOMAIN: NumericalDomainSpec(
|
||||
value_source=ATTR_WEATHER_TEMPERATURE,
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
class _TemperatureTriggerMixin(EntityNumericalStateTriggerWithUnitBase):
|
||||
"""Mixin for temperature triggers providing entity filtering, value extraction, and unit conversion."""
|
||||
|
||||
_base_unit = UnitOfTemperature.CELSIUS
|
||||
_domain_specs = TEMPERATURE_DOMAIN_SPECS
|
||||
_unit_converter = TemperatureConverter
|
||||
|
||||
def _get_entity_unit(self, state: State) -> str | None:
|
||||
"""Get the temperature unit of an entity from its state."""
|
||||
if state.domain == SENSOR_DOMAIN:
|
||||
return state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||
if state.domain == WEATHER_DOMAIN:
|
||||
return state.attributes.get(ATTR_WEATHER_TEMPERATURE_UNIT)
|
||||
# Climate and water_heater: show_temp converts to system unit
|
||||
return self._hass.config.units.temperature_unit
|
||||
|
||||
|
||||
class TemperatureChangedTrigger(
|
||||
_TemperatureTriggerMixin, EntityNumericalStateChangedTriggerWithUnitBase
|
||||
):
|
||||
"""Trigger for temperature value changes across multiple domains."""
|
||||
|
||||
|
||||
class TemperatureCrossedThresholdTrigger(
|
||||
_TemperatureTriggerMixin, EntityNumericalStateCrossedThresholdTriggerWithUnitBase
|
||||
):
|
||||
"""Trigger for temperature value crossing a threshold across multiple domains."""
|
||||
|
||||
|
||||
TRIGGERS: dict[str, type[Trigger]] = {
|
||||
"changed": TemperatureChangedTrigger,
|
||||
"crossed_threshold": TemperatureCrossedThresholdTrigger,
|
||||
}
|
||||
|
||||
|
||||
async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]:
|
||||
"""Return the triggers for temperature."""
|
||||
return TRIGGERS
|
||||
77
homeassistant/components/temperature/triggers.yaml
Normal file
77
homeassistant/components/temperature/triggers.yaml
Normal file
@@ -0,0 +1,77 @@
|
||||
.trigger_common_fields:
|
||||
behavior: &trigger_behavior
|
||||
required: true
|
||||
default: any
|
||||
selector:
|
||||
select:
|
||||
translation_key: trigger_behavior
|
||||
options:
|
||||
- first
|
||||
- last
|
||||
- any
|
||||
|
||||
.number_or_entity: &number_or_entity
|
||||
required: false
|
||||
selector:
|
||||
choose:
|
||||
choices:
|
||||
number:
|
||||
selector:
|
||||
number:
|
||||
mode: box
|
||||
entity:
|
||||
selector:
|
||||
entity:
|
||||
filter:
|
||||
- domain: input_number
|
||||
unit_of_measurement:
|
||||
- "°C"
|
||||
- "°F"
|
||||
- domain: sensor
|
||||
device_class: temperature
|
||||
- domain: number
|
||||
device_class: temperature
|
||||
translation_key: number_or_entity
|
||||
|
||||
.trigger_threshold_type: &trigger_threshold_type
|
||||
required: true
|
||||
default: above
|
||||
selector:
|
||||
select:
|
||||
options:
|
||||
- above
|
||||
- below
|
||||
- between
|
||||
- outside
|
||||
translation_key: trigger_threshold_type
|
||||
|
||||
.trigger_unit: &trigger_unit
|
||||
required: false
|
||||
selector:
|
||||
select:
|
||||
options:
|
||||
- "°C"
|
||||
- "°F"
|
||||
.trigger_target: &trigger_target
|
||||
entity:
|
||||
- domain: sensor
|
||||
device_class: temperature
|
||||
- domain: climate
|
||||
- domain: water_heater
|
||||
- domain: weather
|
||||
|
||||
changed:
|
||||
target: *trigger_target
|
||||
fields:
|
||||
above: *number_or_entity
|
||||
below: *number_or_entity
|
||||
unit: *trigger_unit
|
||||
|
||||
crossed_threshold:
|
||||
target: *trigger_target
|
||||
fields:
|
||||
behavior: *trigger_behavior
|
||||
threshold_type: *trigger_threshold_type
|
||||
lower_limit: *number_or_entity
|
||||
upper_limit: *number_or_entity
|
||||
unit: *trigger_unit
|
||||
@@ -5,14 +5,14 @@ from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers.automation import DomainSpec
|
||||
from homeassistant.helpers.trigger import (
|
||||
ENTITY_STATE_TRIGGER_SCHEMA,
|
||||
EntityTriggerBase,
|
||||
StringEntityTriggerBase,
|
||||
Trigger,
|
||||
)
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
|
||||
class TextChangedTrigger(EntityTriggerBase):
|
||||
class TextChangedTrigger(StringEntityTriggerBase):
|
||||
"""Trigger for text entity when its content changes."""
|
||||
|
||||
_domain_specs = {DOMAIN: DomainSpec()}
|
||||
|
||||
@@ -7,5 +7,5 @@
|
||||
"integration_type": "device",
|
||||
"iot_class": "cloud_polling",
|
||||
"loggers": ["wolf_comm"],
|
||||
"requirements": ["wolf-comm==0.0.23"]
|
||||
"requirements": ["wolf-comm==0.0.48"]
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ class DomainSpec:
|
||||
class NumericalDomainSpec(DomainSpec):
|
||||
"""DomainSpec with an optional value converter for numerical triggers."""
|
||||
|
||||
value_converter: Callable[[Any], float] | None = None
|
||||
value_converter: Callable[[float], float] | None = None
|
||||
"""Optional converter for numerical values (e.g. uint8 → percentage)."""
|
||||
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
CONF_ABOVE,
|
||||
CONF_ALIAS,
|
||||
CONF_BELOW,
|
||||
@@ -64,6 +65,7 @@ from homeassistant.loader import (
|
||||
)
|
||||
from homeassistant.util.async_ import create_eager_task
|
||||
from homeassistant.util.hass_dict import HassKey
|
||||
from homeassistant.util.unit_conversion import BaseUnitConverter
|
||||
from homeassistant.util.yaml import load_yaml_dict
|
||||
|
||||
from . import config_validation as cv, selector
|
||||
@@ -361,13 +363,6 @@ class EntityTriggerBase[DomainSpecT: DomainSpec = DomainSpec](Trigger):
|
||||
"""Filter entities matching any of the domain specs."""
|
||||
return filter_by_domain_specs(self._hass, self._domain_specs, entities)
|
||||
|
||||
def _get_tracked_value(self, state: State) -> Any:
|
||||
"""Get the tracked value from a state based on the DomainSpec."""
|
||||
domain_spec = self._domain_specs[state.domain]
|
||||
if domain_spec.value_source is None:
|
||||
return state.state
|
||||
return state.attributes.get(domain_spec.value_source)
|
||||
|
||||
@abc.abstractmethod
|
||||
def is_valid_transition(self, from_state: State, to_state: State) -> bool:
|
||||
"""Check if the origin state is valid and the state has changed."""
|
||||
@@ -450,7 +445,23 @@ class EntityTriggerBase[DomainSpecT: DomainSpec = DomainSpec](Trigger):
|
||||
)
|
||||
|
||||
|
||||
class EntityTargetStateTriggerBase(EntityTriggerBase):
|
||||
class StringEntityTriggerBase[DomainSpecT: DomainSpec = DomainSpec](
|
||||
EntityTriggerBase[DomainSpecT]
|
||||
):
|
||||
"""Trigger for string based entity state changes."""
|
||||
|
||||
def _get_tracked_value(self, state: State) -> str | None:
|
||||
"""Get the tracked value from a state based on the DomainSpec."""
|
||||
domain_spec = self._domain_specs[state.domain]
|
||||
if domain_spec.value_source is None:
|
||||
return state.state
|
||||
value = state.attributes.get(domain_spec.value_source)
|
||||
if not isinstance(value, str):
|
||||
return None
|
||||
return value
|
||||
|
||||
|
||||
class EntityTargetStateTriggerBase(StringEntityTriggerBase):
|
||||
"""Trigger for entity state changes to a specific state.
|
||||
|
||||
Uses _get_tracked_value to extract the value, so it works for both
|
||||
@@ -475,7 +486,7 @@ class EntityTargetStateTriggerBase(EntityTriggerBase):
|
||||
return self._get_tracked_value(state) in self._to_states
|
||||
|
||||
|
||||
class EntityTransitionTriggerBase(EntityTriggerBase):
|
||||
class EntityTransitionTriggerBase(StringEntityTriggerBase):
|
||||
"""Trigger for entity state changes between specific states."""
|
||||
|
||||
_from_states: set[str]
|
||||
@@ -497,7 +508,7 @@ class EntityTransitionTriggerBase(EntityTriggerBase):
|
||||
return self._get_tracked_value(state) in self._to_states
|
||||
|
||||
|
||||
class EntityOriginStateTriggerBase(EntityTriggerBase):
|
||||
class EntityOriginStateTriggerBase(StringEntityTriggerBase):
|
||||
"""Trigger for entity state changes from a specific state."""
|
||||
|
||||
_from_state: str
|
||||
@@ -519,7 +530,7 @@ def _validate_range[_T: dict[str, Any]](
|
||||
) -> Callable[[_T], _T]:
|
||||
"""Generate range validator."""
|
||||
|
||||
def _validate_range(value: _T) -> _T:
|
||||
def _validate_range_impl(value: _T) -> _T:
|
||||
above = value.get(lower_limit)
|
||||
below = value.get(upper_limit)
|
||||
|
||||
@@ -539,7 +550,28 @@ def _validate_range[_T: dict[str, Any]](
|
||||
|
||||
return value
|
||||
|
||||
return _validate_range
|
||||
return _validate_range_impl
|
||||
|
||||
|
||||
CONF_UNIT: Final = "unit"
|
||||
|
||||
|
||||
def _validate_unit_set_if_range_numerical[_T: dict[str, Any]](
|
||||
lower_limit: str, upper_limit: str
|
||||
) -> Callable[[_T], _T]:
|
||||
"""Validate that unit is set if upper or lower limit is numerical."""
|
||||
|
||||
def _validate_unit_set_if_range_numerical_impl(options: _T) -> _T:
|
||||
if (
|
||||
any(
|
||||
opt in options and not isinstance(options[opt], str)
|
||||
for opt in (lower_limit, upper_limit)
|
||||
)
|
||||
) and options.get(CONF_UNIT) is None:
|
||||
raise vol.Invalid("Unit must be specified when using numerical thresholds.")
|
||||
return options
|
||||
|
||||
return _validate_unit_set_if_range_numerical_impl
|
||||
|
||||
|
||||
_NUMBER_OR_ENTITY_CHOOSE_SCHEMA = vol.Schema(
|
||||
@@ -576,38 +608,107 @@ NUMERICAL_ATTRIBUTE_CHANGED_TRIGGER_SCHEMA = ENTITY_STATE_TRIGGER_SCHEMA.extend(
|
||||
)
|
||||
|
||||
|
||||
def _get_numerical_value(
|
||||
hass: HomeAssistant, entity_or_float: float | str
|
||||
) -> float | None:
|
||||
"""Get numerical value from float or entity state."""
|
||||
if isinstance(entity_or_float, str):
|
||||
if not (state := hass.states.get(entity_or_float)):
|
||||
# Entity not found
|
||||
return None
|
||||
try:
|
||||
return float(state.state)
|
||||
except TypeError, ValueError:
|
||||
# Entity state is not a valid number
|
||||
return None
|
||||
return entity_or_float
|
||||
|
||||
|
||||
class EntityNumericalStateTriggerBase(EntityTriggerBase[NumericalDomainSpec]):
|
||||
"""Base class for numerical state and state attribute triggers."""
|
||||
|
||||
def _get_tracked_value(self, state: State) -> Any:
|
||||
def _get_numerical_value(self, entity_or_float: float | str) -> float | None:
|
||||
"""Get numerical value from float or entity state."""
|
||||
if isinstance(entity_or_float, str):
|
||||
if not (state := self._hass.states.get(entity_or_float)):
|
||||
# Entity not found
|
||||
return None
|
||||
try:
|
||||
return float(state.state)
|
||||
except TypeError, ValueError:
|
||||
# Entity state is not a valid number
|
||||
return None
|
||||
return entity_or_float
|
||||
|
||||
def _get_tracked_value(self, state: State) -> float | None:
|
||||
"""Get the tracked numerical value from a state."""
|
||||
domain_spec = self._domain_specs[state.domain]
|
||||
raw_value: Any
|
||||
if domain_spec.value_source is None:
|
||||
return state.state
|
||||
return state.attributes.get(domain_spec.value_source)
|
||||
raw_value = state.state
|
||||
else:
|
||||
raw_value = state.attributes.get(domain_spec.value_source)
|
||||
|
||||
def _get_converter(self, state: State) -> Callable[[Any], float]:
|
||||
try:
|
||||
return float(raw_value)
|
||||
except TypeError, ValueError:
|
||||
# Entity state is not a valid number
|
||||
return None
|
||||
|
||||
def _get_converter(self, state: State) -> Callable[[float], float]:
|
||||
"""Get the value converter for an entity."""
|
||||
domain_spec = self._domain_specs[state.domain]
|
||||
if domain_spec.value_converter is not None:
|
||||
return domain_spec.value_converter
|
||||
return float
|
||||
return lambda x: x
|
||||
|
||||
|
||||
class EntityNumericalStateTriggerWithUnitBase(EntityNumericalStateTriggerBase):
|
||||
"""Base class for numerical state and state attribute triggers."""
|
||||
|
||||
_base_unit: str # Base unit for the tracked value
|
||||
_manual_limit_unit: str | None # Unit of above/below limits when numbers
|
||||
_unit_converter: type[BaseUnitConverter]
|
||||
|
||||
def __init__(self, hass: HomeAssistant, config: TriggerConfig) -> None:
|
||||
"""Initialize the trigger."""
|
||||
super().__init__(hass, config)
|
||||
self._manual_limit_unit = self._options.get(CONF_UNIT)
|
||||
|
||||
def _get_entity_unit(self, state: State) -> str | None:
|
||||
"""Get the unit of an entity from its state."""
|
||||
return state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||
|
||||
def _get_numerical_value(self, entity_or_float: float | str) -> float | None:
|
||||
"""Get numerical value from float or entity state."""
|
||||
if isinstance(entity_or_float, (int, float)):
|
||||
return self._unit_converter.convert(
|
||||
entity_or_float, self._manual_limit_unit, self._base_unit
|
||||
)
|
||||
|
||||
if not (state := self._hass.states.get(entity_or_float)):
|
||||
# Entity not found
|
||||
return None
|
||||
try:
|
||||
value = float(state.state)
|
||||
except TypeError, ValueError:
|
||||
# Entity state is not a valid number
|
||||
return None
|
||||
|
||||
try:
|
||||
return self._unit_converter.convert(
|
||||
value, state.attributes.get(ATTR_UNIT_OF_MEASUREMENT), self._base_unit
|
||||
)
|
||||
except HomeAssistantError:
|
||||
# Unit conversion failed (i.e. incompatible units), treat as invalid number
|
||||
return None
|
||||
|
||||
def _get_tracked_value(self, state: State) -> float | None:
|
||||
"""Get the tracked numerical value from a state."""
|
||||
domain_spec = self._domain_specs[state.domain]
|
||||
raw_value: Any
|
||||
if domain_spec.value_source is None:
|
||||
raw_value = state.state
|
||||
else:
|
||||
raw_value = state.attributes.get(domain_spec.value_source)
|
||||
|
||||
try:
|
||||
value = float(raw_value)
|
||||
except TypeError, ValueError:
|
||||
# Entity state is not a valid number
|
||||
return None
|
||||
|
||||
try:
|
||||
return self._unit_converter.convert(
|
||||
value, self._get_entity_unit(state), self._base_unit
|
||||
)
|
||||
except HomeAssistantError:
|
||||
# Unit conversion failed (i.e. incompatible units), treat as invalid number
|
||||
return None
|
||||
|
||||
|
||||
class EntityNumericalStateChangedTriggerBase(EntityNumericalStateTriggerBase):
|
||||
@@ -629,7 +730,7 @@ class EntityNumericalStateChangedTriggerBase(EntityNumericalStateTriggerBase):
|
||||
if from_state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN):
|
||||
return False
|
||||
|
||||
return self._get_tracked_value(from_state) != self._get_tracked_value(to_state) # type: ignore[no-any-return]
|
||||
return self._get_tracked_value(from_state) != self._get_tracked_value(to_state)
|
||||
|
||||
def is_valid_state(self, state: State) -> bool:
|
||||
"""Check if the new state or state attribute matches the expected one."""
|
||||
@@ -637,14 +738,10 @@ class EntityNumericalStateChangedTriggerBase(EntityNumericalStateTriggerBase):
|
||||
if (_attribute_value := self._get_tracked_value(state)) is None:
|
||||
return False
|
||||
|
||||
try:
|
||||
current_value = self._get_converter(state)(_attribute_value)
|
||||
except TypeError, ValueError:
|
||||
# Value is not a valid number, don't trigger
|
||||
return False
|
||||
current_value = self._get_converter(state)(_attribute_value)
|
||||
|
||||
if self._above is not None:
|
||||
if (above := _get_numerical_value(self._hass, self._above)) is None:
|
||||
if (above := self._get_numerical_value(self._above)) is None:
|
||||
# Entity not found or invalid number, don't trigger
|
||||
return False
|
||||
if current_value <= above:
|
||||
@@ -652,7 +749,7 @@ class EntityNumericalStateChangedTriggerBase(EntityNumericalStateTriggerBase):
|
||||
return False
|
||||
|
||||
if self._below is not None:
|
||||
if (below := _get_numerical_value(self._hass, self._below)) is None:
|
||||
if (below := self._get_numerical_value(self._below)) is None:
|
||||
# Entity not found or invalid number, don't trigger
|
||||
return False
|
||||
if current_value >= below:
|
||||
@@ -662,6 +759,37 @@ class EntityNumericalStateChangedTriggerBase(EntityNumericalStateTriggerBase):
|
||||
return True
|
||||
|
||||
|
||||
def make_numerical_state_changed_with_unit_schema(
|
||||
unit_converter: type[BaseUnitConverter],
|
||||
) -> vol.Schema:
|
||||
"""Factory for numerical state trigger schema with unit option."""
|
||||
return ENTITY_STATE_TRIGGER_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_OPTIONS, default={}): vol.All(
|
||||
{
|
||||
vol.Optional(CONF_ABOVE): _number_or_entity,
|
||||
vol.Optional(CONF_BELOW): _number_or_entity,
|
||||
vol.Optional(CONF_UNIT): vol.In(unit_converter.VALID_UNITS),
|
||||
},
|
||||
_validate_range(CONF_ABOVE, CONF_BELOW),
|
||||
_validate_unit_set_if_range_numerical(CONF_ABOVE, CONF_BELOW),
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class EntityNumericalStateChangedTriggerWithUnitBase(
|
||||
EntityNumericalStateChangedTriggerBase,
|
||||
EntityNumericalStateTriggerWithUnitBase,
|
||||
):
|
||||
"""Trigger for numerical state and state attribute changes."""
|
||||
|
||||
def __init_subclass__(cls, **kwargs: Any) -> None:
|
||||
"""Create a schema."""
|
||||
super().__init_subclass__(**kwargs)
|
||||
cls._schema = make_numerical_state_changed_with_unit_schema(cls._unit_converter)
|
||||
|
||||
|
||||
CONF_LOWER_LIMIT = "lower_limit"
|
||||
CONF_UPPER_LIMIT = "upper_limit"
|
||||
CONF_THRESHOLD_TYPE = "threshold_type"
|
||||
@@ -744,16 +872,12 @@ class EntityNumericalStateCrossedThresholdTriggerBase(EntityNumericalStateTrigge
|
||||
def is_valid_state(self, state: State) -> bool:
|
||||
"""Check if the new state attribute matches the expected one."""
|
||||
if self._lower_limit is not None:
|
||||
if (
|
||||
lower_limit := _get_numerical_value(self._hass, self._lower_limit)
|
||||
) is None:
|
||||
if (lower_limit := self._get_numerical_value(self._lower_limit)) is None:
|
||||
# Entity not found or invalid number, don't trigger
|
||||
return False
|
||||
|
||||
if self._upper_limit is not None:
|
||||
if (
|
||||
upper_limit := _get_numerical_value(self._hass, self._upper_limit)
|
||||
) is None:
|
||||
if (upper_limit := self._get_numerical_value(self._upper_limit)) is None:
|
||||
# Entity not found or invalid number, don't trigger
|
||||
return False
|
||||
|
||||
@@ -761,11 +885,7 @@ class EntityNumericalStateCrossedThresholdTriggerBase(EntityNumericalStateTrigge
|
||||
if (_attribute_value := self._get_tracked_value(state)) is None:
|
||||
return False
|
||||
|
||||
try:
|
||||
current_value = self._get_converter(state)(_attribute_value)
|
||||
except TypeError, ValueError:
|
||||
# Value is not a valid number, don't trigger
|
||||
return False
|
||||
current_value = self._get_converter(state)(_attribute_value)
|
||||
|
||||
# Note: We do not need to check for lower_limit/upper_limit being None here
|
||||
# because of the validation done in the schema.
|
||||
@@ -781,6 +901,50 @@ class EntityNumericalStateCrossedThresholdTriggerBase(EntityNumericalStateTrigge
|
||||
return not between
|
||||
|
||||
|
||||
def make_numerical_state_crossed_threshold_with_unit_schema(
|
||||
unit_converter: type[BaseUnitConverter],
|
||||
) -> vol.Schema:
|
||||
"""Trigger for numerical state and state attribute changes.
|
||||
|
||||
This trigger only fires when the observed attribute changes from not within to within
|
||||
the defined threshold.
|
||||
"""
|
||||
return ENTITY_STATE_TRIGGER_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_OPTIONS, default={}): vol.All(
|
||||
{
|
||||
vol.Required(ATTR_BEHAVIOR, default=BEHAVIOR_ANY): vol.In(
|
||||
[BEHAVIOR_FIRST, BEHAVIOR_LAST, BEHAVIOR_ANY]
|
||||
),
|
||||
vol.Optional(CONF_LOWER_LIMIT): _number_or_entity,
|
||||
vol.Optional(CONF_UPPER_LIMIT): _number_or_entity,
|
||||
vol.Required(CONF_THRESHOLD_TYPE): vol.Coerce(ThresholdType),
|
||||
vol.Optional(CONF_UNIT): vol.In(unit_converter.VALID_UNITS),
|
||||
},
|
||||
_validate_range(CONF_LOWER_LIMIT, CONF_UPPER_LIMIT),
|
||||
_validate_limits_for_threshold_type,
|
||||
_validate_unit_set_if_range_numerical(
|
||||
CONF_LOWER_LIMIT, CONF_UPPER_LIMIT
|
||||
),
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class EntityNumericalStateCrossedThresholdTriggerWithUnitBase(
|
||||
EntityNumericalStateCrossedThresholdTriggerBase,
|
||||
EntityNumericalStateTriggerWithUnitBase,
|
||||
):
|
||||
"""Trigger for numerical state and state attribute changes."""
|
||||
|
||||
def __init_subclass__(cls, **kwargs: Any) -> None:
|
||||
"""Create a schema."""
|
||||
super().__init_subclass__(**kwargs)
|
||||
cls._schema = make_numerical_state_crossed_threshold_with_unit_schema(
|
||||
cls._unit_converter
|
||||
)
|
||||
|
||||
|
||||
def _normalize_domain_specs(
|
||||
domain_specs: Mapping[str, DomainSpec] | str,
|
||||
) -> Mapping[str, DomainSpec]:
|
||||
|
||||
@@ -35,7 +35,7 @@ file-read-backwards==2.0.0
|
||||
fnv-hash-fast==2.0.0
|
||||
go2rtc-client==0.4.0
|
||||
ha-ffmpeg==3.2.2
|
||||
habluetooth==5.10.2
|
||||
habluetooth==5.11.1
|
||||
hass-nabucasa==2.0.0
|
||||
hassil==3.5.0
|
||||
home-assistant-bluetooth==1.13.1
|
||||
|
||||
8
requirements_all.txt
generated
8
requirements_all.txt
generated
@@ -1176,7 +1176,7 @@ ha-silabs-firmware-client==0.3.0
|
||||
habiticalib==0.4.6
|
||||
|
||||
# homeassistant.components.bluetooth
|
||||
habluetooth==5.10.2
|
||||
habluetooth==5.11.1
|
||||
|
||||
# homeassistant.components.hanna
|
||||
hanna-cloud==0.0.7
|
||||
@@ -1732,7 +1732,7 @@ openwrt-ubus-rpc==0.0.2
|
||||
opower==0.17.1
|
||||
|
||||
# homeassistant.components.oralb
|
||||
oralb-ble==1.0.2
|
||||
oralb-ble==1.1.0
|
||||
|
||||
# homeassistant.components.oru
|
||||
oru==0.1.11
|
||||
@@ -2660,7 +2660,7 @@ python-rabbitair==0.0.8
|
||||
python-ripple-api==0.0.3
|
||||
|
||||
# homeassistant.components.roborock
|
||||
python-roborock==4.26.1
|
||||
python-roborock==4.26.3
|
||||
|
||||
# homeassistant.components.smarttub
|
||||
python-smarttub==0.0.47
|
||||
@@ -3310,7 +3310,7 @@ wirelesstagpy==0.8.1
|
||||
wled==0.21.0
|
||||
|
||||
# homeassistant.components.wolflink
|
||||
wolf-comm==0.0.23
|
||||
wolf-comm==0.0.48
|
||||
|
||||
# homeassistant.components.wsdot
|
||||
wsdot==0.0.1
|
||||
|
||||
8
requirements_test_all.txt
generated
8
requirements_test_all.txt
generated
@@ -1046,7 +1046,7 @@ ha-silabs-firmware-client==0.3.0
|
||||
habiticalib==0.4.6
|
||||
|
||||
# homeassistant.components.bluetooth
|
||||
habluetooth==5.10.2
|
||||
habluetooth==5.11.1
|
||||
|
||||
# homeassistant.components.hanna
|
||||
hanna-cloud==0.0.7
|
||||
@@ -1509,7 +1509,7 @@ openwebifpy==4.3.1
|
||||
opower==0.17.1
|
||||
|
||||
# homeassistant.components.oralb
|
||||
oralb-ble==1.0.2
|
||||
oralb-ble==1.1.0
|
||||
|
||||
# homeassistant.components.orvibo
|
||||
orvibo==1.1.2
|
||||
@@ -2256,7 +2256,7 @@ python-pooldose==0.8.6
|
||||
python-rabbitair==0.0.8
|
||||
|
||||
# homeassistant.components.roborock
|
||||
python-roborock==4.26.1
|
||||
python-roborock==4.26.3
|
||||
|
||||
# homeassistant.components.smarttub
|
||||
python-smarttub==0.0.47
|
||||
@@ -2792,7 +2792,7 @@ wiim==0.1.0
|
||||
wled==0.21.0
|
||||
|
||||
# homeassistant.components.wolflink
|
||||
wolf-comm==0.0.23
|
||||
wolf-comm==0.0.48
|
||||
|
||||
# homeassistant.components.wsdot
|
||||
wsdot==0.0.1
|
||||
|
||||
@@ -120,6 +120,7 @@ NO_IOT_CLASS = [
|
||||
"system_health",
|
||||
"system_log",
|
||||
"tag",
|
||||
"temperature",
|
||||
"timer",
|
||||
"trace",
|
||||
"web_rtc",
|
||||
|
||||
@@ -2149,6 +2149,7 @@ NO_QUALITY_SCALE = [
|
||||
"system_health",
|
||||
"system_log",
|
||||
"tag",
|
||||
"temperature",
|
||||
"timer",
|
||||
"trace",
|
||||
"usage_prediction",
|
||||
|
||||
@@ -518,54 +518,75 @@ def parametrize_trigger_states(
|
||||
|
||||
|
||||
def parametrize_numerical_attribute_changed_trigger_states(
|
||||
trigger: str, state: str, attribute: str
|
||||
trigger: str,
|
||||
state: str,
|
||||
attribute: str,
|
||||
*,
|
||||
trigger_options: dict[str, Any] | None = None,
|
||||
required_filter_attributes: dict | None = None,
|
||||
unit_attributes: dict | None = None,
|
||||
) -> list[tuple[str, dict[str, Any], list[TriggerStateDescription]]]:
|
||||
"""Parametrize states and expected service call counts for numerical changed triggers."""
|
||||
trigger_options = trigger_options or {}
|
||||
unit_attributes = unit_attributes or {}
|
||||
|
||||
return [
|
||||
*parametrize_trigger_states(
|
||||
trigger=trigger,
|
||||
trigger_options={},
|
||||
trigger_options={**trigger_options},
|
||||
target_states=[
|
||||
(state, {attribute: 0}),
|
||||
(state, {attribute: 50}),
|
||||
(state, {attribute: 100}),
|
||||
(state, {attribute: 0} | unit_attributes),
|
||||
(state, {attribute: 50} | unit_attributes),
|
||||
(state, {attribute: 100} | unit_attributes),
|
||||
],
|
||||
other_states=[(state, {attribute: None})],
|
||||
other_states=[(state, {attribute: None} | unit_attributes)],
|
||||
required_filter_attributes=required_filter_attributes,
|
||||
retrigger_on_target_state=True,
|
||||
),
|
||||
*parametrize_trigger_states(
|
||||
trigger=trigger,
|
||||
trigger_options={CONF_ABOVE: 10},
|
||||
trigger_options={CONF_ABOVE: 10, **trigger_options},
|
||||
target_states=[
|
||||
(state, {attribute: 50}),
|
||||
(state, {attribute: 100}),
|
||||
(state, {attribute: 50} | unit_attributes),
|
||||
(state, {attribute: 100} | unit_attributes),
|
||||
],
|
||||
other_states=[
|
||||
(state, {attribute: None}),
|
||||
(state, {attribute: 0}),
|
||||
(state, {attribute: None} | unit_attributes),
|
||||
(state, {attribute: 0} | unit_attributes),
|
||||
],
|
||||
required_filter_attributes=required_filter_attributes,
|
||||
retrigger_on_target_state=True,
|
||||
),
|
||||
*parametrize_trigger_states(
|
||||
trigger=trigger,
|
||||
trigger_options={CONF_BELOW: 90},
|
||||
trigger_options={CONF_BELOW: 90, **trigger_options},
|
||||
target_states=[
|
||||
(state, {attribute: 0}),
|
||||
(state, {attribute: 50}),
|
||||
(state, {attribute: 0} | unit_attributes),
|
||||
(state, {attribute: 50} | unit_attributes),
|
||||
],
|
||||
other_states=[
|
||||
(state, {attribute: None}),
|
||||
(state, {attribute: 100}),
|
||||
(state, {attribute: None} | unit_attributes),
|
||||
(state, {attribute: 100} | unit_attributes),
|
||||
],
|
||||
required_filter_attributes=required_filter_attributes,
|
||||
retrigger_on_target_state=True,
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
||||
trigger: str, state: str, attribute: str
|
||||
trigger: str,
|
||||
state: str,
|
||||
attribute: str,
|
||||
*,
|
||||
trigger_options: dict[str, Any] | None = None,
|
||||
required_filter_attributes: dict | None = None,
|
||||
unit_attributes: dict | None = None,
|
||||
) -> list[tuple[str, dict[str, Any], list[TriggerStateDescription]]]:
|
||||
"""Parametrize states and expected service call counts for numerical crossed threshold triggers."""
|
||||
trigger_options = trigger_options or {}
|
||||
unit_attributes = unit_attributes or {}
|
||||
|
||||
return [
|
||||
*parametrize_trigger_states(
|
||||
trigger=trigger,
|
||||
@@ -573,16 +594,18 @@ def parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
||||
CONF_THRESHOLD_TYPE: ThresholdType.BETWEEN,
|
||||
CONF_LOWER_LIMIT: 10,
|
||||
CONF_UPPER_LIMIT: 90,
|
||||
**trigger_options,
|
||||
},
|
||||
target_states=[
|
||||
(state, {attribute: 50}),
|
||||
(state, {attribute: 60}),
|
||||
(state, {attribute: 50} | unit_attributes),
|
||||
(state, {attribute: 60} | unit_attributes),
|
||||
],
|
||||
other_states=[
|
||||
(state, {attribute: None}),
|
||||
(state, {attribute: 0}),
|
||||
(state, {attribute: 100}),
|
||||
(state, {attribute: None} | unit_attributes),
|
||||
(state, {attribute: 0} | unit_attributes),
|
||||
(state, {attribute: 100} | unit_attributes),
|
||||
],
|
||||
required_filter_attributes=required_filter_attributes,
|
||||
),
|
||||
*parametrize_trigger_states(
|
||||
trigger=trigger,
|
||||
@@ -590,52 +613,62 @@ def parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
||||
CONF_THRESHOLD_TYPE: ThresholdType.OUTSIDE,
|
||||
CONF_LOWER_LIMIT: 10,
|
||||
CONF_UPPER_LIMIT: 90,
|
||||
**trigger_options,
|
||||
},
|
||||
target_states=[
|
||||
(state, {attribute: 0}),
|
||||
(state, {attribute: 100}),
|
||||
(state, {attribute: 0} | unit_attributes),
|
||||
(state, {attribute: 100} | unit_attributes),
|
||||
],
|
||||
other_states=[
|
||||
(state, {attribute: None}),
|
||||
(state, {attribute: 50}),
|
||||
(state, {attribute: 60}),
|
||||
(state, {attribute: None} | unit_attributes),
|
||||
(state, {attribute: 50} | unit_attributes),
|
||||
(state, {attribute: 60} | unit_attributes),
|
||||
],
|
||||
required_filter_attributes=required_filter_attributes,
|
||||
),
|
||||
*parametrize_trigger_states(
|
||||
trigger=trigger,
|
||||
trigger_options={
|
||||
CONF_THRESHOLD_TYPE: ThresholdType.ABOVE,
|
||||
CONF_LOWER_LIMIT: 10,
|
||||
**trigger_options,
|
||||
},
|
||||
target_states=[
|
||||
(state, {attribute: 50}),
|
||||
(state, {attribute: 100}),
|
||||
(state, {attribute: 50} | unit_attributes),
|
||||
(state, {attribute: 100} | unit_attributes),
|
||||
],
|
||||
other_states=[
|
||||
(state, {attribute: None}),
|
||||
(state, {attribute: 0}),
|
||||
(state, {attribute: None} | unit_attributes),
|
||||
(state, {attribute: 0} | unit_attributes),
|
||||
],
|
||||
required_filter_attributes=required_filter_attributes,
|
||||
),
|
||||
*parametrize_trigger_states(
|
||||
trigger=trigger,
|
||||
trigger_options={
|
||||
CONF_THRESHOLD_TYPE: ThresholdType.BELOW,
|
||||
CONF_UPPER_LIMIT: 90,
|
||||
**trigger_options,
|
||||
},
|
||||
target_states=[
|
||||
(state, {attribute: 0}),
|
||||
(state, {attribute: 50}),
|
||||
(state, {attribute: 0} | unit_attributes),
|
||||
(state, {attribute: 50} | unit_attributes),
|
||||
],
|
||||
other_states=[
|
||||
(state, {attribute: None}),
|
||||
(state, {attribute: 100}),
|
||||
(state, {attribute: None} | unit_attributes),
|
||||
(state, {attribute: 100} | unit_attributes),
|
||||
],
|
||||
required_filter_attributes=required_filter_attributes,
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def parametrize_numerical_state_value_changed_trigger_states(
|
||||
trigger: str, device_class: str
|
||||
trigger: str,
|
||||
*,
|
||||
device_class: str,
|
||||
trigger_options: dict[str, Any] | None = None,
|
||||
unit_attributes: dict | None = None,
|
||||
) -> list[tuple[str, dict[str, Any], list[TriggerStateDescription]]]:
|
||||
"""Parametrize states and expected service call counts for numerical state-value changed triggers.
|
||||
|
||||
@@ -646,30 +679,37 @@ def parametrize_numerical_state_value_changed_trigger_states(
|
||||
from homeassistant.const import ATTR_DEVICE_CLASS # noqa: PLC0415
|
||||
|
||||
required_filter_attributes = {ATTR_DEVICE_CLASS: device_class}
|
||||
trigger_options = trigger_options or {}
|
||||
unit_attributes = unit_attributes or {}
|
||||
|
||||
return [
|
||||
*parametrize_trigger_states(
|
||||
trigger=trigger,
|
||||
trigger_options={},
|
||||
target_states=["0", "50", "100"],
|
||||
other_states=["none"],
|
||||
trigger_options=trigger_options,
|
||||
target_states=[
|
||||
("0", unit_attributes),
|
||||
("50", unit_attributes),
|
||||
("100", unit_attributes),
|
||||
],
|
||||
other_states=[("none", unit_attributes)],
|
||||
required_filter_attributes=required_filter_attributes,
|
||||
retrigger_on_target_state=True,
|
||||
trigger_from_none=False,
|
||||
),
|
||||
*parametrize_trigger_states(
|
||||
trigger=trigger,
|
||||
trigger_options={CONF_ABOVE: 10},
|
||||
target_states=["50", "100"],
|
||||
other_states=["none", "0"],
|
||||
trigger_options={CONF_ABOVE: 10} | trigger_options,
|
||||
target_states=[("50", unit_attributes), ("100", unit_attributes)],
|
||||
other_states=[("none", unit_attributes), ("0", unit_attributes)],
|
||||
required_filter_attributes=required_filter_attributes,
|
||||
retrigger_on_target_state=True,
|
||||
trigger_from_none=False,
|
||||
),
|
||||
*parametrize_trigger_states(
|
||||
trigger=trigger,
|
||||
trigger_options={CONF_BELOW: 90},
|
||||
target_states=["0", "50"],
|
||||
other_states=["none", "100"],
|
||||
trigger_options={CONF_BELOW: 90} | trigger_options,
|
||||
target_states=[("0", unit_attributes), ("50", unit_attributes)],
|
||||
other_states=[("none", unit_attributes), ("100", unit_attributes)],
|
||||
required_filter_attributes=required_filter_attributes,
|
||||
retrigger_on_target_state=True,
|
||||
trigger_from_none=False,
|
||||
@@ -678,7 +718,11 @@ def parametrize_numerical_state_value_changed_trigger_states(
|
||||
|
||||
|
||||
def parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
||||
trigger: str, device_class: str
|
||||
trigger: str,
|
||||
*,
|
||||
device_class: str,
|
||||
trigger_options: dict[str, Any] | None = None,
|
||||
unit_attributes: dict | None = None,
|
||||
) -> list[tuple[str, dict[str, Any], list[TriggerStateDescription]]]:
|
||||
"""Parametrize states and expected service call counts for numerical state-value crossed threshold triggers.
|
||||
|
||||
@@ -689,6 +733,9 @@ def parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
||||
from homeassistant.const import ATTR_DEVICE_CLASS # noqa: PLC0415
|
||||
|
||||
required_filter_attributes = {ATTR_DEVICE_CLASS: device_class}
|
||||
trigger_options = trigger_options or {}
|
||||
unit_attributes = unit_attributes or {}
|
||||
|
||||
return [
|
||||
*parametrize_trigger_states(
|
||||
trigger=trigger,
|
||||
@@ -696,9 +743,14 @@ def parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
||||
CONF_THRESHOLD_TYPE: ThresholdType.BETWEEN,
|
||||
CONF_LOWER_LIMIT: 10,
|
||||
CONF_UPPER_LIMIT: 90,
|
||||
**trigger_options,
|
||||
},
|
||||
target_states=["50", "60"],
|
||||
other_states=["none", "0", "100"],
|
||||
target_states=[("50", unit_attributes), ("60", unit_attributes)],
|
||||
other_states=[
|
||||
("none", unit_attributes),
|
||||
("0", unit_attributes),
|
||||
("100", unit_attributes),
|
||||
],
|
||||
required_filter_attributes=required_filter_attributes,
|
||||
trigger_from_none=False,
|
||||
),
|
||||
@@ -708,9 +760,14 @@ def parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
||||
CONF_THRESHOLD_TYPE: ThresholdType.OUTSIDE,
|
||||
CONF_LOWER_LIMIT: 10,
|
||||
CONF_UPPER_LIMIT: 90,
|
||||
**trigger_options,
|
||||
},
|
||||
target_states=["0", "100"],
|
||||
other_states=["none", "50", "60"],
|
||||
target_states=[("0", unit_attributes), ("100", unit_attributes)],
|
||||
other_states=[
|
||||
("none", unit_attributes),
|
||||
("50", unit_attributes),
|
||||
("60", unit_attributes),
|
||||
],
|
||||
required_filter_attributes=required_filter_attributes,
|
||||
trigger_from_none=False,
|
||||
),
|
||||
@@ -719,9 +776,10 @@ def parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
||||
trigger_options={
|
||||
CONF_THRESHOLD_TYPE: ThresholdType.ABOVE,
|
||||
CONF_LOWER_LIMIT: 10,
|
||||
**trigger_options,
|
||||
},
|
||||
target_states=["50", "100"],
|
||||
other_states=["none", "0"],
|
||||
target_states=[("50", unit_attributes), ("100", unit_attributes)],
|
||||
other_states=[("none", unit_attributes), ("0", unit_attributes)],
|
||||
required_filter_attributes=required_filter_attributes,
|
||||
trigger_from_none=False,
|
||||
),
|
||||
@@ -730,9 +788,10 @@ def parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
||||
trigger_options={
|
||||
CONF_THRESHOLD_TYPE: ThresholdType.BELOW,
|
||||
CONF_UPPER_LIMIT: 90,
|
||||
**trigger_options,
|
||||
},
|
||||
target_states=["0", "50"],
|
||||
other_states=["none", "100"],
|
||||
target_states=[("0", unit_attributes), ("50", unit_attributes)],
|
||||
other_states=[("none", unit_attributes), ("100", unit_attributes)],
|
||||
required_filter_attributes=required_filter_attributes,
|
||||
trigger_from_none=False,
|
||||
),
|
||||
|
||||
@@ -11,6 +11,7 @@ from homeassistant.components.climate import (
|
||||
from homeassistant.components.humidifier import (
|
||||
ATTR_CURRENT_HUMIDITY as HUMIDIFIER_ATTR_CURRENT_HUMIDITY,
|
||||
)
|
||||
from homeassistant.components.sensor import SensorDeviceClass
|
||||
from homeassistant.components.weather import ATTR_WEATHER_HUMIDITY
|
||||
from homeassistant.const import ATTR_DEVICE_CLASS, CONF_ENTITY_ID, STATE_ON
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
@@ -81,10 +82,10 @@ async def test_humidity_triggers_gated_by_labs_flag(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_state_value_changed_trigger_states(
|
||||
"humidity.changed", "humidity"
|
||||
"humidity.changed", device_class=SensorDeviceClass.HUMIDITY
|
||||
),
|
||||
*parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
||||
"humidity.crossed_threshold", "humidity"
|
||||
"humidity.crossed_threshold", device_class=SensorDeviceClass.HUMIDITY
|
||||
),
|
||||
],
|
||||
)
|
||||
@@ -122,7 +123,7 @@ async def test_humidity_trigger_sensor_behavior_any(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
||||
"humidity.crossed_threshold", "humidity"
|
||||
"humidity.crossed_threshold", device_class=SensorDeviceClass.HUMIDITY
|
||||
),
|
||||
],
|
||||
)
|
||||
@@ -160,7 +161,7 @@ async def test_humidity_trigger_sensor_crossed_threshold_behavior_first(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
||||
"humidity.crossed_threshold", "humidity"
|
||||
"humidity.crossed_threshold", device_class=SensorDeviceClass.HUMIDITY
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
@@ -5474,6 +5474,7 @@
|
||||
'hens_eggs_size_xl_soft',
|
||||
'huanghuanian_rapid_steam_cooking',
|
||||
'huanghuanian_steam_cooking',
|
||||
'hydroclean',
|
||||
'intensive_bake',
|
||||
'iridescent_shark_fillet',
|
||||
'jasmine_rice_rapid_steam_cooking',
|
||||
@@ -5675,6 +5676,7 @@
|
||||
'rhubarb_chunks',
|
||||
'rice_pudding_rapid_steam_cooking',
|
||||
'rice_pudding_steam_cooking',
|
||||
'rinse',
|
||||
'risotto',
|
||||
'roast_beef_low_temperature_cooking',
|
||||
'roast_beef_roast',
|
||||
@@ -6085,6 +6087,7 @@
|
||||
'hens_eggs_size_xl_soft',
|
||||
'huanghuanian_rapid_steam_cooking',
|
||||
'huanghuanian_steam_cooking',
|
||||
'hydroclean',
|
||||
'intensive_bake',
|
||||
'iridescent_shark_fillet',
|
||||
'jasmine_rice_rapid_steam_cooking',
|
||||
@@ -6286,6 +6289,7 @@
|
||||
'rhubarb_chunks',
|
||||
'rice_pudding_rapid_steam_cooking',
|
||||
'rice_pudding_steam_cooking',
|
||||
'rinse',
|
||||
'risotto',
|
||||
'roast_beef_low_temperature_cooking',
|
||||
'roast_beef_roast',
|
||||
@@ -9268,6 +9272,7 @@
|
||||
'hens_eggs_size_xl_soft',
|
||||
'huanghuanian_rapid_steam_cooking',
|
||||
'huanghuanian_steam_cooking',
|
||||
'hydroclean',
|
||||
'intensive_bake',
|
||||
'iridescent_shark_fillet',
|
||||
'jasmine_rice_rapid_steam_cooking',
|
||||
@@ -9469,6 +9474,7 @@
|
||||
'rhubarb_chunks',
|
||||
'rice_pudding_rapid_steam_cooking',
|
||||
'rice_pudding_steam_cooking',
|
||||
'rinse',
|
||||
'risotto',
|
||||
'roast_beef_low_temperature_cooking',
|
||||
'roast_beef_roast',
|
||||
@@ -9879,6 +9885,7 @@
|
||||
'hens_eggs_size_xl_soft',
|
||||
'huanghuanian_rapid_steam_cooking',
|
||||
'huanghuanian_steam_cooking',
|
||||
'hydroclean',
|
||||
'intensive_bake',
|
||||
'iridescent_shark_fillet',
|
||||
'jasmine_rice_rapid_steam_cooking',
|
||||
@@ -10080,6 +10087,7 @@
|
||||
'rhubarb_chunks',
|
||||
'rice_pudding_rapid_steam_cooking',
|
||||
'rice_pudding_steam_cooking',
|
||||
'rinse',
|
||||
'risotto',
|
||||
'roast_beef_low_temperature_cooking',
|
||||
'roast_beef_roast',
|
||||
|
||||
@@ -26,7 +26,7 @@ async def test_async_step_bluetooth_valid_device(hass: HomeAssistant) -> None:
|
||||
result["flow_id"], user_input={}
|
||||
)
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "Smart Series 7000 48BE"
|
||||
assert result2["title"] == "Triumph D36 48BE"
|
||||
assert result2["data"] == {}
|
||||
assert result2["result"].unique_id == "78:DB:2F:C2:48:BE"
|
||||
|
||||
@@ -91,7 +91,7 @@ async def test_async_step_user_with_found_devices(hass: HomeAssistant) -> None:
|
||||
user_input={"address": "78:DB:2F:C2:48:BE"},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "Smart Series 7000 48BE"
|
||||
assert result2["title"] == "Triumph D36 48BE"
|
||||
assert result2["data"] == {}
|
||||
assert result2["result"].unique_id == "78:DB:2F:C2:48:BE"
|
||||
|
||||
@@ -121,7 +121,7 @@ async def test_async_step_user_replace_ignored(hass: HomeAssistant) -> None:
|
||||
user_input={"address": "78:DB:2F:C2:48:BE"},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "Smart Series 7000 48BE"
|
||||
assert result2["title"] == "Triumph D36 48BE"
|
||||
assert result2["data"] == {}
|
||||
assert result2["result"].unique_id == "78:DB:2F:C2:48:BE"
|
||||
|
||||
@@ -240,7 +240,7 @@ async def test_async_step_user_takes_precedence_over_discovery(
|
||||
user_input={"address": "78:DB:2F:C2:48:BE"},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "Smart Series 7000 48BE"
|
||||
assert result2["title"] == "Triumph D36 48BE"
|
||||
assert result2["data"] == {}
|
||||
assert result2["result"].unique_id == "78:DB:2F:C2:48:BE"
|
||||
|
||||
|
||||
@@ -47,10 +47,10 @@ async def test_sensors(hass: HomeAssistant) -> None:
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 9
|
||||
|
||||
toothbrush_sensor = hass.states.get("sensor.smart_series_7000_48be")
|
||||
toothbrush_sensor = hass.states.get("sensor.triumph_d36_48be")
|
||||
toothbrush_sensor_attrs = toothbrush_sensor.attributes
|
||||
assert toothbrush_sensor.state == "running"
|
||||
assert toothbrush_sensor_attrs[ATTR_FRIENDLY_NAME] == "Smart Series 7000 48BE"
|
||||
assert toothbrush_sensor_attrs[ATTR_FRIENDLY_NAME] == "Triumph D36 48BE"
|
||||
assert ATTR_ASSUMED_STATE not in toothbrush_sensor_attrs
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
@@ -76,7 +76,7 @@ async def test_sensors(hass: HomeAssistant) -> None:
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# All of these devices are sleepy so we should still be available
|
||||
toothbrush_sensor = hass.states.get("sensor.smart_series_7000_48be")
|
||||
toothbrush_sensor = hass.states.get("sensor.triumph_d36_48be")
|
||||
assert toothbrush_sensor.state == "running"
|
||||
|
||||
|
||||
@@ -155,9 +155,9 @@ async def test_sensors_battery(hass: HomeAssistant) -> None:
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all()) == 7
|
||||
|
||||
bat_sensor = hass.states.get("sensor.io_series_6_7_1dcf_battery")
|
||||
bat_sensor = hass.states.get("sensor.io_series_1dcf_battery")
|
||||
assert bat_sensor.state == "49"
|
||||
assert bat_sensor.name == "IO Series 6/7 1DCF Battery"
|
||||
assert bat_sensor.name == "IO Series 1DCF Battery"
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -1,4 +1,54 @@
|
||||
# serializer version: 1
|
||||
# name: test_buttons[button.roborock_q10_s5_empty_dustbin-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': None,
|
||||
'entity_id': 'button.roborock_q10_s5_empty_dustbin',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Empty dustbin',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Empty dustbin',
|
||||
'platform': 'roborock',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'empty_dustbin',
|
||||
'unique_id': 'empty_dustbin_q10_duid',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_buttons[button.roborock_q10_s5_empty_dustbin-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Roborock Q10 S5+ Empty dustbin',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.roborock_q10_s5_empty_dustbin',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
||||
# name: test_buttons[button.roborock_s7_2_reset_air_filter_consumable-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
|
||||
@@ -272,3 +272,55 @@ async def test_press_a01_button_failure(
|
||||
|
||||
washing_machine.zeo.set_value.assert_called_once()
|
||||
assert hass.states.get(entity_id).state == "2023-10-30T08:50:00+00:00"
|
||||
|
||||
|
||||
@pytest.mark.freeze_time("2023-10-30 08:50:00")
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_press_q10_empty_dustbin_button_success(
|
||||
hass: HomeAssistant,
|
||||
bypass_api_client_fixture: None,
|
||||
setup_entry: MockConfigEntry,
|
||||
fake_q10_vacuum: FakeDevice,
|
||||
) -> None:
|
||||
"""Test pressing Q10 empty dustbin button entity."""
|
||||
entity_id = "button.roborock_q10_s5_empty_dustbin"
|
||||
|
||||
assert hass.states.get(entity_id) is not None
|
||||
await hass.services.async_call(
|
||||
"button",
|
||||
SERVICE_PRESS,
|
||||
blocking=True,
|
||||
target={"entity_id": entity_id},
|
||||
)
|
||||
|
||||
assert fake_q10_vacuum.b01_q10_properties is not None
|
||||
fake_q10_vacuum.b01_q10_properties.vacuum.empty_dustbin.assert_called_once()
|
||||
assert hass.states.get(entity_id).state == "2023-10-30T08:50:00+00:00"
|
||||
|
||||
|
||||
@pytest.mark.freeze_time("2023-10-30 08:50:00")
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_press_q10_empty_dustbin_button_failure(
|
||||
hass: HomeAssistant,
|
||||
bypass_api_client_fixture: None,
|
||||
setup_entry: MockConfigEntry,
|
||||
fake_q10_vacuum: FakeDevice,
|
||||
) -> None:
|
||||
"""Test failure while pressing Q10 empty dustbin button entity."""
|
||||
entity_id = "button.roborock_q10_s5_empty_dustbin"
|
||||
assert fake_q10_vacuum.b01_q10_properties is not None
|
||||
fake_q10_vacuum.b01_q10_properties.vacuum.empty_dustbin.side_effect = (
|
||||
RoborockException
|
||||
)
|
||||
|
||||
assert hass.states.get(entity_id) is not None
|
||||
with pytest.raises(HomeAssistantError, match="Error while calling empty_dustbin"):
|
||||
await hass.services.async_call(
|
||||
"button",
|
||||
SERVICE_PRESS,
|
||||
blocking=True,
|
||||
target={"entity_id": entity_id},
|
||||
)
|
||||
|
||||
fake_q10_vacuum.b01_q10_properties.vacuum.empty_dustbin.assert_called_once()
|
||||
assert hass.states.get(entity_id).state == "2023-10-30T08:50:00+00:00"
|
||||
|
||||
1
tests/components/temperature/__init__.py
Normal file
1
tests/components/temperature/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for the temperature integration."""
|
||||
931
tests/components/temperature/test_trigger.py
Normal file
931
tests/components/temperature/test_trigger.py
Normal file
@@ -0,0 +1,931 @@
|
||||
"""Test temperature trigger."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.climate import (
|
||||
ATTR_CURRENT_TEMPERATURE as CLIMATE_ATTR_CURRENT_TEMPERATURE,
|
||||
HVACMode,
|
||||
)
|
||||
from homeassistant.components.sensor import SensorDeviceClass
|
||||
from homeassistant.components.water_heater import (
|
||||
ATTR_CURRENT_TEMPERATURE as WATER_HEATER_ATTR_CURRENT_TEMPERATURE,
|
||||
)
|
||||
from homeassistant.components.weather import (
|
||||
ATTR_WEATHER_TEMPERATURE,
|
||||
ATTR_WEATHER_TEMPERATURE_UNIT,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
CONF_ENTITY_ID,
|
||||
UnitOfTemperature,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
|
||||
from tests.components.common import (
|
||||
TriggerStateDescription,
|
||||
arm_trigger,
|
||||
assert_trigger_behavior_any,
|
||||
assert_trigger_behavior_first,
|
||||
assert_trigger_behavior_last,
|
||||
assert_trigger_gated_by_labs_flag,
|
||||
parametrize_numerical_attribute_changed_trigger_states,
|
||||
parametrize_numerical_attribute_crossed_threshold_trigger_states,
|
||||
parametrize_numerical_state_value_changed_trigger_states,
|
||||
parametrize_numerical_state_value_crossed_threshold_trigger_states,
|
||||
parametrize_target_entities,
|
||||
target_entities,
|
||||
)
|
||||
|
||||
_TEMPERATURE_TRIGGER_OPTIONS = {"unit": UnitOfTemperature.CELSIUS}
|
||||
_SENSOR_UNIT_ATTRIBUTES = {
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS,
|
||||
}
|
||||
_WEATHER_UNIT_ATTRIBUTES = {
|
||||
ATTR_WEATHER_TEMPERATURE_UNIT: UnitOfTemperature.CELSIUS,
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def target_sensors(hass: HomeAssistant) -> dict[str, list[str]]:
|
||||
"""Create multiple sensor entities associated with different targets."""
|
||||
return await target_entities(hass, "sensor")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def target_climates(hass: HomeAssistant) -> dict[str, list[str]]:
|
||||
"""Create multiple climate entities associated with different targets."""
|
||||
return await target_entities(hass, "climate")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def target_water_heaters(hass: HomeAssistant) -> dict[str, list[str]]:
|
||||
"""Create multiple water_heater entities associated with different targets."""
|
||||
return await target_entities(hass, "water_heater")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def target_weathers(hass: HomeAssistant) -> dict[str, list[str]]:
|
||||
"""Create multiple weather entities associated with different targets."""
|
||||
return await target_entities(hass, "weather")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"trigger_key",
|
||||
[
|
||||
"temperature.changed",
|
||||
"temperature.crossed_threshold",
|
||||
],
|
||||
)
|
||||
async def test_temperature_triggers_gated_by_labs_flag(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, trigger_key: str
|
||||
) -> None:
|
||||
"""Test the temperature triggers are gated by the labs flag."""
|
||||
await assert_trigger_gated_by_labs_flag(hass, caplog, trigger_key)
|
||||
|
||||
|
||||
# --- Sensor domain tests (value in state.state) ---
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_target_config", "entity_id", "entities_in_target"),
|
||||
parametrize_target_entities("sensor"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_state_value_changed_trigger_states(
|
||||
"temperature.changed",
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
unit_attributes=_SENSOR_UNIT_ATTRIBUTES,
|
||||
),
|
||||
*parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
||||
"temperature.crossed_threshold",
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
unit_attributes=_SENSOR_UNIT_ATTRIBUTES,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_temperature_trigger_sensor_behavior_any(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
target_sensors: dict[str, list[str]],
|
||||
trigger_target_config: dict,
|
||||
entity_id: str,
|
||||
entities_in_target: int,
|
||||
trigger: str,
|
||||
trigger_options: dict[str, Any],
|
||||
states: list[TriggerStateDescription],
|
||||
) -> None:
|
||||
"""Test temperature trigger fires for sensor entities with device_class temperature."""
|
||||
await assert_trigger_behavior_any(
|
||||
hass,
|
||||
service_calls=service_calls,
|
||||
target_entities=target_sensors,
|
||||
trigger_target_config=trigger_target_config,
|
||||
entity_id=entity_id,
|
||||
entities_in_target=entities_in_target,
|
||||
trigger=trigger,
|
||||
trigger_options=trigger_options,
|
||||
states=states,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_target_config", "entity_id", "entities_in_target"),
|
||||
parametrize_target_entities("sensor"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
||||
"temperature.crossed_threshold",
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
unit_attributes=_SENSOR_UNIT_ATTRIBUTES,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_temperature_trigger_sensor_crossed_threshold_behavior_first(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
target_sensors: dict[str, list[str]],
|
||||
trigger_target_config: dict,
|
||||
entity_id: str,
|
||||
entities_in_target: int,
|
||||
trigger: str,
|
||||
trigger_options: dict[str, Any],
|
||||
states: list[TriggerStateDescription],
|
||||
) -> None:
|
||||
"""Test temperature crossed_threshold trigger fires on the first sensor state change."""
|
||||
await assert_trigger_behavior_first(
|
||||
hass,
|
||||
service_calls=service_calls,
|
||||
target_entities=target_sensors,
|
||||
trigger_target_config=trigger_target_config,
|
||||
entity_id=entity_id,
|
||||
entities_in_target=entities_in_target,
|
||||
trigger=trigger,
|
||||
trigger_options=trigger_options,
|
||||
states=states,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_target_config", "entity_id", "entities_in_target"),
|
||||
parametrize_target_entities("sensor"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_state_value_crossed_threshold_trigger_states(
|
||||
"temperature.crossed_threshold",
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
unit_attributes=_SENSOR_UNIT_ATTRIBUTES,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_temperature_trigger_sensor_crossed_threshold_behavior_last(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
target_sensors: dict[str, list[str]],
|
||||
trigger_target_config: dict,
|
||||
entity_id: str,
|
||||
entities_in_target: int,
|
||||
trigger: str,
|
||||
trigger_options: dict[str, Any],
|
||||
states: list[TriggerStateDescription],
|
||||
) -> None:
|
||||
"""Test temperature crossed_threshold trigger fires when the last sensor changes state."""
|
||||
await assert_trigger_behavior_last(
|
||||
hass,
|
||||
service_calls=service_calls,
|
||||
target_entities=target_sensors,
|
||||
trigger_target_config=trigger_target_config,
|
||||
entity_id=entity_id,
|
||||
entities_in_target=entities_in_target,
|
||||
trigger=trigger,
|
||||
trigger_options=trigger_options,
|
||||
states=states,
|
||||
)
|
||||
|
||||
|
||||
# --- Climate domain tests (value in current_temperature attribute) ---
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_target_config", "entity_id", "entities_in_target"),
|
||||
parametrize_target_entities("climate"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_attribute_changed_trigger_states(
|
||||
"temperature.changed",
|
||||
HVACMode.AUTO,
|
||||
CLIMATE_ATTR_CURRENT_TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
),
|
||||
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
||||
"temperature.crossed_threshold",
|
||||
HVACMode.AUTO,
|
||||
CLIMATE_ATTR_CURRENT_TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_temperature_trigger_climate_behavior_any(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
target_climates: dict[str, list[str]],
|
||||
trigger_target_config: dict,
|
||||
entity_id: str,
|
||||
entities_in_target: int,
|
||||
trigger: str,
|
||||
trigger_options: dict[str, Any],
|
||||
states: list[TriggerStateDescription],
|
||||
) -> None:
|
||||
"""Test temperature trigger fires for climate entities."""
|
||||
await assert_trigger_behavior_any(
|
||||
hass,
|
||||
service_calls=service_calls,
|
||||
target_entities=target_climates,
|
||||
trigger_target_config=trigger_target_config,
|
||||
entity_id=entity_id,
|
||||
entities_in_target=entities_in_target,
|
||||
trigger=trigger,
|
||||
trigger_options=trigger_options,
|
||||
states=states,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_target_config", "entity_id", "entities_in_target"),
|
||||
parametrize_target_entities("climate"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
||||
"temperature.crossed_threshold",
|
||||
HVACMode.AUTO,
|
||||
CLIMATE_ATTR_CURRENT_TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_temperature_trigger_climate_crossed_threshold_behavior_first(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
target_climates: dict[str, list[str]],
|
||||
trigger_target_config: dict,
|
||||
entity_id: str,
|
||||
entities_in_target: int,
|
||||
trigger: str,
|
||||
trigger_options: dict[str, Any],
|
||||
states: list[TriggerStateDescription],
|
||||
) -> None:
|
||||
"""Test temperature crossed_threshold trigger fires on the first climate state change."""
|
||||
await assert_trigger_behavior_first(
|
||||
hass,
|
||||
service_calls=service_calls,
|
||||
target_entities=target_climates,
|
||||
trigger_target_config=trigger_target_config,
|
||||
entity_id=entity_id,
|
||||
entities_in_target=entities_in_target,
|
||||
trigger=trigger,
|
||||
trigger_options=trigger_options,
|
||||
states=states,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_target_config", "entity_id", "entities_in_target"),
|
||||
parametrize_target_entities("climate"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
||||
"temperature.crossed_threshold",
|
||||
HVACMode.AUTO,
|
||||
CLIMATE_ATTR_CURRENT_TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_temperature_trigger_climate_crossed_threshold_behavior_last(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
target_climates: dict[str, list[str]],
|
||||
trigger_target_config: dict,
|
||||
entity_id: str,
|
||||
entities_in_target: int,
|
||||
trigger: str,
|
||||
trigger_options: dict[str, Any],
|
||||
states: list[TriggerStateDescription],
|
||||
) -> None:
|
||||
"""Test temperature crossed_threshold trigger fires when the last climate changes state."""
|
||||
await assert_trigger_behavior_last(
|
||||
hass,
|
||||
service_calls=service_calls,
|
||||
target_entities=target_climates,
|
||||
trigger_target_config=trigger_target_config,
|
||||
entity_id=entity_id,
|
||||
entities_in_target=entities_in_target,
|
||||
trigger=trigger,
|
||||
trigger_options=trigger_options,
|
||||
states=states,
|
||||
)
|
||||
|
||||
|
||||
# --- Water heater domain tests (value in current_temperature attribute) ---
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_target_config", "entity_id", "entities_in_target"),
|
||||
parametrize_target_entities("water_heater"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_attribute_changed_trigger_states(
|
||||
"temperature.changed",
|
||||
"eco",
|
||||
WATER_HEATER_ATTR_CURRENT_TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
),
|
||||
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
||||
"temperature.crossed_threshold",
|
||||
"eco",
|
||||
WATER_HEATER_ATTR_CURRENT_TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_temperature_trigger_water_heater_behavior_any(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
target_water_heaters: dict[str, list[str]],
|
||||
trigger_target_config: dict,
|
||||
entity_id: str,
|
||||
entities_in_target: int,
|
||||
trigger: str,
|
||||
trigger_options: dict[str, Any],
|
||||
states: list[TriggerStateDescription],
|
||||
) -> None:
|
||||
"""Test temperature trigger fires for water_heater entities."""
|
||||
await assert_trigger_behavior_any(
|
||||
hass,
|
||||
service_calls=service_calls,
|
||||
target_entities=target_water_heaters,
|
||||
trigger_target_config=trigger_target_config,
|
||||
entity_id=entity_id,
|
||||
entities_in_target=entities_in_target,
|
||||
trigger=trigger,
|
||||
trigger_options=trigger_options,
|
||||
states=states,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_target_config", "entity_id", "entities_in_target"),
|
||||
parametrize_target_entities("water_heater"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
||||
"temperature.crossed_threshold",
|
||||
"eco",
|
||||
WATER_HEATER_ATTR_CURRENT_TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_temperature_trigger_water_heater_crossed_threshold_behavior_first(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
target_water_heaters: dict[str, list[str]],
|
||||
trigger_target_config: dict,
|
||||
entity_id: str,
|
||||
entities_in_target: int,
|
||||
trigger: str,
|
||||
trigger_options: dict[str, Any],
|
||||
states: list[TriggerStateDescription],
|
||||
) -> None:
|
||||
"""Test temperature crossed_threshold trigger fires on the first water_heater state change."""
|
||||
await assert_trigger_behavior_first(
|
||||
hass,
|
||||
service_calls=service_calls,
|
||||
target_entities=target_water_heaters,
|
||||
trigger_target_config=trigger_target_config,
|
||||
entity_id=entity_id,
|
||||
entities_in_target=entities_in_target,
|
||||
trigger=trigger,
|
||||
trigger_options=trigger_options,
|
||||
states=states,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_target_config", "entity_id", "entities_in_target"),
|
||||
parametrize_target_entities("water_heater"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
||||
"temperature.crossed_threshold",
|
||||
"eco",
|
||||
WATER_HEATER_ATTR_CURRENT_TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_temperature_trigger_water_heater_crossed_threshold_behavior_last(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
target_water_heaters: dict[str, list[str]],
|
||||
trigger_target_config: dict,
|
||||
entity_id: str,
|
||||
entities_in_target: int,
|
||||
trigger: str,
|
||||
trigger_options: dict[str, Any],
|
||||
states: list[TriggerStateDescription],
|
||||
) -> None:
|
||||
"""Test temperature crossed_threshold trigger fires when the last water_heater changes state."""
|
||||
await assert_trigger_behavior_last(
|
||||
hass,
|
||||
service_calls=service_calls,
|
||||
target_entities=target_water_heaters,
|
||||
trigger_target_config=trigger_target_config,
|
||||
entity_id=entity_id,
|
||||
entities_in_target=entities_in_target,
|
||||
trigger=trigger,
|
||||
trigger_options=trigger_options,
|
||||
states=states,
|
||||
)
|
||||
|
||||
|
||||
# --- Weather domain tests (value in temperature attribute) ---
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_target_config", "entity_id", "entities_in_target"),
|
||||
parametrize_target_entities("weather"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_attribute_changed_trigger_states(
|
||||
"temperature.changed",
|
||||
"sunny",
|
||||
ATTR_WEATHER_TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
unit_attributes=_WEATHER_UNIT_ATTRIBUTES,
|
||||
),
|
||||
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
||||
"temperature.crossed_threshold",
|
||||
"sunny",
|
||||
ATTR_WEATHER_TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
unit_attributes=_WEATHER_UNIT_ATTRIBUTES,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_temperature_trigger_weather_behavior_any(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
target_weathers: dict[str, list[str]],
|
||||
trigger_target_config: dict,
|
||||
entity_id: str,
|
||||
entities_in_target: int,
|
||||
trigger: str,
|
||||
trigger_options: dict[str, Any],
|
||||
states: list[TriggerStateDescription],
|
||||
) -> None:
|
||||
"""Test temperature trigger fires for weather entities."""
|
||||
await assert_trigger_behavior_any(
|
||||
hass,
|
||||
service_calls=service_calls,
|
||||
target_entities=target_weathers,
|
||||
trigger_target_config=trigger_target_config,
|
||||
entity_id=entity_id,
|
||||
entities_in_target=entities_in_target,
|
||||
trigger=trigger,
|
||||
trigger_options=trigger_options,
|
||||
states=states,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_target_config", "entity_id", "entities_in_target"),
|
||||
parametrize_target_entities("weather"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
||||
"temperature.crossed_threshold",
|
||||
"sunny",
|
||||
ATTR_WEATHER_TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
unit_attributes=_WEATHER_UNIT_ATTRIBUTES,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_temperature_trigger_weather_crossed_threshold_behavior_first(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
target_weathers: dict[str, list[str]],
|
||||
trigger_target_config: dict,
|
||||
entity_id: str,
|
||||
entities_in_target: int,
|
||||
trigger: str,
|
||||
trigger_options: dict[str, Any],
|
||||
states: list[TriggerStateDescription],
|
||||
) -> None:
|
||||
"""Test temperature crossed_threshold trigger fires on the first weather state change."""
|
||||
await assert_trigger_behavior_first(
|
||||
hass,
|
||||
service_calls=service_calls,
|
||||
target_entities=target_weathers,
|
||||
trigger_target_config=trigger_target_config,
|
||||
entity_id=entity_id,
|
||||
entities_in_target=entities_in_target,
|
||||
trigger=trigger,
|
||||
trigger_options=trigger_options,
|
||||
states=states,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_target_config", "entity_id", "entities_in_target"),
|
||||
parametrize_target_entities("weather"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("trigger", "trigger_options", "states"),
|
||||
[
|
||||
*parametrize_numerical_attribute_crossed_threshold_trigger_states(
|
||||
"temperature.crossed_threshold",
|
||||
"sunny",
|
||||
ATTR_WEATHER_TEMPERATURE,
|
||||
trigger_options=_TEMPERATURE_TRIGGER_OPTIONS,
|
||||
unit_attributes=_WEATHER_UNIT_ATTRIBUTES,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_temperature_trigger_weather_crossed_threshold_behavior_last(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
target_weathers: dict[str, list[str]],
|
||||
trigger_target_config: dict,
|
||||
entity_id: str,
|
||||
entities_in_target: int,
|
||||
trigger: str,
|
||||
trigger_options: dict[str, Any],
|
||||
states: list[TriggerStateDescription],
|
||||
) -> None:
|
||||
"""Test temperature crossed_threshold trigger fires when the last weather changes state."""
|
||||
await assert_trigger_behavior_last(
|
||||
hass,
|
||||
service_calls=service_calls,
|
||||
target_entities=target_weathers,
|
||||
trigger_target_config=trigger_target_config,
|
||||
entity_id=entity_id,
|
||||
entities_in_target=entities_in_target,
|
||||
trigger=trigger,
|
||||
trigger_options=trigger_options,
|
||||
states=states,
|
||||
)
|
||||
|
||||
|
||||
# --- Device class exclusion test ---
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
"trigger_key",
|
||||
"trigger_options",
|
||||
"sensor_initial",
|
||||
"sensor_target",
|
||||
),
|
||||
[
|
||||
(
|
||||
"temperature.changed",
|
||||
{},
|
||||
"20",
|
||||
"25",
|
||||
),
|
||||
(
|
||||
"temperature.crossed_threshold",
|
||||
{"threshold_type": "above", "lower_limit": 10, "unit": "°C"},
|
||||
"5",
|
||||
"20",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_temperature_trigger_excludes_non_temperature_sensor(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
trigger_key: str,
|
||||
trigger_options: dict[str, Any],
|
||||
sensor_initial: str,
|
||||
sensor_target: str,
|
||||
) -> None:
|
||||
"""Test temperature trigger does not fire for sensor entities without device_class temperature."""
|
||||
entity_id_temperature = "sensor.test_temperature"
|
||||
entity_id_humidity = "sensor.test_humidity"
|
||||
|
||||
temp_attrs = {
|
||||
ATTR_DEVICE_CLASS: "temperature",
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS,
|
||||
}
|
||||
humidity_attrs = {ATTR_DEVICE_CLASS: "humidity"}
|
||||
|
||||
# Set initial states
|
||||
hass.states.async_set(entity_id_temperature, sensor_initial, temp_attrs)
|
||||
hass.states.async_set(entity_id_humidity, sensor_initial, humidity_attrs)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await arm_trigger(
|
||||
hass,
|
||||
trigger_key,
|
||||
trigger_options,
|
||||
{
|
||||
CONF_ENTITY_ID: [
|
||||
entity_id_temperature,
|
||||
entity_id_humidity,
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
# Temperature sensor changes - should trigger
|
||||
hass.states.async_set(entity_id_temperature, sensor_target, temp_attrs)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
assert service_calls[0].data[CONF_ENTITY_ID] == entity_id_temperature
|
||||
service_calls.clear()
|
||||
|
||||
# Humidity sensor changes - should NOT trigger (wrong device class)
|
||||
hass.states.async_set(entity_id_humidity, sensor_target, humidity_attrs)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
|
||||
# --- Unit conversion tests ---
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
async def test_temperature_trigger_unit_conversion_sensor_celsius_to_fahrenheit(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
) -> None:
|
||||
"""Test temperature trigger converts sensor value from °C to °F for threshold comparison."""
|
||||
entity_id = "sensor.test_temp"
|
||||
|
||||
# Sensor reports in °C, trigger configured in °F with threshold above 70°F
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
"20",
|
||||
{
|
||||
ATTR_DEVICE_CLASS: "temperature",
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await arm_trigger(
|
||||
hass,
|
||||
"temperature.crossed_threshold",
|
||||
{
|
||||
"threshold_type": "above",
|
||||
"lower_limit": 70,
|
||||
"unit": "°F",
|
||||
},
|
||||
{CONF_ENTITY_ID: [entity_id]},
|
||||
)
|
||||
|
||||
# 20°C = 68°F, which is below 70°F - should NOT trigger
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
"20",
|
||||
{
|
||||
ATTR_DEVICE_CLASS: "temperature",
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# 22°C = 71.6°F, which is above 70°F - should trigger
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
"22",
|
||||
{
|
||||
ATTR_DEVICE_CLASS: "temperature",
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
service_calls.clear()
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
async def test_temperature_trigger_unit_conversion_sensor_fahrenheit_to_celsius(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
) -> None:
|
||||
"""Test temperature trigger converts sensor value from °F to °C for threshold comparison."""
|
||||
entity_id = "sensor.test_temp"
|
||||
|
||||
# Sensor reports in °F, trigger configured in °C with threshold above 25°C
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
"70",
|
||||
{
|
||||
ATTR_DEVICE_CLASS: "temperature",
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await arm_trigger(
|
||||
hass,
|
||||
"temperature.crossed_threshold",
|
||||
{
|
||||
"threshold_type": "above",
|
||||
"lower_limit": 25,
|
||||
"unit": "°C",
|
||||
},
|
||||
{CONF_ENTITY_ID: [entity_id]},
|
||||
)
|
||||
|
||||
# 70°F = 21.1°C, which is below 25°C - should NOT trigger
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
"70",
|
||||
{
|
||||
ATTR_DEVICE_CLASS: "temperature",
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# 80°F = 26.7°C, which is above 25°C - should trigger
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
"80",
|
||||
{
|
||||
ATTR_DEVICE_CLASS: "temperature",
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
service_calls.clear()
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
async def test_temperature_trigger_unit_conversion_changed(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
) -> None:
|
||||
"""Test temperature changed trigger with unit conversion and above/below limits."""
|
||||
entity_id = "sensor.test_temp"
|
||||
|
||||
# Sensor reports in °C, trigger configured in °F: above 68°F (20°C), below 77°F (25°C)
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
"18",
|
||||
{
|
||||
ATTR_DEVICE_CLASS: "temperature",
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await arm_trigger(
|
||||
hass,
|
||||
"temperature.changed",
|
||||
{
|
||||
"above": 68,
|
||||
"below": 77,
|
||||
"unit": "°F",
|
||||
},
|
||||
{CONF_ENTITY_ID: [entity_id]},
|
||||
)
|
||||
|
||||
# 18°C = 64.4°F, below 68°F - should NOT trigger
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
"19",
|
||||
{
|
||||
ATTR_DEVICE_CLASS: "temperature",
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# 22°C = 71.6°F, between 68°F and 77°F - should trigger
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
"22",
|
||||
{
|
||||
ATTR_DEVICE_CLASS: "temperature",
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
service_calls.clear()
|
||||
|
||||
# 26°C = 78.8°F, above 77°F - should NOT trigger
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
"26",
|
||||
{
|
||||
ATTR_DEVICE_CLASS: "temperature",
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_labs_preview_features")
|
||||
async def test_temperature_trigger_unit_conversion_weather(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
) -> None:
|
||||
"""Test temperature trigger with unit conversion for weather entities."""
|
||||
entity_id = "weather.test"
|
||||
|
||||
# Weather reports temperature in °F, trigger configured in °C with threshold above 25°C
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
"sunny",
|
||||
{
|
||||
ATTR_WEATHER_TEMPERATURE: 70,
|
||||
ATTR_WEATHER_TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await arm_trigger(
|
||||
hass,
|
||||
"temperature.crossed_threshold",
|
||||
{
|
||||
"threshold_type": "above",
|
||||
"lower_limit": 25,
|
||||
"unit": "°C",
|
||||
},
|
||||
{CONF_ENTITY_ID: [entity_id]},
|
||||
)
|
||||
|
||||
# 70°F = 21.1°C, below 25°C - should NOT trigger
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
"sunny",
|
||||
{
|
||||
ATTR_WEATHER_TEMPERATURE: 70,
|
||||
ATTR_WEATHER_TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# 80°F = 26.7°C, above 25°C - should trigger
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
"sunny",
|
||||
{
|
||||
ATTR_WEATHER_TEMPERATURE: 80,
|
||||
ATTR_WEATHER_TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
service_calls.clear()
|
||||
@@ -71,7 +71,7 @@ def mock_wolflink() -> Generator[MagicMock]:
|
||||
|
||||
wolflink.fetch_parameters.return_value = [
|
||||
EnergyParameter(
|
||||
6002800000, "Energy Parameter", "Heating", 6005200000, 2000
|
||||
6002800000, "Energy Parameter", "Heating", 6005200000, 2000, True
|
||||
),
|
||||
ListItemParameter(
|
||||
8002800000,
|
||||
@@ -80,22 +80,35 @@ def mock_wolflink() -> Generator[MagicMock]:
|
||||
[ListItem("0", "Aus"), ListItem("1", "Ein")],
|
||||
8005200000,
|
||||
3001,
|
||||
True,
|
||||
),
|
||||
PowerParameter(
|
||||
5002800000, "Power Parameter", "Heating", 5005200000, 1000, True
|
||||
),
|
||||
Pressure(
|
||||
4002800000, "Pressure Parameter", "Heating", 4005200000, 1000, True
|
||||
),
|
||||
Temperature(
|
||||
3002800000, "Temperature Parameter", "Solar", 3005200000, 1000, True
|
||||
),
|
||||
PowerParameter(5002800000, "Power Parameter", "Heating", 5005200000, 1000),
|
||||
Pressure(4002800000, "Pressure Parameter", "Heating", 4005200000, 1000),
|
||||
Temperature(3002800000, "Temperature Parameter", "Solar", 3005200000, 1000),
|
||||
PercentageParameter(
|
||||
2002800000, "Percentage Parameter", "Solar", 2005200000, 1000
|
||||
2002800000, "Percentage Parameter", "Solar", 2005200000, 1000, True
|
||||
),
|
||||
HoursParameter(
|
||||
7002800000, "Hours Parameter", "Heating", 7005200000, 1000, True
|
||||
),
|
||||
SimpleParameter(
|
||||
1002800000, "Simple Parameter", "DHW", 1005200000, 1000, True
|
||||
),
|
||||
HoursParameter(7002800000, "Hours Parameter", "Heating", 7005200000, 1000),
|
||||
SimpleParameter(1002800000, "Simple Parameter", "DHW", 1005200000, 1000),
|
||||
FrequencyParameter(
|
||||
9002800000, "Frequency Parameter", "Heating", 9005200000, 1000
|
||||
9002800000, "Frequency Parameter", "Heating", 9005200000, 1000, True
|
||||
),
|
||||
RPMParameter(
|
||||
1000280001, "RPM Parameter", "Heating", 10005200000, 7000, True
|
||||
),
|
||||
FlowParameter(
|
||||
1100280001, "Flow Parameter", "Heating", 11005200000, 8000, True
|
||||
),
|
||||
RPMParameter(1000280001, "RPM Parameter", "Heating", 10005200000, 7000),
|
||||
FlowParameter(1100280001, "Flow Parameter", "Heating", 11005200000, 8000),
|
||||
HoursParameter(7002800000, "Hours Parameter", "Heating", 7005200000, 1000),
|
||||
SimpleParameter(1002800000, "Simple Parameter", "DHW", 1005200000, 1000),
|
||||
]
|
||||
|
||||
wolflink.fetch_value.return_value = [
|
||||
|
||||
@@ -17,6 +17,7 @@ from homeassistant.components.tag import DOMAIN as TAG_DOMAIN
|
||||
from homeassistant.components.text import DOMAIN as TEXT_DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
CONF_ABOVE,
|
||||
CONF_BELOW,
|
||||
CONF_ENTITY_ID,
|
||||
@@ -25,6 +26,7 @@ from homeassistant.const import (
|
||||
CONF_TARGET,
|
||||
STATE_UNAVAILABLE,
|
||||
STATE_UNKNOWN,
|
||||
UnitOfTemperature,
|
||||
)
|
||||
from homeassistant.core import (
|
||||
CALLBACK_TYPE,
|
||||
@@ -45,10 +47,13 @@ from homeassistant.helpers.automation import (
|
||||
from homeassistant.helpers.trigger import (
|
||||
CONF_LOWER_LIMIT,
|
||||
CONF_THRESHOLD_TYPE,
|
||||
CONF_UNIT,
|
||||
CONF_UPPER_LIMIT,
|
||||
DATA_PLUGGABLE_ACTIONS,
|
||||
EntityTriggerBase,
|
||||
EntityNumericalStateChangedTriggerWithUnitBase,
|
||||
EntityNumericalStateCrossedThresholdTriggerWithUnitBase,
|
||||
PluggableAction,
|
||||
StringEntityTriggerBase,
|
||||
Trigger,
|
||||
TriggerActionRunner,
|
||||
TriggerConfig,
|
||||
@@ -64,6 +69,7 @@ from homeassistant.helpers.trigger import (
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.loader import Integration, async_get_integration
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util.unit_conversion import TemperatureConverter
|
||||
from homeassistant.util.yaml.loader import parse_yaml
|
||||
|
||||
from tests.common import MockModule, MockPlatform, mock_integration, mock_platform
|
||||
@@ -1174,31 +1180,38 @@ async def test_subscribe_triggers_no_triggers(
|
||||
("trigger_options", "expected_result"),
|
||||
[
|
||||
# Test validating climate.target_temperature_changed
|
||||
# Valid configurations
|
||||
# Valid: no limits at all
|
||||
(
|
||||
{},
|
||||
does_not_raise(),
|
||||
),
|
||||
# Valid: numerical limits
|
||||
(
|
||||
{CONF_ABOVE: 10},
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
{CONF_ABOVE: "sensor.test"},
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
{CONF_BELOW: 90},
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
{CONF_ABOVE: 10, CONF_BELOW: 90},
|
||||
does_not_raise(),
|
||||
),
|
||||
# Valid: entity references
|
||||
(
|
||||
{CONF_ABOVE: "sensor.test"},
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
{CONF_BELOW: "sensor.test"},
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
{CONF_ABOVE: 10, CONF_BELOW: 90},
|
||||
{CONF_ABOVE: "sensor.test", CONF_BELOW: "sensor.test"},
|
||||
does_not_raise(),
|
||||
),
|
||||
# Valid: Mix of numerical limits and entity references
|
||||
(
|
||||
{CONF_ABOVE: "sensor.test", CONF_BELOW: 90},
|
||||
does_not_raise(),
|
||||
@@ -1207,10 +1220,6 @@ async def test_subscribe_triggers_no_triggers(
|
||||
{CONF_ABOVE: 10, CONF_BELOW: "sensor.test"},
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
{CONF_ABOVE: "sensor.test", CONF_BELOW: "sensor.test"},
|
||||
does_not_raise(),
|
||||
),
|
||||
# Test verbose choose selector options
|
||||
(
|
||||
{CONF_ABOVE: {"active_choice": "entity", "entity": "sensor.test"}},
|
||||
@@ -1276,6 +1285,147 @@ async def test_numerical_state_attribute_changed_trigger_config_validation(
|
||||
)
|
||||
|
||||
|
||||
def _make_with_unit_changed_trigger_class() -> type[
|
||||
EntityNumericalStateChangedTriggerWithUnitBase
|
||||
]:
|
||||
"""Create a concrete WithUnit changed trigger class for testing."""
|
||||
|
||||
class _TestChangedTrigger(
|
||||
EntityNumericalStateChangedTriggerWithUnitBase,
|
||||
):
|
||||
_base_unit = UnitOfTemperature.CELSIUS
|
||||
_domain_specs = {"test": NumericalDomainSpec(value_source="test_attribute")}
|
||||
_unit_converter = TemperatureConverter
|
||||
|
||||
return _TestChangedTrigger
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_options", "expected_result"),
|
||||
[
|
||||
# Valid: no limits at all
|
||||
(
|
||||
{},
|
||||
does_not_raise(),
|
||||
),
|
||||
# Valid: unit provided with numerical limits
|
||||
(
|
||||
{CONF_ABOVE: 10, CONF_UNIT: UnitOfTemperature.CELSIUS},
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
{CONF_BELOW: 90, CONF_UNIT: UnitOfTemperature.FAHRENHEIT},
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
{
|
||||
CONF_ABOVE: 10,
|
||||
CONF_BELOW: 90,
|
||||
CONF_UNIT: UnitOfTemperature.CELSIUS,
|
||||
},
|
||||
does_not_raise(),
|
||||
),
|
||||
# Valid: no unit needed when using entity references
|
||||
(
|
||||
{CONF_ABOVE: "sensor.test"},
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
{CONF_BELOW: "sensor.test"},
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
{CONF_ABOVE: "sensor.test", CONF_BELOW: "sensor.test"},
|
||||
does_not_raise(),
|
||||
),
|
||||
# Valid: unit only needed for numerical limits, not entity references
|
||||
(
|
||||
{
|
||||
CONF_ABOVE: "sensor.test",
|
||||
CONF_BELOW: 90,
|
||||
CONF_UNIT: UnitOfTemperature.CELSIUS,
|
||||
},
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
{
|
||||
CONF_ABOVE: 10,
|
||||
CONF_BELOW: "sensor.test",
|
||||
CONF_UNIT: UnitOfTemperature.CELSIUS,
|
||||
},
|
||||
does_not_raise(),
|
||||
),
|
||||
# Invalid: numerical limit without unit
|
||||
(
|
||||
{CONF_ABOVE: 10},
|
||||
pytest.raises(vol.Invalid),
|
||||
),
|
||||
(
|
||||
{CONF_BELOW: 90},
|
||||
pytest.raises(vol.Invalid),
|
||||
),
|
||||
(
|
||||
{CONF_ABOVE: 10, CONF_BELOW: 90},
|
||||
pytest.raises(vol.Invalid),
|
||||
),
|
||||
# Invalid: one numerical limit without unit (other is entity)
|
||||
(
|
||||
{CONF_ABOVE: 10, CONF_BELOW: "sensor.test"},
|
||||
pytest.raises(vol.Invalid),
|
||||
),
|
||||
(
|
||||
{CONF_ABOVE: "sensor.test", CONF_BELOW: 90},
|
||||
pytest.raises(vol.Invalid),
|
||||
),
|
||||
# Invalid: invalid unit value
|
||||
(
|
||||
{CONF_ABOVE: 10, CONF_UNIT: "invalid_unit"},
|
||||
pytest.raises(vol.Invalid),
|
||||
),
|
||||
# Invalid: Must use valid entity id
|
||||
(
|
||||
{CONF_ABOVE: "cat", CONF_BELOW: "dog"},
|
||||
pytest.raises(vol.Invalid),
|
||||
),
|
||||
# Invalid: above must be smaller than below
|
||||
(
|
||||
{CONF_ABOVE: 90, CONF_BELOW: 10, CONF_UNIT: UnitOfTemperature.CELSIUS},
|
||||
pytest.raises(vol.Invalid),
|
||||
),
|
||||
# Invalid: invalid choose selector option
|
||||
(
|
||||
{CONF_BELOW: {"active_choice": "cat", "cat": 90}},
|
||||
pytest.raises(vol.Invalid),
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_numerical_state_attribute_changed_with_unit_trigger_config_validation(
|
||||
hass: HomeAssistant,
|
||||
trigger_options: dict[str, Any],
|
||||
expected_result: AbstractContextManager,
|
||||
) -> None:
|
||||
"""Test numerical state attribute change with unit trigger config validation."""
|
||||
trigger_cls = _make_with_unit_changed_trigger_class()
|
||||
|
||||
async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]:
|
||||
return {"test_trigger": trigger_cls}
|
||||
|
||||
mock_integration(hass, MockModule("test"))
|
||||
mock_platform(hass, "test.trigger", Mock(async_get_triggers=async_get_triggers))
|
||||
|
||||
with expected_result:
|
||||
await async_validate_trigger_config(
|
||||
hass,
|
||||
[
|
||||
{
|
||||
"platform": "test.test_trigger",
|
||||
CONF_TARGET: {CONF_ENTITY_ID: "test.test_entity"},
|
||||
CONF_OPTIONS: trigger_options,
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
async def test_numerical_state_attribute_changed_error_handling(
|
||||
hass: HomeAssistant, service_calls: list[ServiceCall]
|
||||
) -> None:
|
||||
@@ -1389,6 +1539,302 @@ async def test_numerical_state_attribute_changed_error_handling(
|
||||
assert len(service_calls) == 0
|
||||
|
||||
|
||||
async def test_numerical_state_attribute_changed_with_unit_error_handling(
|
||||
hass: HomeAssistant, service_calls: list[ServiceCall]
|
||||
) -> None:
|
||||
"""Test numerical state attribute change with unit conversion error handling."""
|
||||
trigger_cls = _make_with_unit_changed_trigger_class()
|
||||
|
||||
async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]:
|
||||
return {"attribute_changed": trigger_cls}
|
||||
|
||||
mock_integration(hass, MockModule("test"))
|
||||
mock_platform(hass, "test.trigger", Mock(async_get_triggers=async_get_triggers))
|
||||
|
||||
# Entity reports in °F, trigger configured in °C with above 20°C, below 30°C
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": 68, # 68°F = 20°C
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
CONF_PLATFORM: "test.attribute_changed",
|
||||
CONF_TARGET: {CONF_ENTITY_ID: "test.test_entity"},
|
||||
CONF_OPTIONS: {
|
||||
CONF_ABOVE: 20,
|
||||
CONF_BELOW: 30,
|
||||
CONF_UNIT: UnitOfTemperature.CELSIUS,
|
||||
},
|
||||
},
|
||||
"action": {
|
||||
"service": "test.numerical_automation",
|
||||
"data_template": {CONF_ENTITY_ID: "{{ trigger.entity_id }}"},
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {
|
||||
CONF_PLATFORM: "test.attribute_changed",
|
||||
CONF_TARGET: {CONF_ENTITY_ID: "test.test_entity"},
|
||||
CONF_OPTIONS: {
|
||||
CONF_ABOVE: "sensor.above",
|
||||
CONF_BELOW: "sensor.below",
|
||||
},
|
||||
},
|
||||
"action": {
|
||||
"service": "test.entity_automation",
|
||||
"data_template": {CONF_ENTITY_ID: "{{ trigger.entity_id }}"},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# 77°F = 25°C, within range (above 20, below 30) - should trigger numerical
|
||||
# Entity automation won't trigger because sensor.above/below don't exist yet
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": 77,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
assert service_calls[0].service == "numerical_automation"
|
||||
service_calls.clear()
|
||||
|
||||
# 59°F = 15°C, below 20°C - should NOT trigger
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": 59,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# 95°F = 35°C, above 30°C - should NOT trigger
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": 95,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Set up entity limits referencing sensors that report in °F
|
||||
hass.states.async_set(
|
||||
"sensor.above",
|
||||
"68", # 68°F = 20°C
|
||||
{ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"sensor.below",
|
||||
"86", # 86°F = 30°C
|
||||
{ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT},
|
||||
)
|
||||
|
||||
# 77°F = 25°C, between 20°C and 30°C - should trigger both automations
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": 77,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 2
|
||||
assert {call.service for call in service_calls} == {
|
||||
"numerical_automation",
|
||||
"entity_automation",
|
||||
}
|
||||
service_calls.clear()
|
||||
|
||||
# Test the trigger does not fire when the attribute value is missing
|
||||
hass.states.async_set("test.test_entity", "on", {})
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Test the trigger does not fire when the attribute value is invalid
|
||||
for value in ("cat", None):
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": value,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Test the trigger does not fire when the unit is incompatible
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": 50,
|
||||
ATTR_UNIT_OF_MEASUREMENT: "invalid_unit",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Test the trigger does not fire when the above sensor does not exist
|
||||
hass.states.async_remove("sensor.above")
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": None,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{"test_attribute": 50, ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Test the trigger does not fire when the above sensor state is not numeric
|
||||
for invalid_value in ("cat", None):
|
||||
hass.states.async_set(
|
||||
"sensor.above",
|
||||
invalid_value,
|
||||
{ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": None,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": 50,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Test the trigger does not fire when the above sensor's unit is incompatible
|
||||
hass.states.async_set(
|
||||
"sensor.above",
|
||||
"68", # 68°F = 20°C
|
||||
{ATTR_UNIT_OF_MEASUREMENT: "invalid_unit"},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": None,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{"test_attribute": 50, ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Reset the above sensor state to a valid numeric value
|
||||
hass.states.async_set(
|
||||
"sensor.above",
|
||||
"68", # 68°F = 20°C
|
||||
{ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT},
|
||||
)
|
||||
|
||||
# Test the trigger does not fire when the below sensor does not exist
|
||||
hass.states.async_remove("sensor.below")
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": None,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{"test_attribute": 50, ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Test the trigger does not fire when the below sensor state is not numeric
|
||||
for invalid_value in ("cat", None):
|
||||
hass.states.async_set("sensor.below", invalid_value)
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": None,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": 50,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Test the trigger does not fire when the below sensor's unit is incompatible
|
||||
hass.states.async_set(
|
||||
"sensor.below",
|
||||
"68", # 68°F = 20°C
|
||||
{ATTR_UNIT_OF_MEASUREMENT: "invalid_unit"},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": None,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{"test_attribute": 50, ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_options", "expected_result"),
|
||||
[
|
||||
@@ -1586,12 +2032,373 @@ async def test_numerical_state_attribute_crossed_threshold_trigger_config_valida
|
||||
)
|
||||
|
||||
|
||||
def _make_with_unit_crossed_threshold_trigger_class() -> type[
|
||||
EntityNumericalStateCrossedThresholdTriggerWithUnitBase
|
||||
]:
|
||||
"""Create a concrete WithUnit crossed threshold trigger class for testing."""
|
||||
|
||||
class _TestCrossedThresholdTrigger(
|
||||
EntityNumericalStateCrossedThresholdTriggerWithUnitBase,
|
||||
):
|
||||
_base_unit = UnitOfTemperature.CELSIUS
|
||||
_domain_specs = {"test": NumericalDomainSpec(value_source="test_attribute")}
|
||||
_unit_converter = TemperatureConverter
|
||||
|
||||
return _TestCrossedThresholdTrigger
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("trigger_options", "expected_result"),
|
||||
[
|
||||
# Valid: unit provided with numerical limits
|
||||
(
|
||||
{
|
||||
CONF_THRESHOLD_TYPE: "above",
|
||||
CONF_LOWER_LIMIT: 10,
|
||||
CONF_UNIT: UnitOfTemperature.CELSIUS,
|
||||
},
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
{
|
||||
CONF_THRESHOLD_TYPE: "below",
|
||||
CONF_UPPER_LIMIT: 90,
|
||||
CONF_UNIT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
{
|
||||
CONF_THRESHOLD_TYPE: "between",
|
||||
CONF_LOWER_LIMIT: 10,
|
||||
CONF_UPPER_LIMIT: 90,
|
||||
CONF_UNIT: UnitOfTemperature.CELSIUS,
|
||||
},
|
||||
does_not_raise(),
|
||||
),
|
||||
# Valid: no unit needed when using entity references
|
||||
(
|
||||
{
|
||||
CONF_THRESHOLD_TYPE: "above",
|
||||
CONF_LOWER_LIMIT: "sensor.test",
|
||||
},
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
{
|
||||
CONF_THRESHOLD_TYPE: "between",
|
||||
CONF_LOWER_LIMIT: "sensor.test",
|
||||
CONF_UPPER_LIMIT: "sensor.test",
|
||||
},
|
||||
does_not_raise(),
|
||||
),
|
||||
# Invalid: numerical limit without unit
|
||||
(
|
||||
{CONF_THRESHOLD_TYPE: "above", CONF_LOWER_LIMIT: 10},
|
||||
pytest.raises(vol.Invalid),
|
||||
),
|
||||
(
|
||||
{
|
||||
CONF_THRESHOLD_TYPE: "between",
|
||||
CONF_LOWER_LIMIT: 10,
|
||||
CONF_UPPER_LIMIT: 90,
|
||||
},
|
||||
pytest.raises(vol.Invalid),
|
||||
),
|
||||
# Invalid: one numerical limit without unit (other is entity)
|
||||
(
|
||||
{
|
||||
CONF_THRESHOLD_TYPE: "between",
|
||||
CONF_LOWER_LIMIT: 10,
|
||||
CONF_UPPER_LIMIT: "sensor.test",
|
||||
},
|
||||
pytest.raises(vol.Invalid),
|
||||
),
|
||||
# Invalid: invalid unit value
|
||||
(
|
||||
{
|
||||
CONF_THRESHOLD_TYPE: "above",
|
||||
CONF_LOWER_LIMIT: 10,
|
||||
CONF_UNIT: "invalid_unit",
|
||||
},
|
||||
pytest.raises(vol.Invalid),
|
||||
),
|
||||
# Invalid: missing threshold type (shared validation)
|
||||
(
|
||||
{},
|
||||
pytest.raises(vol.Invalid),
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_numerical_state_attribute_crossed_threshold_with_unit_trigger_config_validation(
|
||||
hass: HomeAssistant,
|
||||
trigger_options: dict[str, Any],
|
||||
expected_result: AbstractContextManager,
|
||||
) -> None:
|
||||
"""Test numerical state attribute crossed threshold with unit trigger config validation."""
|
||||
trigger_cls = _make_with_unit_crossed_threshold_trigger_class()
|
||||
|
||||
async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]:
|
||||
return {"test_trigger": trigger_cls}
|
||||
|
||||
mock_integration(hass, MockModule("test"))
|
||||
mock_platform(hass, "test.trigger", Mock(async_get_triggers=async_get_triggers))
|
||||
|
||||
with expected_result:
|
||||
await async_validate_trigger_config(
|
||||
hass,
|
||||
[
|
||||
{
|
||||
"platform": "test.test_trigger",
|
||||
CONF_TARGET: {CONF_ENTITY_ID: "test.test_entity"},
|
||||
CONF_OPTIONS: trigger_options,
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
async def test_numerical_state_attribute_crossed_threshold_error_handling(
|
||||
hass: HomeAssistant, service_calls: list[ServiceCall]
|
||||
) -> None:
|
||||
"""Test numerical state attribute crossed threshold error handling."""
|
||||
|
||||
async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]:
|
||||
return {
|
||||
"crossed_threshold": make_entity_numerical_state_crossed_threshold_trigger(
|
||||
{"test": NumericalDomainSpec(value_source="test_attribute")}
|
||||
),
|
||||
}
|
||||
|
||||
mock_integration(hass, MockModule("test"))
|
||||
mock_platform(hass, "test.trigger", Mock(async_get_triggers=async_get_triggers))
|
||||
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": 0})
|
||||
|
||||
options = {
|
||||
CONF_OPTIONS: {
|
||||
CONF_THRESHOLD_TYPE: "between",
|
||||
CONF_LOWER_LIMIT: "sensor.lower",
|
||||
CONF_UPPER_LIMIT: "sensor.upper",
|
||||
},
|
||||
}
|
||||
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
CONF_PLATFORM: "test.crossed_threshold",
|
||||
CONF_TARGET: {CONF_ENTITY_ID: "test.test_entity"},
|
||||
}
|
||||
| options,
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {CONF_ENTITY_ID: "{{ trigger.entity_id }}"},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Test the trigger works
|
||||
hass.states.async_set("sensor.lower", "10")
|
||||
hass.states.async_set("sensor.upper", "90")
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": 50})
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
service_calls.clear()
|
||||
|
||||
# Test the trigger does not fire again when still within limits
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": 51})
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
service_calls.clear()
|
||||
|
||||
# Test the trigger does not fire when the from-state is unknown or unavailable
|
||||
for from_state in (STATE_UNKNOWN, STATE_UNAVAILABLE):
|
||||
hass.states.async_set("test.test_entity", from_state)
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": 50})
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Test the trigger does fire when the attribute value is changing from None
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": None})
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": 50})
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
service_calls.clear()
|
||||
|
||||
# Test the trigger does not fire when the attribute value is outside the limits
|
||||
for value in (5, 95):
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": value})
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Test the trigger does not fire when the attribute value is missing
|
||||
hass.states.async_set("test.test_entity", "on", {})
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Test the trigger does not fire when the attribute value is invalid
|
||||
for value in ("cat", None):
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": value})
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Test the trigger does not fire when the lower sensor does not exist
|
||||
hass.states.async_remove("sensor.lower")
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": None})
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": 50})
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Test the trigger does not fire when the lower sensor state is not numeric
|
||||
for invalid_value in ("cat", None):
|
||||
hass.states.async_set("sensor.lower", invalid_value)
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": None})
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": 50})
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Reset the lower sensor state to a valid numeric value
|
||||
hass.states.async_set("sensor.lower", "10")
|
||||
|
||||
# Test the trigger does not fire when the upper sensor does not exist
|
||||
hass.states.async_remove("sensor.upper")
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": None})
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": 50})
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Test the trigger does not fire when the upper sensor state is not numeric
|
||||
for invalid_value in ("cat", None):
|
||||
hass.states.async_set("sensor.upper", invalid_value)
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": None})
|
||||
hass.states.async_set("test.test_entity", "on", {"test_attribute": 50})
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
|
||||
async def test_numerical_state_attribute_crossed_threshold_with_unit_error_handling(
|
||||
hass: HomeAssistant, service_calls: list[ServiceCall]
|
||||
) -> None:
|
||||
"""Test numerical state attribute crossed threshold with unit conversion."""
|
||||
trigger_cls = _make_with_unit_crossed_threshold_trigger_class()
|
||||
|
||||
async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]:
|
||||
return {"crossed_threshold": trigger_cls}
|
||||
|
||||
mock_integration(hass, MockModule("test"))
|
||||
mock_platform(hass, "test.trigger", Mock(async_get_triggers=async_get_triggers))
|
||||
|
||||
# Entity reports in °F, trigger configured in °C: above 25°C
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": 68, # 68°F = 20°C, below threshold
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
|
||||
options = {
|
||||
CONF_OPTIONS: {
|
||||
CONF_THRESHOLD_TYPE: "above",
|
||||
CONF_LOWER_LIMIT: 25,
|
||||
CONF_UNIT: UnitOfTemperature.CELSIUS,
|
||||
},
|
||||
}
|
||||
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
CONF_PLATFORM: "test.crossed_threshold",
|
||||
CONF_TARGET: {CONF_ENTITY_ID: "test.test_entity"},
|
||||
}
|
||||
| options,
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {CONF_ENTITY_ID: "{{ trigger.entity_id }}"},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# 80.6°F = 27°C, above 25°C threshold - should trigger
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": 80.6,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
service_calls.clear()
|
||||
|
||||
# Still above threshold - should NOT trigger (already crossed)
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": 82,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Drop below threshold and cross again
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": 68, # 20°C, below 25°C
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": 80.6, # 27°C, above 25°C again
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
service_calls.clear()
|
||||
|
||||
# Test with incompatible unit - should NOT trigger
|
||||
hass.states.async_set(
|
||||
"test.test_entity",
|
||||
"on",
|
||||
{
|
||||
"test_attribute": 50,
|
||||
ATTR_UNIT_OF_MEASUREMENT: "invalid_unit",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
|
||||
def _make_trigger(
|
||||
hass: HomeAssistant, domain_specs: Mapping[str, DomainSpec]
|
||||
) -> EntityTriggerBase:
|
||||
"""Create a minimal EntityTriggerBase subclass with the given domain specs."""
|
||||
) -> StringEntityTriggerBase:
|
||||
"""Create a minimal StringEntityTriggerBase subclass with the given domain specs."""
|
||||
|
||||
class _SimpleTrigger(EntityTriggerBase):
|
||||
class _SimpleTrigger(StringEntityTriggerBase):
|
||||
"""Minimal concrete trigger for testing entity_filter."""
|
||||
|
||||
_domain_specs = domain_specs
|
||||
@@ -1773,6 +2580,33 @@ async def test_make_entity_target_state_trigger(
|
||||
assert not trig.is_valid_state(wrong_value_state)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"attribute_value",
|
||||
[
|
||||
pytest.param(["a", "b"], id="list"),
|
||||
pytest.param({"key": "value"}, id="dict"),
|
||||
pytest.param(123, id="int"),
|
||||
pytest.param(None, id="none"),
|
||||
],
|
||||
)
|
||||
async def test_string_entity_trigger_base_non_string_attribute(
|
||||
hass: HomeAssistant,
|
||||
attribute_value: Any,
|
||||
) -> None:
|
||||
"""Test that attribute-based triggers handle non-string attribute values gracefully."""
|
||||
trigger_cls = make_entity_target_state_trigger(
|
||||
{"light": DomainSpec(value_source="effect")}, to_states={"rainbow"}
|
||||
)
|
||||
|
||||
config = TriggerConfig(key="light.test", target={"entity_id": "light.bed"})
|
||||
trig = trigger_cls(hass, config)
|
||||
|
||||
state_with_unhashable = State("light.bed", "on", {"effect": attribute_value})
|
||||
|
||||
# Non-string attribute values should not raise and should not match
|
||||
assert not trig.is_valid_state(state_with_unhashable)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
"domain_specs",
|
||||
|
||||
@@ -86,6 +86,7 @@
|
||||
'system_health',
|
||||
'system_log',
|
||||
'tag',
|
||||
'temperature',
|
||||
'text',
|
||||
'time',
|
||||
'timer',
|
||||
@@ -190,6 +191,7 @@
|
||||
'system_health',
|
||||
'system_log',
|
||||
'tag',
|
||||
'temperature',
|
||||
'text',
|
||||
'time',
|
||||
'timer',
|
||||
|
||||
Reference in New Issue
Block a user