diff --git a/homeassistant/components/sun/sensor.py b/homeassistant/components/sun/sensor.py index a042adb9b83..9c219d78efc 100644 --- a/homeassistant/components/sun/sensor.py +++ b/homeassistant/components/sun/sensor.py @@ -18,6 +18,11 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback +from homeassistant.helpers.issue_registry import ( + IssueSeverity, + async_create_issue, + async_delete_issue, +) from homeassistant.helpers.typing import StateType from .const import DOMAIN, SIGNAL_EVENTS_CHANGED, SIGNAL_POSITION_CHANGED @@ -149,6 +154,21 @@ class SunSensor(SensorEntity): async def async_added_to_hass(self) -> None: """Register signal listener when added to hass.""" await super().async_added_to_hass() + + if self.entity_description.key == "solar_rising": + async_create_issue( + self.hass, + DOMAIN, + "deprecated_sun_solar_rising", + breaks_in_ha_version="2026.1.0", + is_fixable=False, + severity=IssueSeverity.WARNING, + translation_key="deprecated_sun_solar_rising", + translation_placeholders={ + "entity": self.entity_id, + }, + ) + self.async_on_remove( async_dispatcher_connect( self.hass, @@ -156,3 +176,9 @@ class SunSensor(SensorEntity): self.async_write_ha_state, ) ) + + async def async_will_remove_from_hass(self) -> None: + """Call when entity will be removed from hass.""" + await super().async_will_remove_from_hass() + if self.entity_description.key == "solar_rising": + async_delete_issue(self.hass, DOMAIN, "deprecated_sun_solar_rising") diff --git a/homeassistant/components/sun/strings.json b/homeassistant/components/sun/strings.json index 14f612b7b50..e703e58e942 100644 --- a/homeassistant/components/sun/strings.json +++ b/homeassistant/components/sun/strings.json @@ -37,5 +37,11 @@ } } } + }, + "issues": { + "deprecated_sun_solar_rising": { + "title": "Deprecated 'Solar rising' sensor", + "description": "The 'Solar rising' sensor of the Sun integration is being deprecated; an equivalent 'Solar rising' binary sensor has been made available as a replacement. To resolve this issue, disable {entity}." + } } } diff --git a/tests/components/sun/test_sensor.py b/tests/components/sun/test_sensor.py index 59e4e4c700b..95f4364f775 100644 --- a/tests/components/sun/test_sensor.py +++ b/tests/components/sun/test_sensor.py @@ -8,12 +8,15 @@ from freezegun.api import FrozenDateTimeFactory import pytest from homeassistant.components import sun +from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant -from homeassistant.helpers import entity_registry as er +from homeassistant.helpers import entity_registry as er, issue_registry as ir from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util +from tests.common import async_fire_time_changed + @pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_setting_rising( @@ -179,3 +182,46 @@ async def test_setting_rising( assert entity assert entity.entity_category is EntityCategory.DIAGNOSTIC assert entity.unique_id == f"{entry_ids[0].entry_id}-solar_rising" + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_deprecation( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + issue_registry: ir.IssueRegistry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test sensor.sun_solar_rising deprecation.""" + utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC) + freezer.move_to(utc_now) + await async_setup_component(hass, sun.DOMAIN, {sun.DOMAIN: {}}) + await hass.async_block_till_done() + + assert issue_registry.async_get_issue( + domain="sun", + issue_id="deprecated_sun_solar_rising", + ) + assert len(issue_registry.issues) == 1 + + entity_registry.async_update_entity( + "sensor.sun_solar_rising", disabled_by=er.RegistryEntryDisabler.USER + ) + await hass.async_block_till_done() + + assert not issue_registry.async_get_issue( + domain="sun", + issue_id="deprecated_sun_solar_rising", + ) + assert len(issue_registry.issues) == 0 + + entity_registry.async_update_entity("sensor.sun_solar_rising", disabled_by=None) + await hass.async_block_till_done() + freezer.tick(delta=RELOAD_AFTER_UPDATE_DELAY) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert issue_registry.async_get_issue( + domain="sun", + issue_id="deprecated_sun_solar_rising", + ) + assert len(issue_registry.issues) == 1