Use report for deprecation warning in alarm_control_panel

This commit is contained in:
G Johansson 2024-11-13 17:39:02 +00:00
parent 5f68d405b2
commit 70947a4120
2 changed files with 35 additions and 24 deletions

View file

@ -35,6 +35,7 @@ from homeassistant.helpers.deprecation import (
from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.entity_platform import EntityPlatform
from homeassistant.helpers.frame import report
from homeassistant.helpers.typing import ConfigType
from homeassistant.util.hass_dict import HassKey
@ -163,7 +164,6 @@ class AlarmControlPanelEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_A
_alarm_control_panel_option_default_code: str | None = None
__alarm_legacy_state: bool = False
__alarm_legacy_state_reported: bool = False
def __init_subclass__(cls, **kwargs: Any) -> None:
"""Post initialisation processing."""
@ -180,9 +180,7 @@ class AlarmControlPanelEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_A
unless already reported.
"""
if __name == "_attr_state":
if self.__alarm_legacy_state_reported is not True:
self._report_deprecated_alarm_state_handling()
self.__alarm_legacy_state_reported = True
self._report_deprecated_alarm_state_handling()
return super().__setattr__(__name, __value)
@callback
@ -194,7 +192,7 @@ class AlarmControlPanelEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_A
) -> None:
"""Start adding an entity to a platform."""
super().add_to_platform_start(hass, platform, parallel_updates)
if self.__alarm_legacy_state and not self.__alarm_legacy_state_reported:
if self.__alarm_legacy_state:
self._report_deprecated_alarm_state_handling()
@callback
@ -203,19 +201,13 @@ class AlarmControlPanelEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_A
Integrations should implement alarm_state instead of using state directly.
"""
self.__alarm_legacy_state_reported = True
if "custom_components" in type(self).__module__:
# Do not report on core integrations as they have been fixed.
report_issue = "report it to the custom integration author."
_LOGGER.warning(
"Entity %s (%s) is setting state directly"
" which will stop working in HA Core 2025.11."
" Entities should implement the 'alarm_state' property and"
" return its state using the AlarmControlPanelState enum, please %s",
self.entity_id,
type(self),
report_issue,
)
report(
"is setting state directly which will stop working in HA Core 2025.11."
f" Entity {self.entity_id} ({type(self)}) should implement the 'alarm_state'"
" property and return its state using the AlarmControlPanelState enum.",
error_if_core=True,
error_if_integration=False,
)
@final
@property

View file

@ -25,7 +25,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers import entity_registry as er, frame
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import UNDEFINED, UndefinedType
@ -297,6 +297,7 @@ async def test_alarm_control_panel_with_default_code(
mock_alarm_control_panel_entity.calls_disarm.assert_called_with("1234")
@patch.object(frame, "_REPORTED_INTEGRATIONS", set())
async def test_alarm_control_panel_not_log_deprecated_state_warning(
hass: HomeAssistant,
mock_alarm_control_panel_entity: MockAlarmControlPanel,
@ -305,9 +306,13 @@ async def test_alarm_control_panel_not_log_deprecated_state_warning(
"""Test correctly using alarm_state doesn't log issue or raise repair."""
state = hass.states.get(mock_alarm_control_panel_entity.entity_id)
assert state is not None
assert "Entities should implement the 'alarm_state' property and" not in caplog.text
assert (
"the 'alarm_state' property and return its state using the AlarmControlPanelState enum"
not in caplog.text
)
@patch.object(frame, "_REPORTED_INTEGRATIONS", set())
async def test_alarm_control_panel_log_deprecated_state_warning_using_state_prop(
hass: HomeAssistant,
code_format: CodeFormat | None,
@ -386,9 +391,13 @@ async def test_alarm_control_panel_log_deprecated_state_warning_using_state_prop
state = hass.states.get(entity.entity_id)
assert state is not None
assert "Entities should implement the 'alarm_state' property and" in caplog.text
assert (
"the 'alarm_state' property and return its state using the AlarmControlPanelState enum"
in caplog.text
)
@patch.object(frame, "_REPORTED_INTEGRATIONS", set())
async def test_alarm_control_panel_log_deprecated_state_warning_using_attr_state_attr(
hass: HomeAssistant,
code_format: CodeFormat | None,
@ -466,7 +475,10 @@ async def test_alarm_control_panel_log_deprecated_state_warning_using_attr_state
state = hass.states.get(entity.entity_id)
assert state is not None
assert "Entities should implement the 'alarm_state' property and" not in caplog.text
assert (
"the 'alarm_state' property and return its state using the AlarmControlPanelState enum"
not in caplog.text
)
with patch.object(
MockLegacyAlarmControlPanel,
@ -477,7 +489,10 @@ async def test_alarm_control_panel_log_deprecated_state_warning_using_attr_state
hass, entity.entity_id, SERVICE_ALARM_DISARM
)
assert "Entities should implement the 'alarm_state' property and" in caplog.text
assert (
"the 'alarm_state' property and return its state using the AlarmControlPanelState enum"
in caplog.text
)
caplog.clear()
with patch.object(
MockLegacyAlarmControlPanel,
@ -488,9 +503,13 @@ async def test_alarm_control_panel_log_deprecated_state_warning_using_attr_state
hass, entity.entity_id, SERVICE_ALARM_DISARM
)
# Test we only log once
assert "Entities should implement the 'alarm_state' property and" not in caplog.text
assert (
"the 'alarm_state' property and return its state using the AlarmControlPanelState enum"
not in caplog.text
)
@patch.object(frame, "_REPORTED_INTEGRATIONS", set())
async def test_alarm_control_panel_deprecated_state_does_not_break_state(
hass: HomeAssistant,
code_format: CodeFormat | None,