From a46cc82916d0850c5d596749adafb8151a72c88e Mon Sep 17 00:00:00 2001 From: G Johansson Date: Mon, 7 Jul 2025 16:52:29 +0200 Subject: [PATCH] Don't log deprecation warning in vacuum until after entity added to hass (#147959) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Martin Hjelmare Co-authored-by: AbĂ­lio Costa --- homeassistant/components/vacuum/__init__.py | 46 +++++++++++---------- tests/components/vacuum/test_init.py | 37 ++++++++++++++--- 2 files changed, 56 insertions(+), 27 deletions(-) diff --git a/homeassistant/components/vacuum/__init__.py b/homeassistant/components/vacuum/__init__.py index 9108fc5d879..4b7a6907455 100644 --- a/homeassistant/components/vacuum/__init__.py +++ b/homeassistant/components/vacuum/__init__.py @@ -321,16 +321,18 @@ class StateVacuumEntity( Integrations should implement a sensor instead. """ - report_usage( - f"is setting the {property} which has been deprecated." - f" Integration {self.platform.platform_name} should implement a sensor" - " instead with a correct device class and link it to the same device", - core_integration_behavior=ReportBehavior.LOG, - custom_integration_behavior=ReportBehavior.LOG, - breaks_in_ha_version="2026.8", - integration_domain=self.platform.platform_name if self.platform else None, - exclude_integrations={DOMAIN}, - ) + if self.platform: + # Don't report usage until after entity added to hass, after init + report_usage( + f"is setting the {property} which has been deprecated." + f" Integration {self.platform.platform_name} should implement a sensor" + " instead with a correct device class and link it to the same device", + core_integration_behavior=ReportBehavior.LOG, + custom_integration_behavior=ReportBehavior.LOG, + breaks_in_ha_version="2026.8", + integration_domain=self.platform.platform_name, + exclude_integrations={DOMAIN}, + ) @callback def _report_deprecated_battery_feature(self) -> None: @@ -339,17 +341,19 @@ class StateVacuumEntity( Integrations should remove the battery supported feature when migrating battery level and icon to a sensor. """ - report_usage( - f"is setting the battery supported feature which has been deprecated." - f" Integration {self.platform.platform_name} should remove this as part of migrating" - " the battery level and icon to a sensor", - core_behavior=ReportBehavior.LOG, - core_integration_behavior=ReportBehavior.LOG, - custom_integration_behavior=ReportBehavior.LOG, - breaks_in_ha_version="2026.8", - integration_domain=self.platform.platform_name if self.platform else None, - exclude_integrations={DOMAIN}, - ) + if self.platform: + # Don't report usage until after entity added to hass, after init + report_usage( + f"is setting the battery supported feature which has been deprecated." + f" Integration {self.platform.platform_name} should remove this as part of migrating" + " the battery level and icon to a sensor", + core_behavior=ReportBehavior.LOG, + core_integration_behavior=ReportBehavior.LOG, + custom_integration_behavior=ReportBehavior.LOG, + breaks_in_ha_version="2026.8", + integration_domain=self.platform.platform_name, + exclude_integrations={DOMAIN}, + ) @cached_property def battery_level(self) -> int | None: diff --git a/tests/components/vacuum/test_init.py b/tests/components/vacuum/test_init.py index 488852521ed..60ff0a1ebde 100644 --- a/tests/components/vacuum/test_init.py +++ b/tests/components/vacuum/test_init.py @@ -562,16 +562,10 @@ async def test_vacuum_log_deprecated_battery_properties_using_attr( # Test we only log once assert ( "Detected that custom integration 'test' is setting the battery_level which has been deprecated." - " Integration test should implement a sensor instead with a correct device class and link it to" - " the same device. This will stop working in Home Assistant 2026.8," - " please report it to the author of the 'test' custom integration" not in caplog.text ) assert ( "Detected that custom integration 'test' is setting the battery_icon which has been deprecated." - " Integration test should implement a sensor instead with a correct device class and link it to" - " the same device. This will stop working in Home Assistant 2026.8," - " please report it to the author of the 'test' custom integration" not in caplog.text ) @@ -613,3 +607,34 @@ async def test_vacuum_log_deprecated_battery_supported_feature( ", please report it to the author of the 'test' custom integration" in caplog.text ) + + +async def test_vacuum_not_log_deprecated_battery_properties_during_init( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test not logging deprecation until after added to hass.""" + + class MockLegacyVacuum(MockVacuum): + """Mocked vacuum entity.""" + + def __init__(self, **kwargs: Any) -> None: + """Initialize a mock vacuum entity.""" + super().__init__(**kwargs) + self._attr_battery_level = 50 + + @property + def activity(self) -> str: + """Return the state of the entity.""" + return VacuumActivity.CLEANING + + entity = MockLegacyVacuum( + name="Testing", + entity_id="vacuum.test", + ) + assert entity.battery_level == 50 + + assert ( + "Detected that custom integration 'test' is setting the battery_level which has been deprecated." + not in caplog.text + )