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

View file

@ -25,7 +25,7 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError 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.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import UNDEFINED, UndefinedType 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") 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( async def test_alarm_control_panel_not_log_deprecated_state_warning(
hass: HomeAssistant, hass: HomeAssistant,
mock_alarm_control_panel_entity: MockAlarmControlPanel, 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.""" """Test correctly using alarm_state doesn't log issue or raise repair."""
state = hass.states.get(mock_alarm_control_panel_entity.entity_id) state = hass.states.get(mock_alarm_control_panel_entity.entity_id)
assert state is not None 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( async def test_alarm_control_panel_log_deprecated_state_warning_using_state_prop(
hass: HomeAssistant, hass: HomeAssistant,
code_format: CodeFormat | None, 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) state = hass.states.get(entity.entity_id)
assert state is not None 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( async def test_alarm_control_panel_log_deprecated_state_warning_using_attr_state_attr(
hass: HomeAssistant, hass: HomeAssistant,
code_format: CodeFormat | None, 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) state = hass.states.get(entity.entity_id)
assert state is not None 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( with patch.object(
MockLegacyAlarmControlPanel, 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 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() caplog.clear()
with patch.object( with patch.object(
MockLegacyAlarmControlPanel, 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 hass, entity.entity_id, SERVICE_ALARM_DISARM
) )
# Test we only log once # 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( async def test_alarm_control_panel_deprecated_state_does_not_break_state(
hass: HomeAssistant, hass: HomeAssistant,
code_format: CodeFormat | None, code_format: CodeFormat | None,