Added deprecation to binary door sensor at Home Connect (#129245)

Co-authored-by: Joostlek <joostlek@outlook.com>
This commit is contained in:
J. Diego Rodríguez Royo 2024-10-29 12:46:39 +01:00 committed by GitHub
parent 983cd9c3fc
commit 0e959b3019
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 100 additions and 0 deletions

View File

@ -3,14 +3,17 @@
from dataclasses import dataclass
import logging
from homeassistant.components.automation import automations_with_entity
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.components.script import scripts_with_entity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from .api import HomeConnectDevice
from .const import (
@ -181,3 +184,25 @@ class HomeConnectDoorBinarySensor(HomeConnectBinarySensor):
)
self._attr_unique_id = f"{device.appliance.haId}-Door"
self._attr_name = f"{device.appliance.name} Door"
async def async_added_to_hass(self) -> None:
"""Call when entity is added to hass."""
await super().async_added_to_hass()
entity_automations = automations_with_entity(self.hass, self.entity_id)
entity_scripts = scripts_with_entity(self.hass, self.entity_id)
items = entity_automations + entity_scripts
if not items:
return
async_create_issue(
self.hass,
DOMAIN,
f"deprecated_binary_common_door_sensor_{self.entity_id}",
breaks_in_ha_version="2025.5.0",
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key="deprecated_binary_common_door_sensor",
translation_placeholders={
"entity": self.entity_id,
"items": "\n".join([f"- {item}" for item in items]),
},
)

View File

@ -65,6 +65,12 @@
"message": "{appliance_name} does not support turning off or entering standby mode."
}
},
"issues": {
"deprecated_binary_common_door_sensor": {
"title": "Deprecated binary door sensor detected in some automations or scripts",
"description": "The binary door sensor `{entity}`, which is deprecated, is used in the following automations or scripts:\n{items}\n\nA sensor entity with additional possible states is available and should be used going forward; Please use it on the above automations or scripts to fix this issue."
}
},
"services": {
"start_program": {
"name": "Start program",

View File

@ -6,19 +6,25 @@ from unittest.mock import MagicMock, Mock
from homeconnect.api import HomeConnectAPI
import pytest
from homeassistant.components import automation, script
from homeassistant.components.automation import automations_with_entity
from homeassistant.components.home_connect.const import (
BSH_DOOR_STATE,
BSH_DOOR_STATE_CLOSED,
BSH_DOOR_STATE_LOCKED,
BSH_DOOR_STATE_OPEN,
DOMAIN,
REFRIGERATION_STATUS_DOOR_CLOSED,
REFRIGERATION_STATUS_DOOR_OPEN,
REFRIGERATION_STATUS_DOOR_REFRIGERATOR,
)
from homeassistant.components.script import scripts_with_entity
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_component import async_update_entity
import homeassistant.helpers.issue_registry as ir
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, load_json_object_fixture
@ -130,3 +136,66 @@ async def test_bianry_sensors_fridge_door_states(
await async_update_entity(hass, entity_id)
await hass.async_block_till_done()
assert hass.states.is_state(entity_id, expected)
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
@pytest.mark.usefixtures("bypass_throttle")
async def test_create_issue(
hass: HomeAssistant,
appliance: Mock,
config_entry: MockConfigEntry,
integration_setup: Callable[[], Awaitable[bool]],
setup_credentials: None,
get_appliances: MagicMock,
issue_registry: ir.IssueRegistry,
) -> None:
"""Test we create an issue when an automation or script is using a deprecated entity."""
entity_id = "binary_sensor.washer_door"
get_appliances.return_value = [appliance]
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: {
"alias": "test",
"trigger": {"platform": "state", "entity_id": entity_id},
"action": {
"action": "automation.turn_on",
"target": {
"entity_id": "automation.test",
},
},
}
},
)
assert await async_setup_component(
hass,
script.DOMAIN,
{
script.DOMAIN: {
"test": {
"sequence": [
{
"condition": "state",
"entity_id": entity_id,
"state": "on",
},
],
}
}
},
)
assert config_entry.state == ConfigEntryState.NOT_LOADED
appliance.status.update({BSH_DOOR_STATE: {"value": BSH_DOOR_STATE_OPEN}})
assert await integration_setup()
assert config_entry.state == ConfigEntryState.LOADED
assert automations_with_entity(hass, entity_id)[0] == "automation.test"
assert scripts_with_entity(hass, entity_id)[0] == "script.test"
assert len(issue_registry.issues) == 1
assert issue_registry.async_get_issue(
DOMAIN, f"deprecated_binary_common_door_sensor_{entity_id}"
)