diff --git a/CODEOWNERS b/CODEOWNERS index 1472c4f7b5267b..27fe684087ad70 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -743,6 +743,8 @@ build.json @home-assistant/supervisor /tests/components/huisbaasje/ @dennisschroer /homeassistant/components/humidifier/ @home-assistant/core @Shulyaka /tests/components/humidifier/ @home-assistant/core @Shulyaka +/homeassistant/components/humidity/ @home-assistant/core +/tests/components/humidity/ @home-assistant/core /homeassistant/components/hunterdouglas_powerview/ @bdraco @kingy444 @trullock /tests/components/hunterdouglas_powerview/ @bdraco @kingy444 @trullock /homeassistant/components/husqvarna_automower/ @Thomas55555 @@ -792,8 +794,8 @@ build.json @home-assistant/supervisor /tests/components/improv_ble/ @emontnemery /homeassistant/components/incomfort/ @jbouwh /tests/components/incomfort/ @jbouwh -/homeassistant/components/indevolt/ @xirtnl -/tests/components/indevolt/ @xirtnl +/homeassistant/components/indevolt/ @xirt +/tests/components/indevolt/ @xirt /homeassistant/components/inels/ @epdevlab /tests/components/inels/ @epdevlab /homeassistant/components/influxdb/ @mdegat01 @Robbie1221 diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 9285430755e411..6985d0769267ed 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -243,6 +243,7 @@ # Integrations providing triggers and conditions for base platforms: "door", "garage_door", + "humidity", } DEFAULT_INTEGRATIONS_RECOVERY_MODE = { # These integrations are set up if recovery mode is activated. diff --git a/homeassistant/components/adax/climate.py b/homeassistant/components/adax/climate.py old mode 100644 new mode 100755 index b41a443243779c..62ddb213e2a6bd --- a/homeassistant/components/adax/climate.py +++ b/homeassistant/components/adax/climate.py @@ -168,29 +168,57 @@ async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None: if hvac_mode == HVACMode.HEAT: temperature = self._attr_target_temperature or self._attr_min_temp await self._adax_data_handler.set_target_temperature(temperature) + self._attr_target_temperature = temperature + self._attr_icon = "mdi:radiator" elif hvac_mode == HVACMode.OFF: await self._adax_data_handler.set_target_temperature(0) + self._attr_icon = "mdi:radiator-off" + else: + # Ignore unsupported HVAC modes to avoid desynchronizing entity state + # from the physical device. + return + + self._attr_hvac_mode = hvac_mode + self.async_write_ha_state() async def async_set_temperature(self, **kwargs: Any) -> None: """Set new target temperature.""" if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None: return - await self._adax_data_handler.set_target_temperature(temperature) + if self._attr_hvac_mode == HVACMode.HEAT: + await self._adax_data_handler.set_target_temperature(temperature) - @callback - def _handle_coordinator_update(self) -> None: - """Handle updated data from the coordinator.""" + self._attr_target_temperature = temperature + self.async_write_ha_state() + + def _update_hvac_attributes(self) -> None: + """Update hvac mode and temperatures from coordinator data. + + The coordinator reports a target temperature of 0 when the heater is + turned off. In that case, only the hvac mode and icon are updated and + the previous non-zero target temperature is preserved. When the + reported target temperature is non-zero, the stored target temperature + is updated to match the coordinator value. + """ if data := self.coordinator.data: self._attr_current_temperature = data["current_temperature"] - self._attr_available = self._attr_current_temperature is not None if (target_temp := data["target_temperature"]) == 0: self._attr_hvac_mode = HVACMode.OFF self._attr_icon = "mdi:radiator-off" - if target_temp == 0: + if self._attr_target_temperature is None: self._attr_target_temperature = self._attr_min_temp else: self._attr_hvac_mode = HVACMode.HEAT self._attr_icon = "mdi:radiator" self._attr_target_temperature = target_temp + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self._update_hvac_attributes() super()._handle_coordinator_update() + + async def async_added_to_hass(self) -> None: + """When entity is added to hass.""" + await super().async_added_to_hass() + self._update_hvac_attributes() diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 831739101e0c66..66a1c947471984 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -146,6 +146,7 @@ "fan", "garage_door", "humidifier", + "humidity", "lawn_mower", "light", "lock", diff --git a/homeassistant/components/climate/trigger.py b/homeassistant/components/climate/trigger.py index eb31dee8edfca7..231e5273a8c5cc 100644 --- a/homeassistant/components/climate/trigger.py +++ b/homeassistant/components/climate/trigger.py @@ -53,16 +53,16 @@ def __init__(self, hass: HomeAssistant, config: TriggerConfig) -> None: DOMAIN, ATTR_HVAC_ACTION, HVACAction.DRYING ), "target_humidity_changed": make_entity_numerical_state_attribute_changed_trigger( - DOMAIN, ATTR_HUMIDITY + {DOMAIN}, {DOMAIN: ATTR_HUMIDITY} ), "target_humidity_crossed_threshold": make_entity_numerical_state_attribute_crossed_threshold_trigger( - DOMAIN, ATTR_HUMIDITY + {DOMAIN}, {DOMAIN: ATTR_HUMIDITY} ), "target_temperature_changed": make_entity_numerical_state_attribute_changed_trigger( - DOMAIN, ATTR_TEMPERATURE + {DOMAIN}, {DOMAIN: ATTR_TEMPERATURE} ), "target_temperature_crossed_threshold": make_entity_numerical_state_attribute_crossed_threshold_trigger( - DOMAIN, ATTR_TEMPERATURE + {DOMAIN}, {DOMAIN: ATTR_TEMPERATURE} ), "turned_off": make_entity_target_state_trigger(DOMAIN, HVACMode.OFF), "turned_on": make_entity_transition_trigger( diff --git a/homeassistant/components/cover/__init__.py b/homeassistant/components/cover/__init__.py index 1dbf972a26f27e..d252f84677d16a 100644 --- a/homeassistant/components/cover/__init__.py +++ b/homeassistant/components/cover/__init__.py @@ -45,7 +45,7 @@ CoverEntityFeature, CoverState, ) -from .trigger import CoverClosedTriggerBase, CoverOpenedTriggerBase +from .trigger import make_cover_closed_trigger, make_cover_opened_trigger _LOGGER = logging.getLogger(__name__) @@ -74,13 +74,13 @@ "INTENT_OPEN_COVER", "PLATFORM_SCHEMA", "PLATFORM_SCHEMA_BASE", - "CoverClosedTriggerBase", "CoverDeviceClass", "CoverEntity", "CoverEntityDescription", "CoverEntityFeature", - "CoverOpenedTriggerBase", "CoverState", + "make_cover_closed_trigger", + "make_cover_opened_trigger", ] diff --git a/homeassistant/components/cover/icons.json b/homeassistant/components/cover/icons.json index 91775fe634dbec..8ad6123cfb422d 100644 --- a/homeassistant/components/cover/icons.json +++ b/homeassistant/components/cover/icons.json @@ -108,5 +108,37 @@ "toggle_cover_tilt": { "service": "mdi:arrow-top-right-bottom-left" } + }, + "triggers": { + "awning_closed": { + "trigger": "mdi:storefront-outline" + }, + "awning_opened": { + "trigger": "mdi:storefront-outline" + }, + "blind_closed": { + "trigger": "mdi:blinds-horizontal-closed" + }, + "blind_opened": { + "trigger": "mdi:blinds-horizontal" + }, + "curtain_closed": { + "trigger": "mdi:curtains-closed" + }, + "curtain_opened": { + "trigger": "mdi:curtains" + }, + "shade_closed": { + "trigger": "mdi:roller-shade-closed" + }, + "shade_opened": { + "trigger": "mdi:roller-shade" + }, + "shutter_closed": { + "trigger": "mdi:window-shutter" + }, + "shutter_opened": { + "trigger": "mdi:window-shutter-open" + } } } diff --git a/homeassistant/components/cover/strings.json b/homeassistant/components/cover/strings.json index f0d42685e85e4e..67c7ab6c245cba 100644 --- a/homeassistant/components/cover/strings.json +++ b/homeassistant/components/cover/strings.json @@ -1,4 +1,8 @@ { + "common": { + "trigger_behavior_description": "The behavior of the targeted covers to trigger on.", + "trigger_behavior_name": "Behavior" + }, "device_automation": { "action_type": { "close": "Close {entity_name}", @@ -82,6 +86,15 @@ "name": "Window" } }, + "selector": { + "trigger_behavior": { + "options": { + "any": "Any", + "first": "First", + "last": "Last" + } + } + }, "services": { "close_cover": { "description": "Closes a cover.", @@ -136,5 +149,107 @@ "name": "Toggle tilt" } }, - "title": "Cover" + "title": "Cover", + "triggers": { + "awning_closed": { + "description": "Triggers after one or more awnings close.", + "fields": { + "behavior": { + "description": "[%key:component::cover::common::trigger_behavior_description%]", + "name": "[%key:component::cover::common::trigger_behavior_name%]" + } + }, + "name": "Awning closed" + }, + "awning_opened": { + "description": "Triggers after one or more awnings open.", + "fields": { + "behavior": { + "description": "[%key:component::cover::common::trigger_behavior_description%]", + "name": "[%key:component::cover::common::trigger_behavior_name%]" + } + }, + "name": "Awning opened" + }, + "blind_closed": { + "description": "Triggers after one or more blinds close.", + "fields": { + "behavior": { + "description": "[%key:component::cover::common::trigger_behavior_description%]", + "name": "[%key:component::cover::common::trigger_behavior_name%]" + } + }, + "name": "Blind closed" + }, + "blind_opened": { + "description": "Triggers after one or more blinds open.", + "fields": { + "behavior": { + "description": "[%key:component::cover::common::trigger_behavior_description%]", + "name": "[%key:component::cover::common::trigger_behavior_name%]" + } + }, + "name": "Blind opened" + }, + "curtain_closed": { + "description": "Triggers after one or more curtains close.", + "fields": { + "behavior": { + "description": "[%key:component::cover::common::trigger_behavior_description%]", + "name": "[%key:component::cover::common::trigger_behavior_name%]" + } + }, + "name": "Curtain closed" + }, + "curtain_opened": { + "description": "Triggers after one or more curtains open.", + "fields": { + "behavior": { + "description": "[%key:component::cover::common::trigger_behavior_description%]", + "name": "[%key:component::cover::common::trigger_behavior_name%]" + } + }, + "name": "Curtain opened" + }, + "shade_closed": { + "description": "Triggers after one or more shades close.", + "fields": { + "behavior": { + "description": "[%key:component::cover::common::trigger_behavior_description%]", + "name": "[%key:component::cover::common::trigger_behavior_name%]" + } + }, + "name": "Shade closed" + }, + "shade_opened": { + "description": "Triggers after one or more shades open.", + "fields": { + "behavior": { + "description": "[%key:component::cover::common::trigger_behavior_description%]", + "name": "[%key:component::cover::common::trigger_behavior_name%]" + } + }, + "name": "Shade opened" + }, + "shutter_closed": { + "description": "Triggers after one or more shutters close.", + "fields": { + "behavior": { + "description": "[%key:component::cover::common::trigger_behavior_description%]", + "name": "[%key:component::cover::common::trigger_behavior_name%]" + } + }, + "name": "Shutter closed" + }, + "shutter_opened": { + "description": "Triggers after one or more shutters open.", + "fields": { + "behavior": { + "description": "[%key:component::cover::common::trigger_behavior_description%]", + "name": "[%key:component::cover::common::trigger_behavior_name%]" + } + }, + "name": "Shutter opened" + } + } } diff --git a/homeassistant/components/cover/trigger.py b/homeassistant/components/cover/trigger.py index bbd669b7856586..d848d70839b0e1 100644 --- a/homeassistant/components/cover/trigger.py +++ b/homeassistant/components/cover/trigger.py @@ -1,30 +1,19 @@ """Provides triggers for covers.""" -from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN from homeassistant.core import HomeAssistant, State, split_entity_id -from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers.entity import get_device_class -from homeassistant.helpers.trigger import EntityTriggerBase -from homeassistant.helpers.typing import UNDEFINED, UndefinedType +from homeassistant.helpers.trigger import ( + EntityTriggerBase, + Trigger, + get_device_class_or_undefined, +) -from .const import ATTR_IS_CLOSED, DOMAIN - - -def get_device_class_or_undefined( - hass: HomeAssistant, entity_id: str -) -> str | None | UndefinedType: - """Get the device class of an entity or UNDEFINED if not found.""" - try: - return get_device_class(hass, entity_id) - except HomeAssistantError: - return UNDEFINED +from .const import ATTR_IS_CLOSED, DOMAIN, CoverDeviceClass class CoverTriggerBase(EntityTriggerBase): """Base trigger for cover state changes.""" - _domains = {BINARY_SENSOR_DOMAIN, DOMAIN} _binary_sensor_target_state: str _cover_is_closed_target_value: bool _device_classes: dict[str, str] @@ -59,15 +48,60 @@ def is_valid_transition(self, from_state: State, to_state: State) -> bool: return from_state.state != to_state.state -class CoverOpenedTriggerBase(CoverTriggerBase): - """Base trigger for cover opened state changes.""" +def make_cover_opened_trigger( + *, device_classes: dict[str, str], domains: set[str] | None = None +) -> type[CoverTriggerBase]: + """Create a trigger cover_opened.""" + + class CoverOpenedTrigger(CoverTriggerBase): + """Trigger for cover opened state changes.""" + + _binary_sensor_target_state = STATE_ON + _cover_is_closed_target_value = False + _domains = domains or {DOMAIN} + _device_classes = device_classes + + return CoverOpenedTrigger + + +def make_cover_closed_trigger( + *, device_classes: dict[str, str], domains: set[str] | None = None +) -> type[CoverTriggerBase]: + """Create a trigger cover_closed.""" + + class CoverClosedTrigger(CoverTriggerBase): + """Trigger for cover closed state changes.""" + + _binary_sensor_target_state = STATE_OFF + _cover_is_closed_target_value = True + _domains = domains or {DOMAIN} + _device_classes = device_classes + + return CoverClosedTrigger + + +# Concrete triggers for cover device classes (cover-only, no binary sensor) - _binary_sensor_target_state = STATE_ON - _cover_is_closed_target_value = False +DEVICE_CLASSES_AWNING: dict[str, str] = {DOMAIN: CoverDeviceClass.AWNING} +DEVICE_CLASSES_BLIND: dict[str, str] = {DOMAIN: CoverDeviceClass.BLIND} +DEVICE_CLASSES_CURTAIN: dict[str, str] = {DOMAIN: CoverDeviceClass.CURTAIN} +DEVICE_CLASSES_SHADE: dict[str, str] = {DOMAIN: CoverDeviceClass.SHADE} +DEVICE_CLASSES_SHUTTER: dict[str, str] = {DOMAIN: CoverDeviceClass.SHUTTER} +TRIGGERS: dict[str, type[Trigger]] = { + "awning_opened": make_cover_opened_trigger(device_classes=DEVICE_CLASSES_AWNING), + "awning_closed": make_cover_closed_trigger(device_classes=DEVICE_CLASSES_AWNING), + "blind_opened": make_cover_opened_trigger(device_classes=DEVICE_CLASSES_BLIND), + "blind_closed": make_cover_closed_trigger(device_classes=DEVICE_CLASSES_BLIND), + "curtain_opened": make_cover_opened_trigger(device_classes=DEVICE_CLASSES_CURTAIN), + "curtain_closed": make_cover_closed_trigger(device_classes=DEVICE_CLASSES_CURTAIN), + "shade_opened": make_cover_opened_trigger(device_classes=DEVICE_CLASSES_SHADE), + "shade_closed": make_cover_closed_trigger(device_classes=DEVICE_CLASSES_SHADE), + "shutter_opened": make_cover_opened_trigger(device_classes=DEVICE_CLASSES_SHUTTER), + "shutter_closed": make_cover_closed_trigger(device_classes=DEVICE_CLASSES_SHUTTER), +} -class CoverClosedTriggerBase(CoverTriggerBase): - """Base trigger for cover closed state changes.""" - _binary_sensor_target_state = STATE_OFF - _cover_is_closed_target_value = True +async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]: + """Return the triggers for covers.""" + return TRIGGERS diff --git a/homeassistant/components/cover/triggers.yaml b/homeassistant/components/cover/triggers.yaml new file mode 100644 index 00000000000000..4b9d0a054dc9c8 --- /dev/null +++ b/homeassistant/components/cover/triggers.yaml @@ -0,0 +1,81 @@ +.trigger_common_fields: &trigger_common_fields + behavior: + required: true + default: any + selector: + select: + translation_key: trigger_behavior + options: + - first + - last + - any + +awning_closed: + fields: *trigger_common_fields + target: + entity: + - domain: cover + device_class: awning + +awning_opened: + fields: *trigger_common_fields + target: + entity: + - domain: cover + device_class: awning + +blind_closed: + fields: *trigger_common_fields + target: + entity: + - domain: cover + device_class: blind + +blind_opened: + fields: *trigger_common_fields + target: + entity: + - domain: cover + device_class: blind + +curtain_closed: + fields: *trigger_common_fields + target: + entity: + - domain: cover + device_class: curtain + +curtain_opened: + fields: *trigger_common_fields + target: + entity: + - domain: cover + device_class: curtain + +shade_closed: + fields: *trigger_common_fields + target: + entity: + - domain: cover + device_class: shade + +shade_opened: + fields: *trigger_common_fields + target: + entity: + - domain: cover + device_class: shade + +shutter_closed: + fields: *trigger_common_fields + target: + entity: + - domain: cover + device_class: shutter + +shutter_opened: + fields: *trigger_common_fields + target: + entity: + - domain: cover + device_class: shutter diff --git a/homeassistant/components/door/trigger.py b/homeassistant/components/door/trigger.py index 2c6f1b8aab9b79..f301fa16018454 100644 --- a/homeassistant/components/door/trigger.py +++ b/homeassistant/components/door/trigger.py @@ -6,9 +6,9 @@ ) from homeassistant.components.cover import ( DOMAIN as COVER_DOMAIN, - CoverClosedTriggerBase, CoverDeviceClass, - CoverOpenedTriggerBase, + make_cover_closed_trigger, + make_cover_opened_trigger, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.trigger import Trigger @@ -19,21 +19,15 @@ } -class DoorOpenedTrigger(CoverOpenedTriggerBase): - """Trigger for door opened state changes.""" - - _device_classes = DEVICE_CLASSES_DOOR - - -class DoorClosedTrigger(CoverClosedTriggerBase): - """Trigger for door closed state changes.""" - - _device_classes = DEVICE_CLASSES_DOOR - - TRIGGERS: dict[str, type[Trigger]] = { - "opened": DoorOpenedTrigger, - "closed": DoorClosedTrigger, + "opened": make_cover_opened_trigger( + device_classes=DEVICE_CLASSES_DOOR, + domains={BINARY_SENSOR_DOMAIN, COVER_DOMAIN}, + ), + "closed": make_cover_closed_trigger( + device_classes=DEVICE_CLASSES_DOOR, + domains={BINARY_SENSOR_DOMAIN, COVER_DOMAIN}, + ), } diff --git a/homeassistant/components/garage_door/trigger.py b/homeassistant/components/garage_door/trigger.py index 31a0bf04458498..90eebf19227018 100644 --- a/homeassistant/components/garage_door/trigger.py +++ b/homeassistant/components/garage_door/trigger.py @@ -6,9 +6,9 @@ ) from homeassistant.components.cover import ( DOMAIN as COVER_DOMAIN, - CoverClosedTriggerBase, CoverDeviceClass, - CoverOpenedTriggerBase, + make_cover_closed_trigger, + make_cover_opened_trigger, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.trigger import Trigger @@ -19,21 +19,15 @@ } -class GarageDoorOpenedTrigger(CoverOpenedTriggerBase): - """Trigger for garage door opened state changes.""" - - _device_classes = DEVICE_CLASSES_GARAGE_DOOR - - -class GarageDoorClosedTrigger(CoverClosedTriggerBase): - """Trigger for garage door closed state changes.""" - - _device_classes = DEVICE_CLASSES_GARAGE_DOOR - - TRIGGERS: dict[str, type[Trigger]] = { - "opened": GarageDoorOpenedTrigger, - "closed": GarageDoorClosedTrigger, + "opened": make_cover_opened_trigger( + device_classes=DEVICE_CLASSES_GARAGE_DOOR, + domains={BINARY_SENSOR_DOMAIN, COVER_DOMAIN}, + ), + "closed": make_cover_closed_trigger( + device_classes=DEVICE_CLASSES_GARAGE_DOOR, + domains={BINARY_SENSOR_DOMAIN, COVER_DOMAIN}, + ), } diff --git a/homeassistant/components/ghost/quality_scale.yaml b/homeassistant/components/ghost/quality_scale.yaml index 6603b309204e08..55bc8670dc9489 100644 --- a/homeassistant/components/ghost/quality_scale.yaml +++ b/homeassistant/components/ghost/quality_scale.yaml @@ -72,9 +72,7 @@ rules: repair-issues: status: exempt comment: No repair scenarios identified for this integration. - stale-devices: - status: todo - comment: Remove newsletter entities when newsletter is removed + stale-devices: done # Platinum async-dependency: done diff --git a/homeassistant/components/ghost/sensor.py b/homeassistant/components/ghost/sensor.py index 9986edc9dee5ac..9fd3ea977c6576 100644 --- a/homeassistant/components/ghost/sensor.py +++ b/homeassistant/components/ghost/sensor.py @@ -12,7 +12,9 @@ SensorEntityDescription, SensorStateClass, ) +from homeassistant.const import Platform from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import entity_registry as er from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -210,36 +212,67 @@ async def async_setup_entry( async_add_entities(entities) + # Remove stale newsletter entities left over from previous runs. + entity_registry = er.async_get(hass) + prefix = f"{entry.unique_id}_newsletter_" + active_newsletters = { + newsletter_id + for newsletter_id, newsletter in coordinator.data.newsletters.items() + if newsletter.get("status") == "active" + } + for entity_entry in er.async_entries_for_config_entry( + entity_registry, entry.entry_id + ): + if ( + entity_entry.unique_id.startswith(prefix) + and entity_entry.unique_id[len(prefix) :] not in active_newsletters + ): + entity_registry.async_remove(entity_entry.entity_id) + newsletter_added: set[str] = set() @callback - def _async_add_newsletter_entities() -> None: - """Add newsletter entities when new newsletters appear.""" + def _async_update_newsletter_entities() -> None: + """Add new and remove stale newsletter entities.""" nonlocal newsletter_added - new_newsletters = { + active_newsletters = { newsletter_id for newsletter_id, newsletter in coordinator.data.newsletters.items() if newsletter.get("status") == "active" - } - newsletter_added - - if not new_newsletters: - return - - async_add_entities( - GhostNewsletterSensorEntity( - coordinator, - entry, - newsletter_id, - coordinator.data.newsletters[newsletter_id].get("name", "Newsletter"), + } + + new_newsletters = active_newsletters - newsletter_added + + if new_newsletters: + async_add_entities( + GhostNewsletterSensorEntity( + coordinator, + entry, + newsletter_id, + coordinator.data.newsletters[newsletter_id].get( + "name", "Newsletter" + ), + ) + for newsletter_id in new_newsletters ) - for newsletter_id in new_newsletters - ) - newsletter_added |= new_newsletters - - _async_add_newsletter_entities() + newsletter_added.update(new_newsletters) + + removed_newsletters = newsletter_added - active_newsletters + if removed_newsletters: + entity_registry = er.async_get(hass) + for newsletter_id in removed_newsletters: + unique_id = f"{entry.unique_id}_newsletter_{newsletter_id}" + entity_id = entity_registry.async_get_entity_id( + Platform.SENSOR, DOMAIN, unique_id + ) + if entity_id: + entity_registry.async_remove(entity_id) + newsletter_added -= removed_newsletters + + _async_update_newsletter_entities() entry.async_on_unload( - coordinator.async_add_listener(_async_add_newsletter_entities) + coordinator.async_add_listener(_async_update_newsletter_entities) ) @@ -310,9 +343,10 @@ def _get_newsletter_by_id(self) -> dict[str, Any] | None: @property def available(self) -> bool: """Return True if the entity is available.""" - if not super().available or self.coordinator.data is None: - return False - return self._newsletter_id in self.coordinator.data.newsletters + return ( + super().available + and self._newsletter_id in self.coordinator.data.newsletters + ) @property def native_value(self) -> int | None: diff --git a/homeassistant/components/humidity/__init__.py b/homeassistant/components/humidity/__init__.py new file mode 100644 index 00000000000000..2c84f69089fc53 --- /dev/null +++ b/homeassistant/components/humidity/__init__.py @@ -0,0 +1,17 @@ +"""Integration for humidity 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 = "humidity" +CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) + +__all__ = [] + + +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: + """Set up the component.""" + return True diff --git a/homeassistant/components/humidity/icons.json b/homeassistant/components/humidity/icons.json new file mode 100644 index 00000000000000..6b3c862c663647 --- /dev/null +++ b/homeassistant/components/humidity/icons.json @@ -0,0 +1,10 @@ +{ + "triggers": { + "changed": { + "trigger": "mdi:water-percent" + }, + "crossed_threshold": { + "trigger": "mdi:water-percent" + } + } +} diff --git a/homeassistant/components/humidity/manifest.json b/homeassistant/components/humidity/manifest.json new file mode 100644 index 00000000000000..857036a96db7bf --- /dev/null +++ b/homeassistant/components/humidity/manifest.json @@ -0,0 +1,8 @@ +{ + "domain": "humidity", + "name": "Humidity", + "codeowners": ["@home-assistant/core"], + "documentation": "https://www.home-assistant.io/integrations/humidity", + "integration_type": "system", + "quality_scale": "internal" +} diff --git a/homeassistant/components/humidity/strings.json b/homeassistant/components/humidity/strings.json new file mode 100644 index 00000000000000..49d4126ff2584c --- /dev/null +++ b/homeassistant/components/humidity/strings.json @@ -0,0 +1,68 @@ +{ + "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": "Humidity", + "triggers": { + "changed": { + "description": "Triggers when the humidity changes.", + "fields": { + "above": { + "description": "Only trigger when humidity is above this value.", + "name": "Above" + }, + "below": { + "description": "Only trigger when humidity is below this value.", + "name": "Below" + } + }, + "name": "Humidity changed" + }, + "crossed_threshold": { + "description": "Triggers when the humidity crosses a threshold.", + "fields": { + "behavior": { + "description": "[%key:component::humidity::common::trigger_behavior_description%]", + "name": "[%key:component::humidity::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" + }, + "upper_limit": { + "description": "The upper limit of the threshold.", + "name": "Upper limit" + } + }, + "name": "Humidity crossed threshold" + } + } +} diff --git a/homeassistant/components/humidity/trigger.py b/homeassistant/components/humidity/trigger.py new file mode 100644 index 00000000000000..8596f4d0dd86a3 --- /dev/null +++ b/homeassistant/components/humidity/trigger.py @@ -0,0 +1,71 @@ +"""Provides triggers for humidity.""" + +from __future__ import annotations + +from homeassistant.components.climate import ( + ATTR_CURRENT_HUMIDITY as CLIMATE_ATTR_CURRENT_HUMIDITY, + DOMAIN as CLIMATE_DOMAIN, +) +from homeassistant.components.humidifier import ( + ATTR_CURRENT_HUMIDITY as HUMIDIFIER_ATTR_CURRENT_HUMIDITY, + DOMAIN as HUMIDIFIER_DOMAIN, +) +from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorDeviceClass +from homeassistant.components.weather import ( + ATTR_WEATHER_HUMIDITY, + DOMAIN as WEATHER_DOMAIN, +) +from homeassistant.core import HomeAssistant, split_entity_id +from homeassistant.helpers.trigger import ( + EntityNumericalStateAttributeChangedTriggerBase, + EntityNumericalStateAttributeCrossedThresholdTriggerBase, + EntityTriggerBase, + Trigger, + get_device_class_or_undefined, +) + + +class _HumidityTriggerMixin(EntityTriggerBase): + """Mixin for humidity triggers providing entity filtering and value extraction.""" + + _attributes = { + CLIMATE_DOMAIN: CLIMATE_ATTR_CURRENT_HUMIDITY, + HUMIDIFIER_DOMAIN: HUMIDIFIER_ATTR_CURRENT_HUMIDITY, + SENSOR_DOMAIN: None, # Use state.state + WEATHER_DOMAIN: ATTR_WEATHER_HUMIDITY, + } + _domains = {SENSOR_DOMAIN, CLIMATE_DOMAIN, HUMIDIFIER_DOMAIN, WEATHER_DOMAIN} + + def entity_filter(self, entities: set[str]) -> set[str]: + """Filter entities: all climate/humidifier/weather, sensor only with device_class humidity.""" + entities = super().entity_filter(entities) + return { + entity_id + for entity_id in entities + if split_entity_id(entity_id)[0] != SENSOR_DOMAIN + or get_device_class_or_undefined(self._hass, entity_id) + == SensorDeviceClass.HUMIDITY + } + + +class HumidityChangedTrigger( + _HumidityTriggerMixin, EntityNumericalStateAttributeChangedTriggerBase +): + """Trigger for humidity value changes across multiple domains.""" + + +class HumidityCrossedThresholdTrigger( + _HumidityTriggerMixin, EntityNumericalStateAttributeCrossedThresholdTriggerBase +): + """Trigger for humidity value crossing a threshold across multiple domains.""" + + +TRIGGERS: dict[str, type[Trigger]] = { + "changed": HumidityChangedTrigger, + "crossed_threshold": HumidityCrossedThresholdTrigger, +} + + +async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]: + """Return the triggers for humidity.""" + return TRIGGERS diff --git a/homeassistant/components/humidity/triggers.yaml b/homeassistant/components/humidity/triggers.yaml new file mode 100644 index 00000000000000..b1b1116ae8712f --- /dev/null +++ b/homeassistant/components/humidity/triggers.yaml @@ -0,0 +1,64 @@ +.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 + - number + - sensor + 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_target: &trigger_target + entity: + - domain: sensor + device_class: humidity + - domain: climate + - domain: humidifier + - domain: weather + +changed: + target: *trigger_target + fields: + above: *number_or_entity + below: *number_or_entity + +crossed_threshold: + target: *trigger_target + fields: + behavior: *trigger_behavior + threshold_type: *trigger_threshold_type + lower_limit: *number_or_entity + upper_limit: *number_or_entity diff --git a/homeassistant/components/indevolt/manifest.json b/homeassistant/components/indevolt/manifest.json index a91a0a4a6942c5..2e67b487bd60dc 100644 --- a/homeassistant/components/indevolt/manifest.json +++ b/homeassistant/components/indevolt/manifest.json @@ -1,7 +1,7 @@ { "domain": "indevolt", "name": "Indevolt", - "codeowners": ["@xirtnl"], + "codeowners": ["@xirt"], "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/indevolt", "integration_type": "device", diff --git a/homeassistant/components/light/trigger.py b/homeassistant/components/light/trigger.py index 61f90142d34208..da1d957422129d 100644 --- a/homeassistant/components/light/trigger.py +++ b/homeassistant/components/light/trigger.py @@ -24,7 +24,7 @@ class BrightnessChangedTrigger(EntityNumericalStateAttributeChangedTriggerBase): """Trigger for brightness changed.""" _domains = {DOMAIN} - _attribute = ATTR_BRIGHTNESS + _attributes = {DOMAIN: ATTR_BRIGHTNESS} _converter = staticmethod(_convert_uint8_to_percentage) @@ -35,7 +35,7 @@ class BrightnessCrossedThresholdTrigger( """Trigger for brightness crossed threshold.""" _domains = {DOMAIN} - _attribute = ATTR_BRIGHTNESS + _attributes = {DOMAIN: ATTR_BRIGHTNESS} _converter = staticmethod(_convert_uint8_to_percentage) diff --git a/homeassistant/components/litterrobot/__init__.py b/homeassistant/components/litterrobot/__init__.py index 14902a57aa50db..1a9fda45c287ef 100644 --- a/homeassistant/components/litterrobot/__init__.py +++ b/homeassistant/components/litterrobot/__init__.py @@ -3,10 +3,15 @@ from __future__ import annotations import itertools +import logging -from homeassistant.const import Platform +from pylitterbot import Account +from pylitterbot.exceptions import LitterRobotException + +from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.device_registry import DeviceEntry from homeassistant.helpers.typing import ConfigType @@ -14,6 +19,8 @@ from .coordinator import LitterRobotConfigEntry, LitterRobotDataUpdateCoordinator from .services import async_setup_services +_LOGGER = logging.getLogger(__name__) + CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) PLATFORMS = [ Platform.BINARY_SENSOR, @@ -33,6 +40,50 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: return True +async def async_migrate_entry( + hass: HomeAssistant, entry: LitterRobotConfigEntry +) -> bool: + """Migrate old entry.""" + _LOGGER.debug( + "Migrating configuration from version %s.%s", + entry.version, + entry.minor_version, + ) + + if entry.version > 1: + return False + + if entry.minor_version < 2: + account = Account(websession=async_get_clientsession(hass)) + try: + await account.connect( + username=entry.data[CONF_USERNAME], + password=entry.data[CONF_PASSWORD], + ) + user_id = account.user_id + except LitterRobotException: + _LOGGER.debug("Could not connect to set unique_id during migration") + return False + finally: + await account.disconnect() + + if user_id and not hass.config_entries.async_entry_for_domain_unique_id( + DOMAIN, user_id + ): + hass.config_entries.async_update_entry( + entry, unique_id=user_id, minor_version=2 + ) + else: + hass.config_entries.async_update_entry(entry, minor_version=2) + + _LOGGER.debug( + "Migration to configuration version %s.%s successful", + entry.version, + entry.minor_version, + ) + return True + + async def async_setup_entry(hass: HomeAssistant, entry: LitterRobotConfigEntry) -> bool: """Set up Litter-Robot from a config entry.""" coordinator = LitterRobotDataUpdateCoordinator(hass, entry) diff --git a/homeassistant/components/litterrobot/config_flow.py b/homeassistant/components/litterrobot/config_flow.py index 90f1fcba56d25e..149142ab7fe74b 100644 --- a/homeassistant/components/litterrobot/config_flow.py +++ b/homeassistant/components/litterrobot/config_flow.py @@ -27,8 +27,10 @@ class LitterRobotConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for Litter-Robot.""" VERSION = 1 + MINOR_VERSION = 2 username: str + _account_user_id: str | None = None async def async_step_reauth( self, entry_data: Mapping[str, Any] @@ -45,6 +47,8 @@ async def async_step_reauth_confirm( if user_input: user_input = user_input | {CONF_USERNAME: self.username} if not (error := await self._async_validate_input(user_input)): + await self.async_set_unique_id(self._account_user_id) + self._abort_if_unique_id_mismatch() return self.async_update_reload_and_abort( self._get_reauth_entry(), data_updates=user_input ) @@ -65,8 +69,9 @@ async def async_step_user( if user_input is not None: self._async_abort_entries_match({CONF_USERNAME: user_input[CONF_USERNAME]}) - if not (error := await self._async_validate_input(user_input)): + await self.async_set_unique_id(self._account_user_id) + self._abort_if_unique_id_configured() return self.async_create_entry( title=user_input[CONF_USERNAME], data=user_input ) @@ -92,4 +97,7 @@ async def _async_validate_input(self, user_input: Mapping[str, Any]) -> str: except Exception: _LOGGER.exception("Unexpected exception") return "unknown" + self._account_user_id = account.user_id + if not self._account_user_id: + return "unknown" return "" diff --git a/homeassistant/components/litterrobot/strings.json b/homeassistant/components/litterrobot/strings.json index f9e99b52b421cb..1ebe8490976a59 100644 --- a/homeassistant/components/litterrobot/strings.json +++ b/homeassistant/components/litterrobot/strings.json @@ -2,7 +2,8 @@ "config": { "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_account%]", - "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]" + "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]", + "unique_id_mismatch": "The Whisker account does not match the previously configured account. Please re-authenticate using the same account, or remove this integration and set it up again if you want to use a different account." }, "error": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", diff --git a/homeassistant/components/opendisplay/diagnostics.py b/homeassistant/components/opendisplay/diagnostics.py new file mode 100644 index 00000000000000..f4d5375b5c888c --- /dev/null +++ b/homeassistant/components/opendisplay/diagnostics.py @@ -0,0 +1,42 @@ +"""Diagnostics support for OpenDisplay.""" + +from __future__ import annotations + +import dataclasses +from typing import Any + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.core import HomeAssistant + +from . import OpenDisplayConfigEntry + +TO_REDACT = {"ssid", "password", "server_url"} + + +def _asdict(obj: Any) -> Any: + """Recursively convert a dataclass to a dict, encoding bytes as hex strings.""" + if dataclasses.is_dataclass(obj) and not isinstance(obj, type): + return {f.name: _asdict(getattr(obj, f.name)) for f in dataclasses.fields(obj)} + if isinstance(obj, bytes): + return obj.hex() + if isinstance(obj, list): + return [_asdict(item) for item in obj] + return obj + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: OpenDisplayConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + runtime = entry.runtime_data + fw = runtime.firmware + + return { + "firmware": { + "major": fw["major"], + "minor": fw["minor"], + "sha": fw["sha"], + }, + "is_flex": runtime.is_flex, + "device_config": async_redact_data(_asdict(runtime.device_config), TO_REDACT), + } diff --git a/homeassistant/components/opendisplay/quality_scale.yaml b/homeassistant/components/opendisplay/quality_scale.yaml index 28a9e851d22844..720ec101aac442 100644 --- a/homeassistant/components/opendisplay/quality_scale.yaml +++ b/homeassistant/components/opendisplay/quality_scale.yaml @@ -54,7 +54,7 @@ rules: # Gold devices: done - diagnostics: todo + diagnostics: done discovery-update-info: status: exempt comment: The device's BLE MAC address is both its unique identifier and does not change. diff --git a/homeassistant/components/saunum/__init__.py b/homeassistant/components/saunum/__init__.py index 208f0ab9861142..6248ac8dd721c3 100644 --- a/homeassistant/components/saunum/__init__.py +++ b/homeassistant/components/saunum/__init__.py @@ -2,7 +2,7 @@ from __future__ import annotations -from pysaunum import SaunumClient, SaunumConnectionError +from pysaunum import SaunumClient, SaunumConnectionError, SaunumTimeoutError from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST, Platform @@ -40,7 +40,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: LeilSaunaConfigEntry) -> try: client = await SaunumClient.create(host) - except SaunumConnectionError as exc: + except (SaunumConnectionError, SaunumTimeoutError) as exc: raise ConfigEntryNotReady(f"Error connecting to {host}: {exc}") from exc entry.async_on_unload(client.async_close) diff --git a/homeassistant/components/saunum/climate.py b/homeassistant/components/saunum/climate.py index 411d456c3c7b2c..f2615593cbc585 100644 --- a/homeassistant/components/saunum/climate.py +++ b/homeassistant/components/saunum/climate.py @@ -6,7 +6,14 @@ from datetime import timedelta from typing import Any -from pysaunum import MAX_TEMPERATURE, MIN_TEMPERATURE, SaunumException +from pysaunum import ( + DEFAULT_DURATION, + DEFAULT_FAN_DURATION, + DEFAULT_TEMPERATURE, + MAX_TEMPERATURE, + MIN_TEMPERATURE, + SaunumException, +) from homeassistant.components.climate import ( FAN_HIGH, @@ -149,7 +156,7 @@ def hvac_action(self) -> HVACAction | None: def preset_mode(self) -> str | None: """Return the current preset mode.""" sauna_type = self.coordinator.data.sauna_type - if sauna_type is not None and sauna_type in self._preset_name_map: + if sauna_type in self._preset_name_map: return self._preset_name_map[sauna_type] return self._preset_name_map[0] @@ -242,9 +249,9 @@ async def async_set_preset_mode(self, preset_mode: str) -> None: async def async_start_session( self, - duration: timedelta = timedelta(minutes=120), - target_temperature: int = 80, - fan_duration: timedelta = timedelta(minutes=10), + duration: timedelta = timedelta(minutes=DEFAULT_DURATION), + target_temperature: int = DEFAULT_TEMPERATURE, + fan_duration: timedelta = timedelta(minutes=DEFAULT_FAN_DURATION), ) -> None: """Start a sauna session with custom parameters.""" if self.coordinator.data.door_open: diff --git a/homeassistant/components/saunum/number.py b/homeassistant/components/saunum/number.py index 0a59127ffd64ea..d6da69deedebf4 100644 --- a/homeassistant/components/saunum/number.py +++ b/homeassistant/components/saunum/number.py @@ -7,6 +7,8 @@ from typing import TYPE_CHECKING from pysaunum import ( + DEFAULT_DURATION, + DEFAULT_FAN_DURATION, MAX_DURATION, MAX_FAN_DURATION, MIN_DURATION, @@ -35,10 +37,6 @@ PARALLEL_UPDATES = 0 -# Default values when device returns None or invalid data -DEFAULT_DURATION_MIN = 120 -DEFAULT_FAN_DURATION_MIN = 15 - @dataclass(frozen=True, kw_only=True) class LeilSaunaNumberEntityDescription(NumberEntityDescription): @@ -59,8 +57,8 @@ class LeilSaunaNumberEntityDescription(NumberEntityDescription): native_step=1, value_fn=lambda data: ( duration - if (duration := data.sauna_duration) is not None and duration > MIN_DURATION - else DEFAULT_DURATION_MIN + if (duration := data.sauna_duration) > MIN_DURATION + else DEFAULT_DURATION ), set_value_fn=lambda client, value: client.async_set_sauna_duration(int(value)), ), @@ -74,8 +72,8 @@ class LeilSaunaNumberEntityDescription(NumberEntityDescription): native_step=1, value_fn=lambda data: ( fan_dur - if (fan_dur := data.fan_duration) is not None and fan_dur > MIN_FAN_DURATION - else DEFAULT_FAN_DURATION_MIN + if (fan_dur := data.fan_duration) > MIN_FAN_DURATION + else DEFAULT_FAN_DURATION ), set_value_fn=lambda client, value: client.async_set_fan_duration(int(value)), ), diff --git a/homeassistant/components/saunum/services.py b/homeassistant/components/saunum/services.py index 88b074af15d927..c45c412e1647d3 100644 --- a/homeassistant/components/saunum/services.py +++ b/homeassistant/components/saunum/services.py @@ -4,7 +4,15 @@ from datetime import timedelta -from pysaunum import MAX_DURATION, MAX_FAN_DURATION, MAX_TEMPERATURE, MIN_TEMPERATURE +from pysaunum import ( + DEFAULT_DURATION, + DEFAULT_FAN_DURATION, + DEFAULT_TEMPERATURE, + MAX_DURATION, + MAX_FAN_DURATION, + MAX_TEMPERATURE, + MIN_TEMPERATURE, +) import voluptuous as vol from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN @@ -29,17 +37,21 @@ def async_setup_services(hass: HomeAssistant) -> None: SERVICE_START_SESSION, entity_domain=CLIMATE_DOMAIN, schema={ - vol.Optional(ATTR_DURATION, default=timedelta(minutes=120)): vol.All( + vol.Optional( + ATTR_DURATION, default=timedelta(minutes=DEFAULT_DURATION) + ): vol.All( cv.time_period, vol.Range( min=timedelta(minutes=1), max=timedelta(minutes=MAX_DURATION), ), ), - vol.Optional(ATTR_TARGET_TEMPERATURE, default=80): vol.All( + vol.Optional(ATTR_TARGET_TEMPERATURE, default=DEFAULT_TEMPERATURE): vol.All( cv.positive_int, vol.Range(min=MIN_TEMPERATURE, max=MAX_TEMPERATURE) ), - vol.Optional(ATTR_FAN_DURATION, default=timedelta(minutes=10)): vol.All( + vol.Optional( + ATTR_FAN_DURATION, default=timedelta(minutes=DEFAULT_FAN_DURATION) + ): vol.All( cv.time_period, vol.Range( min=timedelta(minutes=1), diff --git a/homeassistant/components/switchbot_cloud/__init__.py b/homeassistant/components/switchbot_cloud/__init__.py index a2e35c6ce5750e..9a218d79b9a24a 100644 --- a/homeassistant/components/switchbot_cloud/__init__.py +++ b/homeassistant/components/switchbot_cloud/__init__.py @@ -190,6 +190,8 @@ async def make_device_data( "Smart Lock Vision", "Smart Lock Vision Pro", "Smart Lock Pro Wifi", + "Lock Vision", + "Lock Vision Pro", ]: coordinator = await coordinator_for_device( hass, entry, api, device, coordinators_by_id diff --git a/homeassistant/components/switchbot_cloud/binary_sensor.py b/homeassistant/components/switchbot_cloud/binary_sensor.py index 5713c1e7f0301d..dac916c6caecb2 100644 --- a/homeassistant/components/switchbot_cloud/binary_sensor.py +++ b/homeassistant/components/switchbot_cloud/binary_sensor.py @@ -102,6 +102,14 @@ class SwitchBotCloudBinarySensorEntityDescription(BinarySensorEntityDescription) CALIBRATION_DESCRIPTION, DOOR_OPEN_DESCRIPTION, ), + "Lock Vision": ( + CALIBRATION_DESCRIPTION, + DOOR_OPEN_DESCRIPTION, + ), + "Lock Vision Pro": ( + CALIBRATION_DESCRIPTION, + DOOR_OPEN_DESCRIPTION, + ), "Smart Lock Pro Wifi": ( CALIBRATION_DESCRIPTION, DOOR_OPEN_DESCRIPTION, diff --git a/homeassistant/components/switchbot_cloud/sensor.py b/homeassistant/components/switchbot_cloud/sensor.py index 22b3b2b2390656..2bd6efa7daac1d 100644 --- a/homeassistant/components/switchbot_cloud/sensor.py +++ b/homeassistant/components/switchbot_cloud/sensor.py @@ -227,6 +227,8 @@ class SwitchbotCloudSensorEntityDescription(SensorEntityDescription): "Smart Lock Ultra": (BATTERY_DESCRIPTION,), "Smart Lock Vision": (BATTERY_DESCRIPTION,), "Smart Lock Vision Pro": (BATTERY_DESCRIPTION,), + "Lock Vision": (BATTERY_DESCRIPTION,), + "Lock Vision Pro": (BATTERY_DESCRIPTION,), "Smart Lock Pro Wifi": (BATTERY_DESCRIPTION,), "Relay Switch 2PM": ( RELAY_SWITCH_2PM_POWER_DESCRIPTION, diff --git a/homeassistant/components/vacuum/services.yaml b/homeassistant/components/vacuum/services.yaml index 2f14a5bd3c6c25..9764f86f556c28 100644 --- a/homeassistant/components/vacuum/services.yaml +++ b/homeassistant/components/vacuum/services.yaml @@ -81,6 +81,7 @@ clean_area: selector: area: multiple: true + reorder: true send_command: target: diff --git a/homeassistant/helpers/selector.py b/homeassistant/helpers/selector.py index 79843c6f3a2f56..12039143db1481 100644 --- a/homeassistant/helpers/selector.py +++ b/homeassistant/helpers/selector.py @@ -301,6 +301,7 @@ class AreaSelectorConfig(BaseSelectorConfig, total=False): entity: EntityFilterSelectorConfig | list[EntityFilterSelectorConfig] device: DeviceFilterSelectorConfig | list[DeviceFilterSelectorConfig] multiple: bool + reorder: bool @SELECTORS.register("area") @@ -320,6 +321,7 @@ class AreaSelector(Selector[AreaSelectorConfig]): [DEVICE_FILTER_SELECTOR_CONFIG_SCHEMA], ), vol.Optional("multiple", default=False): cv.boolean, + vol.Optional("reorder", default=False): cv.boolean, } ) diff --git a/homeassistant/helpers/trigger.py b/homeassistant/helpers/trigger.py index 26ee693af0e0bc..53215d3b265f22 100644 --- a/homeassistant/helpers/trigger.py +++ b/homeassistant/helpers/trigger.py @@ -73,6 +73,7 @@ get_relative_description_key, move_options_fields_to_top_level, ) +from .entity import get_device_class from .integration_platform import async_process_integration_platforms from .selector import TargetSelector from .target import ( @@ -80,7 +81,7 @@ async_track_target_selector_state_change_event, ) from .template import Template -from .typing import ConfigType, TemplateVarsType +from .typing import UNDEFINED, ConfigType, TemplateVarsType, UndefinedType _LOGGER = logging.getLogger(__name__) @@ -333,6 +334,16 @@ async def async_attach_runner( ) +def get_device_class_or_undefined( + hass: HomeAssistant, entity_id: str +) -> str | None | UndefinedType: + """Get the device class of an entity or UNDEFINED if not found.""" + try: + return get_device_class(hass, entity_id) + except HomeAssistantError: + return UNDEFINED + + class EntityTriggerBase(Trigger): """Trigger for entity state changes.""" @@ -600,17 +611,29 @@ def _get_numerical_value( return entity_or_float -class EntityNumericalStateAttributeChangedTriggerBase(EntityTriggerBase): - """Trigger for numerical state attribute changes.""" +class EntityNumericalStateBase(EntityTriggerBase): + """Base class for numerical state and state attribute triggers.""" + + _attributes: dict[str, str | None] + _converter: Callable[[Any], float] = float + + def _get_tracked_value(self, state: State) -> Any: + """Get the tracked numerical value from a state.""" + domain = split_entity_id(state.entity_id)[0] + source = self._attributes[domain] + if source is None: + return state.state + return state.attributes.get(source) + + +class EntityNumericalStateAttributeChangedTriggerBase(EntityNumericalStateBase): + """Trigger for numerical state and state attribute changes.""" - _attribute: str _schema = NUMERICAL_ATTRIBUTE_CHANGED_TRIGGER_SCHEMA _above: None | float | str _below: None | float | str - _converter: Callable[[Any], float] = float - def __init__(self, hass: HomeAssistant, config: TriggerConfig) -> None: """Initialize the state trigger.""" super().__init__(hass, config) @@ -622,20 +645,18 @@ def is_valid_transition(self, from_state: State, to_state: State) -> bool: if from_state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN): return False - return from_state.attributes.get(self._attribute) != to_state.attributes.get( - self._attribute - ) + return self._get_tracked_value(from_state) != self._get_tracked_value(to_state) # type: ignore[no-any-return] def is_valid_state(self, state: State) -> bool: - """Check if the new state attribute matches the expected one.""" - # Handle missing or None attribute case first to avoid expensive exceptions - if (_attribute_value := state.attributes.get(self._attribute)) is None: + """Check if the new state or state attribute matches the expected one.""" + # Handle missing or None value case first to avoid expensive exceptions + if (_attribute_value := self._get_tracked_value(state)) is None: return False try: current_value = self._converter(_attribute_value) except TypeError, ValueError: - # Attribute is not a valid number, don't trigger + # Value is not a valid number, don't trigger return False if self._above is not None: @@ -709,22 +730,21 @@ def _validate_limits_for_threshold_type(value: dict[str, Any]) -> dict[str, Any] ) -class EntityNumericalStateAttributeCrossedThresholdTriggerBase(EntityTriggerBase): - """Trigger for numerical state attribute changes. +class EntityNumericalStateAttributeCrossedThresholdTriggerBase( + EntityNumericalStateBase +): + """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. """ - _attribute: str _schema = NUMERICAL_ATTRIBUTE_CROSSED_THRESHOLD_SCHEMA _lower_limit: float | str | None = None _upper_limit: float | str | None = None _threshold_type: ThresholdType - _converter: Callable[[Any], float] = float - def __init__(self, hass: HomeAssistant, config: TriggerConfig) -> None: """Initialize the state trigger.""" super().__init__(hass, config) @@ -755,14 +775,14 @@ def is_valid_state(self, state: State) -> bool: # Entity not found or invalid number, don't trigger return False - # Handle missing or None attribute case first to avoid expensive exceptions - if (_attribute_value := state.attributes.get(self._attribute)) is None: + # Handle missing or None value case first to avoid expensive exceptions + if (_attribute_value := self._get_tracked_value(state)) is None: return False try: current_value = self._converter(_attribute_value) except TypeError, ValueError: - # Attribute is not a valid number, don't trigger + # Value is not a valid number, don't trigger return False # Note: We do not need to check for lower_limit/upper_limit being None here @@ -828,29 +848,29 @@ class CustomTrigger(EntityOriginStateTriggerBase): def make_entity_numerical_state_attribute_changed_trigger( - domain: str, attribute: str + domains: set[str], attributes: dict[str, str | None] ) -> type[EntityNumericalStateAttributeChangedTriggerBase]: """Create a trigger for numerical state attribute change.""" class CustomTrigger(EntityNumericalStateAttributeChangedTriggerBase): """Trigger for numerical state attribute changes.""" - _domains = {domain} - _attribute = attribute + _domains = domains + _attributes = attributes return CustomTrigger def make_entity_numerical_state_attribute_crossed_threshold_trigger( - domain: str, attribute: str + domains: set[str], attributes: dict[str, str | None] ) -> type[EntityNumericalStateAttributeCrossedThresholdTriggerBase]: """Create a trigger for numerical state attribute change.""" class CustomTrigger(EntityNumericalStateAttributeCrossedThresholdTriggerBase): """Trigger for numerical state attribute changes.""" - _domains = {domain} - _attribute = attribute + _domains = domains + _attributes = attributes return CustomTrigger diff --git a/script/hassfest/manifest.py b/script/hassfest/manifest.py index 420b1b61283b11..a59f24c97b8a8f 100644 --- a/script/hassfest/manifest.py +++ b/script/hassfest/manifest.py @@ -87,6 +87,7 @@ class NonScaledQualityScaleTiers(StrEnum): "homeassistant_hardware", "homeassistant_sky_connect", "homeassistant_yellow", + "humidity", "image_upload", "input_boolean", "input_button", diff --git a/script/hassfest/quality_scale.py b/script/hassfest/quality_scale.py index 6c2f91baa68957..2004413c9d9987 100644 --- a/script/hassfest/quality_scale.py +++ b/script/hassfest/quality_scale.py @@ -2121,6 +2121,7 @@ class Rule: "homeassistant_hardware", "homeassistant_sky_connect", "homeassistant_yellow", + "humidity", "image_upload", "input_boolean", "input_button", diff --git a/tests/components/__init__.py b/tests/components/__init__.py index 336314a7115b05..13ea3427a43d6a 100644 --- a/tests/components/__init__.py +++ b/tests/components/__init__.py @@ -635,6 +635,111 @@ def parametrize_numerical_attribute_crossed_threshold_trigger_states( ] +def parametrize_numerical_state_value_changed_trigger_states( + trigger: str, device_class: str +) -> list[tuple[str, dict[str, Any], list[TriggerStateDescription]]]: + """Parametrize states and expected service call counts for numerical state-value changed triggers. + + Unlike parametrize_numerical_attribute_changed_trigger_states, this is for + entities where the tracked numerical value is in state.state (e.g. sensor + entities), not in an attribute. + """ + from homeassistant.const import ATTR_DEVICE_CLASS # noqa: PLC0415 + + additional_attributes = {ATTR_DEVICE_CLASS: device_class} + return [ + *parametrize_trigger_states( + trigger=trigger, + trigger_options={}, + target_states=["0", "50", "100"], + other_states=["none"], + additional_attributes=additional_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"], + additional_attributes=additional_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"], + additional_attributes=additional_attributes, + retrigger_on_target_state=True, + trigger_from_none=False, + ), + ] + + +def parametrize_numerical_state_value_crossed_threshold_trigger_states( + trigger: str, device_class: str +) -> list[tuple[str, dict[str, Any], list[TriggerStateDescription]]]: + """Parametrize states and expected service call counts for numerical state-value crossed threshold triggers. + + Unlike parametrize_numerical_attribute_crossed_threshold_trigger_states, + this is for entities where the tracked numerical value is in state.state + (e.g. sensor entities), not in an attribute. + """ + from homeassistant.const import ATTR_DEVICE_CLASS # noqa: PLC0415 + + additional_attributes = {ATTR_DEVICE_CLASS: device_class} + return [ + *parametrize_trigger_states( + trigger=trigger, + trigger_options={ + CONF_THRESHOLD_TYPE: ThresholdType.BETWEEN, + CONF_LOWER_LIMIT: 10, + CONF_UPPER_LIMIT: 90, + }, + target_states=["50", "60"], + other_states=["none", "0", "100"], + additional_attributes=additional_attributes, + trigger_from_none=False, + ), + *parametrize_trigger_states( + trigger=trigger, + trigger_options={ + CONF_THRESHOLD_TYPE: ThresholdType.OUTSIDE, + CONF_LOWER_LIMIT: 10, + CONF_UPPER_LIMIT: 90, + }, + target_states=["0", "100"], + other_states=["none", "50", "60"], + additional_attributes=additional_attributes, + trigger_from_none=False, + ), + *parametrize_trigger_states( + trigger=trigger, + trigger_options={ + CONF_THRESHOLD_TYPE: ThresholdType.ABOVE, + CONF_LOWER_LIMIT: 10, + }, + target_states=["50", "100"], + other_states=["none", "0"], + additional_attributes=additional_attributes, + trigger_from_none=False, + ), + *parametrize_trigger_states( + trigger=trigger, + trigger_options={ + CONF_THRESHOLD_TYPE: ThresholdType.BELOW, + CONF_UPPER_LIMIT: 90, + }, + target_states=["0", "50"], + other_states=["none", "100"], + additional_attributes=additional_attributes, + trigger_from_none=False, + ), + ] + + async def arm_trigger( hass: HomeAssistant, trigger: str, diff --git a/tests/components/adax/conftest.py b/tests/components/adax/conftest.py index 026b9558a207b5..f2b110f5552836 100644 --- a/tests/components/adax/conftest.py +++ b/tests/components/adax/conftest.py @@ -47,11 +47,6 @@ } ] -LOCAL_DEVICE_DATA: dict[str, Any] = { - "current_temperature": 15, - "target_temperature": 20, -} - @pytest.fixture def mock_cloud_config_entry(request: pytest.FixtureRequest) -> MockConfigEntry: @@ -94,5 +89,9 @@ def mock_adax_local(): mock_adax_class = mock_adax.return_value mock_adax_class.get_status = AsyncMock() - mock_adax_class.get_status.return_value = LOCAL_DEVICE_DATA + mock_adax_class.get_status.return_value = { + "current_temperature": 15, + "target_temperature": 20, + } + mock_adax_class.set_target_temperature = AsyncMock() yield mock_adax_class diff --git a/tests/components/adax/test_climate.py b/tests/components/adax/test_climate.py index a5a93df74fa9c6..a15c79a21abed2 100644 --- a/tests/components/adax/test_climate.py +++ b/tests/components/adax/test_climate.py @@ -1,12 +1,24 @@ """Test Adax climate entity.""" from homeassistant.components.adax.const import SCAN_INTERVAL -from homeassistant.components.climate import ATTR_CURRENT_TEMPERATURE, HVACMode -from homeassistant.const import ATTR_TEMPERATURE, STATE_UNAVAILABLE, Platform +from homeassistant.components.climate import ( + ATTR_CURRENT_TEMPERATURE, + ATTR_HVAC_MODE, + DOMAIN as CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + SERVICE_SET_TEMPERATURE, + HVACMode, +) +from homeassistant.const import ( + ATTR_ENTITY_ID, + ATTR_TEMPERATURE, + STATE_UNAVAILABLE, + Platform, +) from homeassistant.core import HomeAssistant from . import setup_integration -from .conftest import CLOUD_DEVICE_DATA, LOCAL_DEVICE_DATA +from .conftest import CLOUD_DEVICE_DATA from tests.common import AsyncMock, MockConfigEntry, async_fire_time_changed from tests.test_setup import FrozenDateTimeFactory @@ -67,13 +79,8 @@ async def test_climate_local( state = hass.states.get(entity_id) assert state assert state.state == HVACMode.HEAT - assert ( - state.attributes[ATTR_TEMPERATURE] == (LOCAL_DEVICE_DATA["target_temperature"]) - ) - assert ( - state.attributes[ATTR_CURRENT_TEMPERATURE] - == (LOCAL_DEVICE_DATA["current_temperature"]) - ) + assert state.attributes[ATTR_TEMPERATURE] == 20 + assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 15 mock_adax_local.get_status.side_effect = Exception() freezer.tick(SCAN_INTERVAL) @@ -83,3 +90,152 @@ async def test_climate_local( state = hass.states.get(entity_id) assert state assert state.state == STATE_UNAVAILABLE + + +async def test_climate_local_initial_state_from_first_refresh( + hass: HomeAssistant, + mock_local_config_entry: MockConfigEntry, + mock_adax_local: AsyncMock, +) -> None: + """Test that local climate state is initialized from first refresh data.""" + await setup_integration(hass, mock_local_config_entry) + + assert len(hass.states.async_entity_ids(Platform.CLIMATE)) == 1 + entity_id = hass.states.async_entity_ids(Platform.CLIMATE)[0] + + state = hass.states.get(entity_id) + assert state + assert state.state == HVACMode.HEAT + assert state.attributes[ATTR_TEMPERATURE] == 20 + assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 15 + + +async def test_climate_local_initial_state_off_from_first_refresh( + hass: HomeAssistant, + mock_local_config_entry: MockConfigEntry, + mock_adax_local: AsyncMock, +) -> None: + """Test that local climate initializes correctly when first refresh reports off.""" + mock_adax_local.get_status.return_value["target_temperature"] = 0 + + await setup_integration(hass, mock_local_config_entry) + + assert len(hass.states.async_entity_ids(Platform.CLIMATE)) == 1 + entity_id = hass.states.async_entity_ids(Platform.CLIMATE)[0] + + state = hass.states.get(entity_id) + assert state + assert state.state == HVACMode.OFF + assert state.attributes[ATTR_TEMPERATURE] == 5 + assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 15 + + +async def test_climate_local_set_hvac_mode_updates_state_immediately( + hass: HomeAssistant, + mock_local_config_entry: MockConfigEntry, + mock_adax_local: AsyncMock, +) -> None: + """Test local hvac mode service updates both device and state immediately.""" + await setup_integration(hass, mock_local_config_entry) + + entity_id = hass.states.async_entity_ids(Platform.CLIMATE)[0] + + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + { + ATTR_ENTITY_ID: entity_id, + ATTR_HVAC_MODE: HVACMode.OFF, + }, + blocking=True, + ) + + mock_adax_local.set_target_temperature.assert_called_once_with(0) + state = hass.states.get(entity_id) + assert state + assert state.state == HVACMode.OFF + + mock_adax_local.set_target_temperature.reset_mock() + + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + { + ATTR_ENTITY_ID: entity_id, + ATTR_HVAC_MODE: HVACMode.HEAT, + }, + blocking=True, + ) + + mock_adax_local.set_target_temperature.assert_called_once_with(20) + state = hass.states.get(entity_id) + assert state + assert state.state == HVACMode.HEAT + + +async def test_climate_local_set_temperature_when_off_does_not_change_hvac_mode( + hass: HomeAssistant, + mock_local_config_entry: MockConfigEntry, + mock_adax_local: AsyncMock, +) -> None: + """Test setting target temperature while off does not send command or turn on.""" + await setup_integration(hass, mock_local_config_entry) + + entity_id = hass.states.async_entity_ids(Platform.CLIMATE)[0] + + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + { + ATTR_ENTITY_ID: entity_id, + ATTR_HVAC_MODE: HVACMode.OFF, + }, + blocking=True, + ) + mock_adax_local.set_target_temperature.reset_mock() + + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, + { + ATTR_ENTITY_ID: entity_id, + ATTR_TEMPERATURE: 23, + }, + blocking=True, + ) + + mock_adax_local.set_target_temperature.assert_not_called() + state = hass.states.get(entity_id) + assert state + assert state.state == HVACMode.OFF + assert state.attributes[ATTR_TEMPERATURE] == 23 + + +async def test_climate_local_set_temperature_when_heat_calls_device( + hass: HomeAssistant, + mock_local_config_entry: MockConfigEntry, + mock_adax_local: AsyncMock, +) -> None: + """Test setting target temperature while heating calls local API.""" + await setup_integration(hass, mock_local_config_entry) + + entity_id = hass.states.async_entity_ids(Platform.CLIMATE)[0] + state = hass.states.get(entity_id) + assert state + assert state.state == HVACMode.HEAT + + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, + { + ATTR_ENTITY_ID: entity_id, + ATTR_TEMPERATURE: 24, + }, + blocking=True, + ) + + mock_adax_local.set_target_temperature.assert_called_once_with(24) + state = hass.states.get(entity_id) + assert state + assert state.state == HVACMode.HEAT + assert state.attributes[ATTR_TEMPERATURE] == 24 diff --git a/tests/components/cover/test_trigger.py b/tests/components/cover/test_trigger.py new file mode 100644 index 00000000000000..9929b8847bb846 --- /dev/null +++ b/tests/components/cover/test_trigger.py @@ -0,0 +1,435 @@ +"""Test cover triggers.""" + +from typing import Any + +import pytest + +from homeassistant.components.cover import ATTR_IS_CLOSED, CoverState +from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_LABEL_ID, CONF_ENTITY_ID +from homeassistant.core import HomeAssistant, ServiceCall + +from tests.components import ( + TriggerStateDescription, + arm_trigger, + parametrize_target_entities, + parametrize_trigger_states, + set_or_remove_state, + target_entities, +) + +DEVICE_CLASS_TRIGGERS = [ + ("awning", "cover.awning_opened", "cover.awning_closed"), + ("blind", "cover.blind_opened", "cover.blind_closed"), + ("curtain", "cover.curtain_opened", "cover.curtain_closed"), + ("shade", "cover.shade_opened", "cover.shade_closed"), + ("shutter", "cover.shutter_opened", "cover.shutter_closed"), +] + + +@pytest.fixture +async def target_covers(hass: HomeAssistant) -> dict[str, list[str]]: + """Create multiple cover entities associated with different targets.""" + return await target_entities(hass, "cover") + + +@pytest.mark.parametrize( + "trigger_key", + [ + trigger + for _, opened, closed in DEVICE_CLASS_TRIGGERS + for trigger in (opened, closed) + ], +) +async def test_cover_triggers_gated_by_labs_flag( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture, trigger_key: str +) -> None: + """Test the cover triggers are gated by the labs flag.""" + await arm_trigger(hass, trigger_key, None, {ATTR_LABEL_ID: "test_label"}) + assert ( + "Unnamed automation failed to setup triggers and has been disabled: Trigger " + f"'{trigger_key}' requires the experimental 'New triggers and conditions' " + "feature to be enabled in Home Assistant Labs settings (feature flag: " + "'new_triggers_conditions')" + ) in caplog.text + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ("trigger_target_config", "entity_id", "entities_in_target"), + parametrize_target_entities("cover"), +) +@pytest.mark.parametrize( + ("trigger", "trigger_options", "states"), + [ + param + for device_class, opened_key, closed_key in DEVICE_CLASS_TRIGGERS + for param in ( + *parametrize_trigger_states( + trigger=opened_key, + target_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: False}), + (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + ], + other_states=[ + (CoverState.CLOSED, {ATTR_IS_CLOSED: True}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: True}), + ], + extra_invalid_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: None}), + (CoverState.OPEN, {}), + ], + additional_attributes={ATTR_DEVICE_CLASS: device_class}, + trigger_from_none=False, + ), + *parametrize_trigger_states( + trigger=closed_key, + target_states=[ + (CoverState.CLOSED, {ATTR_IS_CLOSED: True}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: True}), + ], + other_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: False}), + (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: False}), + ], + extra_invalid_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: None}), + (CoverState.OPEN, {}), + ], + additional_attributes={ATTR_DEVICE_CLASS: device_class}, + trigger_from_none=False, + ), + ) + ], +) +async def test_cover_trigger_behavior_any( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_covers: 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 cover trigger fires for cover entities with matching device_class.""" + other_entity_ids = set(target_covers["included"]) - {entity_id} + excluded_entity_ids = set(target_covers["excluded"]) - {entity_id} + + for eid in target_covers["included"]: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + for eid in excluded_entity_ids: + set_or_remove_state(hass, eid, states[0]["excluded"]) + await hass.async_block_till_done() + + await arm_trigger(hass, trigger, {}, trigger_target_config) + + for state in states[1:]: + excluded_state = state["excluded"] + included_state = state["included"] + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + for excluded_entity_id in excluded_entity_ids: + set_or_remove_state(hass, excluded_entity_id, excluded_state) + await hass.async_block_till_done() + assert len(service_calls) == (entities_in_target - 1) * state["count"] + service_calls.clear() + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ("trigger_target_config", "entity_id", "entities_in_target"), + parametrize_target_entities("cover"), +) +@pytest.mark.parametrize( + ("trigger", "trigger_options", "states"), + [ + param + for device_class, opened_key, closed_key in DEVICE_CLASS_TRIGGERS + for param in ( + *parametrize_trigger_states( + trigger=opened_key, + target_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: False}), + (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + ], + other_states=[ + (CoverState.CLOSED, {ATTR_IS_CLOSED: True}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: True}), + ], + extra_invalid_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: None}), + (CoverState.OPEN, {}), + ], + additional_attributes={ATTR_DEVICE_CLASS: device_class}, + trigger_from_none=False, + ), + *parametrize_trigger_states( + trigger=closed_key, + target_states=[ + (CoverState.CLOSED, {ATTR_IS_CLOSED: True}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: True}), + ], + other_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: False}), + (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: False}), + ], + extra_invalid_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: None}), + (CoverState.OPEN, {}), + ], + additional_attributes={ATTR_DEVICE_CLASS: device_class}, + trigger_from_none=False, + ), + ) + ], +) +async def test_cover_trigger_behavior_first( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_covers: 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 cover trigger fires on the first cover state change.""" + other_entity_ids = set(target_covers["included"]) - {entity_id} + excluded_entity_ids = set(target_covers["excluded"]) - {entity_id} + + for eid in target_covers["included"]: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + for eid in excluded_entity_ids: + set_or_remove_state(hass, eid, states[0]["excluded"]) + await hass.async_block_till_done() + + await arm_trigger(hass, trigger, {"behavior": "first"}, trigger_target_config) + + for state in states[1:]: + excluded_state = state["excluded"] + included_state = state["included"] + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, excluded_state) + await hass.async_block_till_done() + for excluded_entity_id in excluded_entity_ids: + set_or_remove_state(hass, excluded_entity_id, excluded_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ("trigger_target_config", "entity_id", "entities_in_target"), + parametrize_target_entities("cover"), +) +@pytest.mark.parametrize( + ("trigger", "trigger_options", "states"), + [ + param + for device_class, opened_key, closed_key in DEVICE_CLASS_TRIGGERS + for param in ( + *parametrize_trigger_states( + trigger=opened_key, + target_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: False}), + (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + ], + other_states=[ + (CoverState.CLOSED, {ATTR_IS_CLOSED: True}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: True}), + ], + extra_invalid_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: None}), + (CoverState.OPEN, {}), + ], + additional_attributes={ATTR_DEVICE_CLASS: device_class}, + trigger_from_none=False, + ), + *parametrize_trigger_states( + trigger=closed_key, + target_states=[ + (CoverState.CLOSED, {ATTR_IS_CLOSED: True}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: True}), + ], + other_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: False}), + (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: False}), + ], + extra_invalid_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: None}), + (CoverState.OPEN, {}), + ], + additional_attributes={ATTR_DEVICE_CLASS: device_class}, + trigger_from_none=False, + ), + ) + ], +) +async def test_cover_trigger_behavior_last( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_covers: 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 cover trigger fires when the last cover changes state.""" + other_entity_ids = set(target_covers["included"]) - {entity_id} + excluded_entity_ids = set(target_covers["excluded"]) - {entity_id} + + for eid in target_covers["included"]: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + for eid in excluded_entity_ids: + set_or_remove_state(hass, eid, states[0]["excluded"]) + await hass.async_block_till_done() + + await arm_trigger(hass, trigger, {"behavior": "last"}, trigger_target_config) + + for state in states[1:]: + excluded_state = state["excluded"] + included_state = state["included"] + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, excluded_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for excluded_entity_id in excluded_entity_ids: + set_or_remove_state(hass, excluded_entity_id, excluded_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ( + "trigger_key", + "device_class", + "wrong_device_class", + "cover_initial", + "cover_initial_is_closed", + "cover_target", + "cover_target_is_closed", + ), + [ + ( + opened_key, + device_class, + "damper", + CoverState.CLOSED, + True, + CoverState.OPEN, + False, + ) + for device_class, opened_key, _ in DEVICE_CLASS_TRIGGERS + ] + + [ + ( + closed_key, + device_class, + "damper", + CoverState.OPEN, + False, + CoverState.CLOSED, + True, + ) + for device_class, _, closed_key in DEVICE_CLASS_TRIGGERS + ], +) +async def test_cover_trigger_excludes_non_matching_device_class( + hass: HomeAssistant, + service_calls: list[ServiceCall], + trigger_key: str, + device_class: str, + wrong_device_class: str, + cover_initial: str, + cover_initial_is_closed: bool, + cover_target: str, + cover_target_is_closed: bool, +) -> None: + """Test cover trigger does not fire for entities without matching device_class.""" + entity_id_matching = "cover.test_matching" + entity_id_wrong = "cover.test_wrong" + + # Set initial states + hass.states.async_set( + entity_id_matching, + cover_initial, + {ATTR_DEVICE_CLASS: device_class, ATTR_IS_CLOSED: cover_initial_is_closed}, + ) + hass.states.async_set( + entity_id_wrong, + cover_initial, + { + ATTR_DEVICE_CLASS: wrong_device_class, + ATTR_IS_CLOSED: cover_initial_is_closed, + }, + ) + await hass.async_block_till_done() + + await arm_trigger( + hass, + trigger_key, + {}, + { + CONF_ENTITY_ID: [ + entity_id_matching, + entity_id_wrong, + ] + }, + ) + + # Matching device class changes - should trigger + hass.states.async_set( + entity_id_matching, + cover_target, + {ATTR_DEVICE_CLASS: device_class, ATTR_IS_CLOSED: cover_target_is_closed}, + ) + await hass.async_block_till_done() + assert len(service_calls) == 1 + assert service_calls[0].data[CONF_ENTITY_ID] == entity_id_matching + service_calls.clear() + + # Wrong device class changes - should NOT trigger + hass.states.async_set( + entity_id_wrong, + cover_target, + { + ATTR_DEVICE_CLASS: wrong_device_class, + ATTR_IS_CLOSED: cover_target_is_closed, + }, + ) + await hass.async_block_till_done() + assert len(service_calls) == 0 diff --git a/tests/components/door/test_trigger.py b/tests/components/door/test_trigger.py index 6602be111a2f8f..0a0f84627a64fa 100644 --- a/tests/components/door/test_trigger.py +++ b/tests/components/door/test_trigger.py @@ -158,6 +158,7 @@ async def test_door_trigger_binary_sensor_behavior_any( other_states=[ (CoverState.OPEN, {ATTR_IS_CLOSED: False}), (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: False}), ], extra_invalid_states=[ (CoverState.OPEN, {ATTR_IS_CLOSED: None}), @@ -382,6 +383,7 @@ async def test_door_trigger_binary_sensor_behavior_last( other_states=[ (CoverState.OPEN, {ATTR_IS_CLOSED: False}), (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: False}), ], extra_invalid_states=[ (CoverState.OPEN, {ATTR_IS_CLOSED: None}), @@ -469,6 +471,7 @@ async def test_door_trigger_cover_behavior_first( other_states=[ (CoverState.OPEN, {ATTR_IS_CLOSED: False}), (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: False}), ], extra_invalid_states=[ (CoverState.OPEN, {ATTR_IS_CLOSED: None}), diff --git a/tests/components/garage_door/test_trigger.py b/tests/components/garage_door/test_trigger.py index ae581eeedb9f07..cdb6db21f9d454 100644 --- a/tests/components/garage_door/test_trigger.py +++ b/tests/components/garage_door/test_trigger.py @@ -158,6 +158,7 @@ async def test_garage_door_trigger_binary_sensor_behavior_any( other_states=[ (CoverState.OPEN, {ATTR_IS_CLOSED: False}), (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: False}), ], extra_invalid_states=[ (CoverState.OPEN, {ATTR_IS_CLOSED: None}), @@ -382,6 +383,7 @@ async def test_garage_door_trigger_binary_sensor_behavior_last( other_states=[ (CoverState.OPEN, {ATTR_IS_CLOSED: False}), (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: False}), ], extra_invalid_states=[ (CoverState.OPEN, {ATTR_IS_CLOSED: None}), @@ -469,6 +471,7 @@ async def test_garage_door_trigger_cover_behavior_first( other_states=[ (CoverState.OPEN, {ATTR_IS_CLOSED: False}), (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: False}), ], extra_invalid_states=[ (CoverState.OPEN, {ATTR_IS_CLOSED: None}), diff --git a/tests/components/ghost/test_sensor.py b/tests/components/ghost/test_sensor.py index 2b85217219c979..30675ed9260be2 100644 --- a/tests/components/ghost/test_sensor.py +++ b/tests/components/ghost/test_sensor.py @@ -76,13 +76,14 @@ async def test_revenue_sensors_not_created_without_stripe( assert hass.states.get("sensor.test_ghost_arr") is None -async def test_newsletter_sensor_not_found( +async def test_newsletter_sensor_removed_when_stale( hass: HomeAssistant, + entity_registry: er.EntityRegistry, mock_ghost_api: AsyncMock, mock_config_entry: MockConfigEntry, freezer: FrozenDateTimeFactory, ) -> None: - """Test newsletter sensor when newsletter is removed.""" + """Test newsletter sensor is removed when newsletter disappears.""" await setup_integration(hass, mock_config_entry) # Verify newsletter sensor exists @@ -97,10 +98,35 @@ async def test_newsletter_sensor_not_found( async_fire_time_changed(hass) await hass.async_block_till_done(wait_background_tasks=True) - # Sensor should now be unavailable (newsletter not found) - state = hass.states.get("sensor.test_ghost_weekly_subscribers") - assert state is not None - assert state.state == STATE_UNAVAILABLE + # Entity should be removed from state and registry + assert hass.states.get("sensor.test_ghost_weekly_subscribers") is None + assert entity_registry.async_get("sensor.test_ghost_weekly_subscribers") is None + + +async def test_newsletter_sensor_removed_on_reload( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_ghost_api: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test stale newsletter sensor is removed when integration reloads.""" + await setup_integration(hass, mock_config_entry) + + # Verify newsletter sensor exists + assert entity_registry.async_get("sensor.test_ghost_weekly_subscribers") is not None + + # Unload the integration + await hass.config_entries.async_unload(mock_config_entry.entry_id) + await hass.async_block_till_done() + + # Newsletter is gone when integration reloads + mock_ghost_api.get_newsletters.return_value = [] + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + # Entity should be removed from registry + assert entity_registry.async_get("sensor.test_ghost_weekly_subscribers") is None async def test_entities_unavailable_on_update_failure( diff --git a/tests/components/humidity/__init__.py b/tests/components/humidity/__init__.py new file mode 100644 index 00000000000000..e70410932402bd --- /dev/null +++ b/tests/components/humidity/__init__.py @@ -0,0 +1 @@ +"""Tests for the humidity integration.""" diff --git a/tests/components/humidity/test_trigger.py b/tests/components/humidity/test_trigger.py new file mode 100644 index 00000000000000..1cd18773c35734 --- /dev/null +++ b/tests/components/humidity/test_trigger.py @@ -0,0 +1,791 @@ +"""Test humidity trigger.""" + +from typing import Any + +import pytest + +from homeassistant.components.climate import ( + ATTR_CURRENT_HUMIDITY as CLIMATE_ATTR_CURRENT_HUMIDITY, + HVACMode, +) +from homeassistant.components.humidifier import ( + ATTR_CURRENT_HUMIDITY as HUMIDIFIER_ATTR_CURRENT_HUMIDITY, +) +from homeassistant.components.weather import ATTR_WEATHER_HUMIDITY +from homeassistant.const import ( + ATTR_DEVICE_CLASS, + ATTR_LABEL_ID, + CONF_ENTITY_ID, + STATE_ON, +) +from homeassistant.core import HomeAssistant, ServiceCall + +from tests.components import ( + TriggerStateDescription, + arm_trigger, + 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, + set_or_remove_state, + target_entities, +) + + +@pytest.fixture +async def target_sensors(hass: HomeAssistant) -> list[str]: + """Create multiple sensor entities associated with different targets.""" + return (await target_entities(hass, "sensor"))["included"] + + +@pytest.fixture +async def target_climates(hass: HomeAssistant) -> list[str]: + """Create multiple climate entities associated with different targets.""" + return (await target_entities(hass, "climate"))["included"] + + +@pytest.fixture +async def target_humidifiers(hass: HomeAssistant) -> list[str]: + """Create multiple humidifier entities associated with different targets.""" + return (await target_entities(hass, "humidifier"))["included"] + + +@pytest.fixture +async def target_weathers(hass: HomeAssistant) -> list[str]: + """Create multiple weather entities associated with different targets.""" + return (await target_entities(hass, "weather"))["included"] + + +@pytest.mark.parametrize( + "trigger_key", + [ + "humidity.changed", + "humidity.crossed_threshold", + ], +) +async def test_humidity_triggers_gated_by_labs_flag( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture, trigger_key: str +) -> None: + """Test the humidity triggers are gated by the labs flag.""" + await arm_trigger(hass, trigger_key, None, {ATTR_LABEL_ID: "test_label"}) + assert ( + "Unnamed automation failed to setup triggers and has been disabled: Trigger " + f"'{trigger_key}' requires the experimental 'New triggers and conditions' " + "feature to be enabled in Home Assistant Labs settings (feature flag: " + "'new_triggers_conditions')" + ) in caplog.text + + +# --- 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( + "humidity.changed", "humidity" + ), + *parametrize_numerical_state_value_crossed_threshold_trigger_states( + "humidity.crossed_threshold", "humidity" + ), + ], +) +async def test_humidity_trigger_sensor_behavior_any( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_sensors: 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 humidity trigger fires for sensor entities with device_class humidity.""" + other_entity_ids = set(target_sensors) - {entity_id} + + for eid in target_sensors: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + + await arm_trigger(hass, trigger, trigger_options, trigger_target_config) + + for state in states[1:]: + included_state = state["included"] + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == (entities_in_target - 1) * state["count"] + service_calls.clear() + + +@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( + "humidity.crossed_threshold", "humidity" + ), + ], +) +async def test_humidity_trigger_sensor_crossed_threshold_behavior_first( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_sensors: 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 humidity crossed_threshold trigger fires on the first sensor state change.""" + other_entity_ids = set(target_sensors) - {entity_id} + + for eid in target_sensors: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + + await arm_trigger( + hass, trigger, {"behavior": "first"} | trigger_options, trigger_target_config + ) + + for state in states[1:]: + included_state = state["included"] + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + +@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( + "humidity.crossed_threshold", "humidity" + ), + ], +) +async def test_humidity_trigger_sensor_crossed_threshold_behavior_last( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_sensors: 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 humidity crossed_threshold trigger fires when the last sensor changes state.""" + other_entity_ids = set(target_sensors) - {entity_id} + + for eid in target_sensors: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + + await arm_trigger( + hass, trigger, {"behavior": "last"} | trigger_options, trigger_target_config + ) + + for state in states[1:]: + included_state = state["included"] + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + +# --- Climate domain tests (value in current_humidity 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( + "humidity.changed", HVACMode.AUTO, CLIMATE_ATTR_CURRENT_HUMIDITY + ), + *parametrize_numerical_attribute_crossed_threshold_trigger_states( + "humidity.crossed_threshold", + HVACMode.AUTO, + CLIMATE_ATTR_CURRENT_HUMIDITY, + ), + ], +) +async def test_humidity_trigger_climate_behavior_any( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_climates: 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 humidity trigger fires for climate entities.""" + other_entity_ids = set(target_climates) - {entity_id} + + for eid in target_climates: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + + await arm_trigger(hass, trigger, trigger_options, trigger_target_config) + + for state in states[1:]: + included_state = state["included"] + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == (entities_in_target - 1) * state["count"] + service_calls.clear() + + +@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( + "humidity.crossed_threshold", + HVACMode.AUTO, + CLIMATE_ATTR_CURRENT_HUMIDITY, + ), + ], +) +async def test_humidity_trigger_climate_crossed_threshold_behavior_first( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_climates: 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 humidity crossed_threshold trigger fires on the first climate state change.""" + other_entity_ids = set(target_climates) - {entity_id} + + for eid in target_climates: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + + await arm_trigger( + hass, trigger, {"behavior": "first"} | trigger_options, trigger_target_config + ) + + for state in states[1:]: + included_state = state["included"] + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + +@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( + "humidity.crossed_threshold", + HVACMode.AUTO, + CLIMATE_ATTR_CURRENT_HUMIDITY, + ), + ], +) +async def test_humidity_trigger_climate_crossed_threshold_behavior_last( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_climates: 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 humidity crossed_threshold trigger fires when the last climate changes state.""" + other_entity_ids = set(target_climates) - {entity_id} + + for eid in target_climates: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + + await arm_trigger( + hass, trigger, {"behavior": "last"} | trigger_options, trigger_target_config + ) + + for state in states[1:]: + included_state = state["included"] + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + +# --- Humidifier domain tests (value in current_humidity attribute) --- + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ("trigger_target_config", "entity_id", "entities_in_target"), + parametrize_target_entities("humidifier"), +) +@pytest.mark.parametrize( + ("trigger", "trigger_options", "states"), + [ + *parametrize_numerical_attribute_changed_trigger_states( + "humidity.changed", STATE_ON, HUMIDIFIER_ATTR_CURRENT_HUMIDITY + ), + *parametrize_numerical_attribute_crossed_threshold_trigger_states( + "humidity.crossed_threshold", + STATE_ON, + HUMIDIFIER_ATTR_CURRENT_HUMIDITY, + ), + ], +) +async def test_humidity_trigger_humidifier_behavior_any( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_humidifiers: 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 humidity trigger fires for humidifier entities.""" + other_entity_ids = set(target_humidifiers) - {entity_id} + + for eid in target_humidifiers: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + + await arm_trigger(hass, trigger, trigger_options, trigger_target_config) + + for state in states[1:]: + included_state = state["included"] + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == (entities_in_target - 1) * state["count"] + service_calls.clear() + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ("trigger_target_config", "entity_id", "entities_in_target"), + parametrize_target_entities("humidifier"), +) +@pytest.mark.parametrize( + ("trigger", "trigger_options", "states"), + [ + *parametrize_numerical_attribute_crossed_threshold_trigger_states( + "humidity.crossed_threshold", + STATE_ON, + HUMIDIFIER_ATTR_CURRENT_HUMIDITY, + ), + ], +) +async def test_humidity_trigger_humidifier_crossed_threshold_behavior_first( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_humidifiers: 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 humidity crossed_threshold trigger fires on the first humidifier state change.""" + other_entity_ids = set(target_humidifiers) - {entity_id} + + for eid in target_humidifiers: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + + await arm_trigger( + hass, trigger, {"behavior": "first"} | trigger_options, trigger_target_config + ) + + for state in states[1:]: + included_state = state["included"] + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ("trigger_target_config", "entity_id", "entities_in_target"), + parametrize_target_entities("humidifier"), +) +@pytest.mark.parametrize( + ("trigger", "trigger_options", "states"), + [ + *parametrize_numerical_attribute_crossed_threshold_trigger_states( + "humidity.crossed_threshold", + STATE_ON, + HUMIDIFIER_ATTR_CURRENT_HUMIDITY, + ), + ], +) +async def test_humidity_trigger_humidifier_crossed_threshold_behavior_last( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_humidifiers: 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 humidity crossed_threshold trigger fires when the last humidifier changes state.""" + other_entity_ids = set(target_humidifiers) - {entity_id} + + for eid in target_humidifiers: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + + await arm_trigger( + hass, trigger, {"behavior": "last"} | trigger_options, trigger_target_config + ) + + for state in states[1:]: + included_state = state["included"] + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + +# --- Weather domain tests (value in humidity 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( + "humidity.changed", "sunny", ATTR_WEATHER_HUMIDITY + ), + *parametrize_numerical_attribute_crossed_threshold_trigger_states( + "humidity.crossed_threshold", + "sunny", + ATTR_WEATHER_HUMIDITY, + ), + ], +) +async def test_humidity_trigger_weather_behavior_any( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_weathers: 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 humidity trigger fires for weather entities.""" + other_entity_ids = set(target_weathers) - {entity_id} + + for eid in target_weathers: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + + await arm_trigger(hass, trigger, trigger_options, trigger_target_config) + + for state in states[1:]: + included_state = state["included"] + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == (entities_in_target - 1) * state["count"] + service_calls.clear() + + +@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( + "humidity.crossed_threshold", + "sunny", + ATTR_WEATHER_HUMIDITY, + ), + ], +) +async def test_humidity_trigger_weather_crossed_threshold_behavior_first( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_weathers: 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 humidity crossed_threshold trigger fires on the first weather state change.""" + other_entity_ids = set(target_weathers) - {entity_id} + + for eid in target_weathers: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + + await arm_trigger( + hass, trigger, {"behavior": "first"} | trigger_options, trigger_target_config + ) + + for state in states[1:]: + included_state = state["included"] + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + +@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( + "humidity.crossed_threshold", + "sunny", + ATTR_WEATHER_HUMIDITY, + ), + ], +) +async def test_humidity_trigger_weather_crossed_threshold_behavior_last( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_weathers: 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 humidity crossed_threshold trigger fires when the last weather changes state.""" + other_entity_ids = set(target_weathers) - {entity_id} + + for eid in target_weathers: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + + await arm_trigger( + hass, trigger, {"behavior": "last"} | trigger_options, trigger_target_config + ) + + for state in states[1:]: + included_state = state["included"] + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + +# --- Device class exclusion test --- + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ( + "trigger_key", + "trigger_options", + "sensor_initial", + "sensor_target", + ), + [ + ( + "humidity.changed", + {}, + "50", + "60", + ), + ( + "humidity.crossed_threshold", + {"threshold_type": "above", "lower_limit": 10}, + "5", + "50", + ), + ], +) +async def test_humidity_trigger_excludes_non_humidity_sensor( + hass: HomeAssistant, + service_calls: list[ServiceCall], + trigger_key: str, + trigger_options: dict[str, Any], + sensor_initial: str, + sensor_target: str, +) -> None: + """Test humidity trigger does not fire for sensor entities without device_class humidity.""" + entity_id_humidity = "sensor.test_humidity" + entity_id_temperature = "sensor.test_temperature" + + # Set initial states + hass.states.async_set( + entity_id_humidity, sensor_initial, {ATTR_DEVICE_CLASS: "humidity"} + ) + hass.states.async_set( + entity_id_temperature, sensor_initial, {ATTR_DEVICE_CLASS: "temperature"} + ) + await hass.async_block_till_done() + + await arm_trigger( + hass, + trigger_key, + trigger_options, + { + CONF_ENTITY_ID: [ + entity_id_humidity, + entity_id_temperature, + ] + }, + ) + + # Humidity sensor changes - should trigger + hass.states.async_set( + entity_id_humidity, sensor_target, {ATTR_DEVICE_CLASS: "humidity"} + ) + await hass.async_block_till_done() + assert len(service_calls) == 1 + assert service_calls[0].data[CONF_ENTITY_ID] == entity_id_humidity + service_calls.clear() + + # Temperature sensor changes - should NOT trigger (wrong device class) + hass.states.async_set( + entity_id_temperature, sensor_target, {ATTR_DEVICE_CLASS: "temperature"} + ) + await hass.async_block_till_done() + assert len(service_calls) == 0 diff --git a/tests/components/litterrobot/common.py b/tests/components/litterrobot/common.py index a86c782a2ebb9b..b9780729d58229 100644 --- a/tests/components/litterrobot/common.py +++ b/tests/components/litterrobot/common.py @@ -6,6 +6,7 @@ BASE_PATH = "homeassistant.components.litterrobot" CONFIG = {DOMAIN: {CONF_USERNAME: "user@example.com", CONF_PASSWORD: "password"}} +ACCOUNT_USER_ID = "1234567" ROBOT_NAME = "Test" ROBOT_SERIAL = "LR3C012345" ROBOT_DATA = { diff --git a/tests/components/litterrobot/conftest.py b/tests/components/litterrobot/conftest.py index f13d0f82d2bd4e..af13c96a71c99d 100644 --- a/tests/components/litterrobot/conftest.py +++ b/tests/components/litterrobot/conftest.py @@ -13,6 +13,7 @@ from homeassistant.core import HomeAssistant from .common import ( + ACCOUNT_USER_ID, CONFIG, DOMAIN, FEEDER_ROBOT_DATA, @@ -79,6 +80,7 @@ def create_mock_account( account = MagicMock(spec=Account) account.connect = AsyncMock() account.refresh_robots = AsyncMock() + account.user_id = ACCOUNT_USER_ID account.robots = ( [] if skip_robots @@ -163,6 +165,9 @@ async def setup_integration( entry = MockConfigEntry( domain=DOMAIN, data=CONFIG[DOMAIN], + unique_id=ACCOUNT_USER_ID, + version=1, + minor_version=2, ) entry.add_to_hass(hass) diff --git a/tests/components/litterrobot/test_config_flow.py b/tests/components/litterrobot/test_config_flow.py index caaf832b7803e8..69527c8776094f 100644 --- a/tests/components/litterrobot/test_config_flow.py +++ b/tests/components/litterrobot/test_config_flow.py @@ -1,6 +1,6 @@ """Test the Litter-Robot config flow.""" -from unittest.mock import patch +from unittest.mock import PropertyMock, patch from pylitterbot import Account from pylitterbot.exceptions import LitterRobotException, LitterRobotLoginException @@ -11,7 +11,7 @@ from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from .common import CONF_USERNAME, CONFIG, DOMAIN +from .common import ACCOUNT_USER_ID, CONF_USERNAME, CONFIG, DOMAIN from tests.common import MockConfigEntry @@ -29,6 +29,11 @@ async def test_full_flow(hass: HomeAssistant, mock_account) -> None: "homeassistant.components.litterrobot.config_flow.Account.connect", return_value=mock_account, ), + patch( + "homeassistant.components.litterrobot.config_flow.Account.user_id", + new_callable=PropertyMock, + return_value=ACCOUNT_USER_ID, + ), patch( "homeassistant.components.litterrobot.async_setup_entry", return_value=True, @@ -41,14 +46,16 @@ async def test_full_flow(hass: HomeAssistant, mock_account) -> None: assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == CONFIG[DOMAIN][CONF_USERNAME] assert result["data"] == CONFIG[DOMAIN] + assert result["result"].unique_id == ACCOUNT_USER_ID assert len(mock_setup_entry.mock_calls) == 1 async def test_already_configured(hass: HomeAssistant) -> None: - """Test already configured case.""" + """Test already configured account is rejected before authentication.""" MockConfigEntry( domain=DOMAIN, data=CONFIG[DOMAIN], + unique_id=ACCOUNT_USER_ID, ).add_to_hass(hass) result = await hass.config_entries.flow.async_init( @@ -72,7 +79,7 @@ async def test_already_configured(hass: HomeAssistant) -> None: async def test_create_entry( hass: HomeAssistant, mock_account, side_effect, connect_errors ) -> None: - """Test creating an entry.""" + """Test creating an entry after error recovery.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) @@ -93,6 +100,11 @@ async def test_create_entry( "homeassistant.components.litterrobot.config_flow.Account.connect", return_value=mock_account, ), + patch( + "homeassistant.components.litterrobot.config_flow.Account.user_id", + new_callable=PropertyMock, + return_value=ACCOUNT_USER_ID, + ), patch( "homeassistant.components.litterrobot.async_setup_entry", return_value=True, @@ -105,6 +117,7 @@ async def test_create_entry( assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == CONFIG[DOMAIN][CONF_USERNAME] assert result["data"] == CONFIG[DOMAIN] + assert result["result"].unique_id == ACCOUNT_USER_ID async def test_reauth(hass: HomeAssistant, mock_account: Account) -> None: @@ -112,6 +125,7 @@ async def test_reauth(hass: HomeAssistant, mock_account: Account) -> None: entry = MockConfigEntry( domain=DOMAIN, data=CONFIG[DOMAIN], + unique_id=ACCOUNT_USER_ID, ) entry.add_to_hass(hass) @@ -137,6 +151,11 @@ async def test_reauth(hass: HomeAssistant, mock_account: Account) -> None: "homeassistant.components.litterrobot.config_flow.Account.connect", return_value=mock_account, ), + patch( + "homeassistant.components.litterrobot.config_flow.Account.user_id", + new_callable=PropertyMock, + return_value=ACCOUNT_USER_ID, + ), patch( "homeassistant.components.litterrobot.async_setup_entry", return_value=True, @@ -148,4 +167,37 @@ async def test_reauth(hass: HomeAssistant, mock_account: Account) -> None: ) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reauth_successful" + assert entry.unique_id == ACCOUNT_USER_ID assert len(mock_setup_entry.mock_calls) == 1 + + +async def test_reauth_wrong_account(hass: HomeAssistant) -> None: + """Test reauth flow aborts when credentials belong to a different account.""" + entry = MockConfigEntry( + domain=DOMAIN, + data=CONFIG[DOMAIN], + unique_id=ACCOUNT_USER_ID, + ) + entry.add_to_hass(hass) + + result = await entry.start_reauth_flow(hass) + + with ( + patch( + "homeassistant.components.litterrobot.config_flow.Account.connect", + ), + patch( + "homeassistant.components.litterrobot.config_flow.Account.user_id", + new_callable=PropertyMock, + return_value="different_user_id", + ), + ): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_PASSWORD: CONFIG[DOMAIN][CONF_PASSWORD]}, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "unique_id_mismatch" + assert entry.unique_id == ACCOUNT_USER_ID + assert entry.data == CONFIG[DOMAIN] diff --git a/tests/components/litterrobot/test_init.py b/tests/components/litterrobot/test_init.py index 9ba4acaa9357d7..c632baf1a7bada 100644 --- a/tests/components/litterrobot/test_init.py +++ b/tests/components/litterrobot/test_init.py @@ -16,7 +16,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.setup import async_setup_component -from .common import CONFIG, DOMAIN, VACUUM_ENTITY_ID +from .common import ACCOUNT_USER_ID, CONFIG, DOMAIN, VACUUM_ENTITY_ID from .conftest import setup_integration from tests.common import MockConfigEntry @@ -58,6 +58,9 @@ async def test_entry_not_setup( entry = MockConfigEntry( domain=DOMAIN, data=CONFIG[DOMAIN], + unique_id=ACCOUNT_USER_ID, + version=1, + minor_version=2, ) entry.add_to_hass(hass) @@ -69,6 +72,118 @@ async def test_entry_not_setup( assert entry.state is expected_state +async def test_unique_id_migration( + hass: HomeAssistant, mock_account: MagicMock +) -> None: + """Test that legacy entries get unique_id set during migration.""" + entry = MockConfigEntry( + domain=DOMAIN, + data=CONFIG[DOMAIN], + version=1, + minor_version=1, + ) + assert entry.unique_id is None + entry.add_to_hass(hass) + + with ( + patch( + "homeassistant.components.litterrobot.Account", + return_value=mock_account, + ), + patch( + "homeassistant.components.litterrobot.coordinator.Account", + return_value=mock_account, + ), + ): + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert entry.unique_id == ACCOUNT_USER_ID + assert entry.minor_version == 2 + mock_account.disconnect.assert_called_once() + + +async def test_unique_id_migration_unsupported_version( + hass: HomeAssistant, +) -> None: + """Test that migration fails for entries with version > 1.""" + entry = MockConfigEntry( + domain=DOMAIN, + data=CONFIG[DOMAIN], + unique_id=ACCOUNT_USER_ID, + version=2, + minor_version=1, + ) + entry.add_to_hass(hass) + + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert entry.state is ConfigEntryState.MIGRATION_ERROR + + +async def test_unique_id_migration_conflict( + hass: HomeAssistant, mock_account: MagicMock +) -> None: + """Test that migration skips unique_id when another entry owns it.""" + # First entry already has the unique_id + MockConfigEntry( + domain=DOMAIN, + data=CONFIG[DOMAIN], + unique_id=ACCOUNT_USER_ID, + ).add_to_hass(hass) + + # Second entry is legacy (no unique_id) + entry = MockConfigEntry( + domain=DOMAIN, + data=CONFIG[DOMAIN], + version=1, + minor_version=1, + ) + assert entry.unique_id is None + entry.add_to_hass(hass) + + with ( + patch( + "homeassistant.components.litterrobot.Account", + return_value=mock_account, + ), + patch( + "homeassistant.components.litterrobot.coordinator.Account", + return_value=mock_account, + ), + ): + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert entry.unique_id is None + assert entry.minor_version == 2 + + +async def test_unique_id_migration_connection_failure( + hass: HomeAssistant, +) -> None: + """Test that migration fails when the API is unreachable during unique_id backfill.""" + entry = MockConfigEntry( + domain=DOMAIN, + data=CONFIG[DOMAIN], + version=1, + minor_version=1, + ) + entry.add_to_hass(hass) + + with patch( + "homeassistant.components.litterrobot.Account.connect", + side_effect=LitterRobotException, + ): + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert entry.unique_id is None + assert entry.minor_version == 1 + assert entry.state is ConfigEntryState.MIGRATION_ERROR + + async def test_device_remove_devices( hass: HomeAssistant, hass_ws_client: WebSocketGenerator, diff --git a/tests/components/opendisplay/snapshots/test_diagnostics.ambr b/tests/components/opendisplay/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000000..423bea32cac022 --- /dev/null +++ b/tests/components/opendisplay/snapshots/test_diagnostics.ambr @@ -0,0 +1,78 @@ +# serializer version: 1 +# name: test_diagnostics + dict({ + 'device_config': dict({ + 'binary_inputs': list([ + ]), + 'data_buses': list([ + ]), + 'displays': list([ + dict({ + 'active_height_mm': 29, + 'active_width_mm': 67, + 'busy_pin': 255, + 'clk_pin': 0, + 'color_scheme': 1, + 'cs_pin': 255, + 'data_pin': 0, + 'dc_pin': 255, + 'display_technology': 0, + 'full_update_mC': 0, + 'instance_number': 0, + 'panel_ic_type': 0, + 'partial_update_support': 0, + 'pixel_height': 128, + 'pixel_width': 296, + 'reserved': '000000000000000000000000000000000000000000000000000000000000000000', + 'reserved_pins': '00000000000000', + 'reset_pin': 255, + 'rotation': 0, + 'tag_type': 0, + 'transmission_modes': 1, + }), + ]), + 'leds': list([ + ]), + 'loaded': False, + 'manufacturer': dict({ + 'board_revision': 0, + 'board_type': 1, + 'manufacturer_id': 1, + 'reserved': '000000000000000000000000000000000000', + }), + 'minor_version': 0, + 'power': dict({ + 'battery_capacity_mah': '000000', + 'battery_sense_enable_pin': 255, + 'battery_sense_flags': 0, + 'battery_sense_pin': 255, + 'capacity_estimator': 0, + 'deep_sleep_current_ua': 0, + 'deep_sleep_time_seconds': 0, + 'power_mode': 0, + 'reserved': '000000000000000000000000', + 'sleep_flags': 0, + 'sleep_timeout_ms': 0, + 'tx_power': 0, + 'voltage_scaling_factor': 0, + }), + 'sensors': list([ + ]), + 'system': dict({ + 'communication_modes': 0, + 'device_flags': 0, + 'ic_type': 0, + 'pwr_pin': 255, + 'reserved': '0000000000000000000000000000000000', + }), + 'version': 0, + 'wifi_config': None, + }), + 'firmware': dict({ + 'major': 1, + 'minor': 2, + 'sha': 'abc123', + }), + 'is_flex': True, + }) +# --- diff --git a/tests/components/opendisplay/test_diagnostics.py b/tests/components/opendisplay/test_diagnostics.py new file mode 100644 index 00000000000000..45cf0ab65ad805 --- /dev/null +++ b/tests/components/opendisplay/test_diagnostics.py @@ -0,0 +1,29 @@ +"""Test the OpenDisplay diagnostics.""" + +from unittest.mock import MagicMock + +from syrupy.assertion import SnapshotAssertion + +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_config_entry: MockConfigEntry, + mock_opendisplay_device: MagicMock, + snapshot: SnapshotAssertion, +) -> None: + """Test diagnostics output matches snapshot.""" + mock_config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + result = await get_diagnostics_for_config_entry( + hass, hass_client, mock_config_entry + ) + assert result == snapshot diff --git a/tests/components/saunum/test_number.py b/tests/components/saunum/test_number.py index 80b5dcd68fab00..03f81a109cbf02 100644 --- a/tests/components/saunum/test_number.py +++ b/tests/components/saunum/test_number.py @@ -5,7 +5,7 @@ from dataclasses import replace from unittest.mock import MagicMock -from pysaunum import SaunumException +from pysaunum import DEFAULT_DURATION, DEFAULT_FAN_DURATION, SaunumException import pytest from syrupy.assertion import SnapshotAssertion @@ -50,7 +50,7 @@ async def test_set_sauna_duration( # Verify initial state state = hass.states.get(entity_id) assert state is not None - assert state.state == "120" + assert state.state == str(DEFAULT_DURATION) # Set new duration await hass.services.async_call( @@ -75,7 +75,7 @@ async def test_set_fan_duration( # Verify initial state state = hass.states.get(entity_id) assert state is not None - assert state.state == "10" + assert state.state == str(DEFAULT_FAN_DURATION) # Set new duration await hass.services.async_call( @@ -151,13 +151,13 @@ async def test_number_with_default_duration( mock_config_entry: MockConfigEntry, mock_saunum_client: MagicMock, ) -> None: - """Test number entities use default when device returns None.""" - # Set duration to None (device hasn't set it yet) + """Test number entities use default when device returns 0.""" + # Set duration to 0 (device hasn't set it yet / sauna type default) base_data = mock_saunum_client.async_get_data.return_value mock_saunum_client.async_get_data.return_value = replace( base_data, - sauna_duration=None, - fan_duration=None, + sauna_duration=0, + fan_duration=0, ) mock_config_entry.add_to_hass(hass) @@ -167,11 +167,11 @@ async def test_number_with_default_duration( # Should show default values sauna_duration_state = hass.states.get("number.saunum_leil_sauna_duration") assert sauna_duration_state is not None - assert sauna_duration_state.state == "120" # DEFAULT_DURATION_MIN + assert sauna_duration_state.state == str(DEFAULT_DURATION) fan_duration_state = hass.states.get("number.saunum_leil_fan_duration") assert fan_duration_state is not None - assert fan_duration_state.state == "15" # DEFAULT_FAN_DURATION_MIN + assert fan_duration_state.state == str(DEFAULT_FAN_DURATION) async def test_number_with_valid_duration_from_device( diff --git a/tests/components/tuya/test_light.py b/tests/components/tuya/test_light.py index d0b541a02314ac..b255c710769719 100644 --- a/tests/components/tuya/test_light.py +++ b/tests/components/tuya/test_light.py @@ -11,6 +11,7 @@ from homeassistant.components.light import ( ATTR_BRIGHTNESS, + ATTR_COLOR_TEMP_KELVIN, ATTR_HS_COLOR, ATTR_WHITE, DOMAIN as LIGHT_DOMAIN, @@ -42,13 +43,11 @@ async def test_platform_setup_and_discovery( @pytest.mark.parametrize( - "mock_device_code", - ["dj_mki13ie507rlry4r"], -) -@pytest.mark.parametrize( - ("service", "service_data", "expected_commands"), + ("mock_device_code", "entity_id", "service", "service_data", "expected_commands"), [ ( + "dj_mki13ie507rlry4r", + "light.garage_light", SERVICE_TURN_ON, { ATTR_WHITE: True, @@ -60,6 +59,8 @@ async def test_platform_setup_and_discovery( ], ), ( + "dj_mki13ie507rlry4r", + "light.garage_light", SERVICE_TURN_ON, { ATTR_BRIGHTNESS: 150, @@ -70,6 +71,8 @@ async def test_platform_setup_and_discovery( ], ), ( + "dj_mki13ie507rlry4r", + "light.garage_light", SERVICE_TURN_ON, { ATTR_WHITE: True, @@ -82,6 +85,8 @@ async def test_platform_setup_and_discovery( ], ), ( + "dj_mki13ie507rlry4r", + "light.garage_light", SERVICE_TURN_ON, { ATTR_WHITE: 150, @@ -93,6 +98,8 @@ async def test_platform_setup_and_discovery( ], ), ( + "dj_mki13ie507rlry4r", + "light.garage_light", SERVICE_TURN_ON, { ATTR_BRIGHTNESS: 255, @@ -105,10 +112,26 @@ async def test_platform_setup_and_discovery( ], ), ( + "dj_mki13ie507rlry4r", + "light.garage_light", SERVICE_TURN_OFF, {}, [{"code": "switch_led", "value": False}], ), + ( + "dj_ilddqqih3tucdk68", + "light.ieskas", + SERVICE_TURN_ON, + { + ATTR_BRIGHTNESS: 255, + ATTR_COLOR_TEMP_KELVIN: 5000, + }, + [ + {"code": "switch_led", "value": True}, + {"code": "temp_value", "value": 220}, + {"code": "bright_value", "value": 255}, + ], + ), ], ) async def test_action( @@ -116,12 +139,12 @@ async def test_action( mock_manager: Manager, mock_config_entry: MockConfigEntry, mock_device: CustomerDevice, + entity_id: str, service: str, service_data: dict[str, Any], expected_commands: list[dict[str, Any]], ) -> None: """Test light action.""" - entity_id = "light.garage_light" await initialize_entry(hass, mock_manager, mock_config_entry, mock_device) state = hass.states.get(entity_id) diff --git a/tests/helpers/test_selector.py b/tests/helpers/test_selector.py index c34154fe356603..43c93dd0747507 100644 --- a/tests/helpers/test_selector.py +++ b/tests/helpers/test_selector.py @@ -383,7 +383,7 @@ def test_entity_selector_schema_error(schema) -> None: (None,), ), ( - {"multiple": True}, + {"multiple": True, "reorder": True}, ((["abc123", "def456"],)), (None, "abc123", ["abc123", None]), ), diff --git a/tests/helpers/test_trigger.py b/tests/helpers/test_trigger.py index 21ed040af246cb..f0abba6235d6b0 100644 --- a/tests/helpers/test_trigger.py +++ b/tests/helpers/test_trigger.py @@ -1249,7 +1249,7 @@ async def test_numerical_state_attribute_changed_trigger_config_validation( async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]: return { "test_trigger": make_entity_numerical_state_attribute_changed_trigger( - "test", "test_attribute" + {"test"}, {"test": "test_attribute"} ), } @@ -1277,7 +1277,7 @@ async def test_numerical_state_attribute_changed_error_handling( async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]: return { "attribute_changed": make_entity_numerical_state_attribute_changed_trigger( - "test", "test_attribute" + {"test"}, {"test": "test_attribute"} ), } @@ -1559,7 +1559,7 @@ async def test_numerical_state_attribute_crossed_threshold_trigger_config_valida async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]: return { "test_trigger": make_entity_numerical_state_attribute_crossed_threshold_trigger( - "test", "test_attribute" + {"test"}, {"test": "test_attribute"} ), } diff --git a/tests/snapshots/test_bootstrap.ambr b/tests/snapshots/test_bootstrap.ambr index 6cb765abd5b7cd..ea899a9e27bdd2 100644 --- a/tests/snapshots/test_bootstrap.ambr +++ b/tests/snapshots/test_bootstrap.ambr @@ -44,6 +44,7 @@ 'homeassistant.scene', 'http', 'humidifier', + 'humidity', 'image', 'image_processing', 'image_upload', @@ -143,6 +144,7 @@ 'homeassistant.scene', 'http', 'humidifier', + 'humidity', 'image', 'image_processing', 'image_upload',