From 45fde81b87b602f67fcdb93b0774a1700053d85c Mon Sep 17 00:00:00 2001 From: G Johansson Date: Tue, 14 Apr 2026 19:34:11 +0000 Subject: [PATCH] Mods --- homeassistant/components/min_max/__init__.py | 14 ++- .../min_max/snapshots/test_init.ambr | 31 +++++ tests/components/min_max/test_config_flow.py | 116 +----------------- tests/components/min_max/test_init.py | 39 +++--- tests/components/min_max/test_sensor.py | 24 +++- 5 files changed, 95 insertions(+), 129 deletions(-) create mode 100644 tests/components/min_max/snapshots/test_init.ambr diff --git a/homeassistant/components/min_max/__init__.py b/homeassistant/components/min_max/__init__.py index b2dce0f596f6..65910a480f59 100644 --- a/homeassistant/components/min_max/__init__.py +++ b/homeassistant/components/min_max/__init__.py @@ -1,5 +1,6 @@ """The min_max component.""" +from datetime import datetime import logging from types import MappingProxyType @@ -15,6 +16,7 @@ from homeassistant.config_entries import SOURCE_USER, ConfigEntry from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er +from homeassistant.helpers.event import async_call_later from .const import CONF_ENTITY_IDS, CONF_ROUND_DIGITS, DOMAIN @@ -47,7 +49,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: version=1, ) await hass.config_entries.async_add(new_config_entry) - await hass.config_entries.async_remove(entry.entry_id) # Migrate entity from old entry to new entry entity_reg = er.async_get(hass) @@ -58,9 +59,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: old_entity, GROUP_DOMAIN, new_config_entry_id=new_config_entry.entry_id ) + # await hass.config_entries.async_remove(entry.entry_id) + + async def remove_old_entry(now: datetime) -> None: + """Remove the old config entry after migration.""" + await hass.config_entries.async_remove(entry.entry_id) + + async_call_later(hass, 60, remove_old_entry) + return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" - return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) + + return True diff --git a/tests/components/min_max/snapshots/test_init.ambr b/tests/components/min_max/snapshots/test_init.ambr new file mode 100644 index 000000000000..7c70f223ea57 --- /dev/null +++ b/tests/components/min_max/snapshots/test_init.ambr @@ -0,0 +1,31 @@ +# serializer version: 1 +# name: test_setup_migrates_to_groups + dict({ + 'data': dict({ + }), + 'disabled_by': None, + 'discovery_keys': dict({ + }), + 'domain': 'group', + 'minor_version': 1, + 'options': dict({ + 'entities': list([ + 'sensor.input_one', + 'sensor.input_two', + ]), + 'group_type': 'sensor', + 'hide_members': False, + 'ignore_non_numeric': False, + 'name': 'My min_max', + 'type': 'max', + }), + 'pref_disable_new_entities': False, + 'pref_disable_polling': False, + 'source': 'user', + 'subentries': list([ + ]), + 'title': 'My min_max', + 'unique_id': None, + 'version': 1, + }) +# --- diff --git a/tests/components/min_max/test_config_flow.py b/tests/components/min_max/test_config_flow.py index a9db7cab9041..76af3adcf2c2 100644 --- a/tests/components/min_max/test_config_flow.py +++ b/tests/components/min_max/test_config_flow.py @@ -1,124 +1,16 @@ """Test the Min/Max config flow.""" -from unittest.mock import patch - -import pytest - from homeassistant import config_entries from homeassistant.components.min_max.const import DOMAIN from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from tests.common import MockConfigEntry, get_schema_suggested_value - -@pytest.mark.parametrize("platform", ["sensor"]) -async def test_config_flow(hass: HomeAssistant, platform: str) -> None: - """Test the config flow.""" - input_sensors = ["sensor.input_one", "sensor.input_two"] +async def test_config_flow_aborts(hass: HomeAssistant) -> None: + """Test the config flow aborts.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - assert result["type"] is FlowResultType.FORM - assert result["errors"] is None - - with patch( - "homeassistant.components.min_max.async_setup_entry", - return_value=True, - ) as mock_setup_entry: - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - {"name": "My min_max", "entity_ids": input_sensors, "type": "max"}, - ) - await hass.async_block_till_done() - - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "My min_max" - assert result["data"] == {} - assert result["options"] == { - "entity_ids": input_sensors, - "name": "My min_max", - "round_digits": 2.0, - "type": "max", - } - assert len(mock_setup_entry.mock_calls) == 1 - - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - assert config_entry.data == {} - assert config_entry.options == { - "entity_ids": input_sensors, - "name": "My min_max", - "round_digits": 2.0, - "type": "max", - } - assert config_entry.title == "My min_max" - - -@pytest.mark.parametrize("platform", ["sensor"]) -async def test_options(hass: HomeAssistant, platform: str) -> None: - """Test reconfiguring.""" - hass.states.async_set("sensor.input_one", "10") - hass.states.async_set("sensor.input_two", "20") - hass.states.async_set("sensor.input_three", "33.33") - - input_sensors1 = ["sensor.input_one", "sensor.input_two"] - input_sensors2 = ["sensor.input_one", "sensor.input_two", "sensor.input_three"] - - # Setup the config entry - config_entry = MockConfigEntry( - data={}, - domain=DOMAIN, - options={ - "entity_ids": input_sensors1, - "name": "My min_max", - "round_digits": 0, - "type": "min", - }, - title="My min_max", - ) - config_entry.add_to_hass(hass) - assert await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done() - - result = await hass.config_entries.options.async_init(config_entry.entry_id) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "init" - schema = result["data_schema"].schema - assert get_schema_suggested_value(schema, "entity_ids") == input_sensors1 - assert get_schema_suggested_value(schema, "round_digits") == 0 - assert get_schema_suggested_value(schema, "type") == "min" - - result = await hass.config_entries.options.async_configure( - result["flow_id"], - user_input={ - "entity_ids": input_sensors2, - "round_digits": 1, - "type": "mean", - }, - ) - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["data"] == { - "entity_ids": input_sensors2, - "name": "My min_max", - "round_digits": 1, - "type": "mean", - } - assert config_entry.data == {} - assert config_entry.options == { - "entity_ids": input_sensors2, - "name": "My min_max", - "round_digits": 1, - "type": "mean", - } - assert config_entry.title == "My min_max" - - # Check config entry is reloaded with new options - await hass.async_block_till_done() - - # Check the entity was updated, no new entity was created - assert len(hass.states.async_all()) == 4 - - # Check the state of the entity has changed as expected - state = hass.states.get(f"{platform}.my_min_max") - assert state.state == "21.1" + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "migrated_to_groups" diff --git a/tests/components/min_max/test_init.py b/tests/components/min_max/test_init.py index 0e180c3135c7..afe30b98dad7 100644 --- a/tests/components/min_max/test_init.py +++ b/tests/components/min_max/test_init.py @@ -1,19 +1,21 @@ """Test the Min/Max integration.""" -import pytest +from freezegun.api import FrozenDateTimeFactory +from syrupy.assertion import SnapshotAssertion +from syrupy.filters import props from homeassistant.components.min_max.const import DOMAIN from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, async_fire_time_changed -@pytest.mark.parametrize("platform", ["sensor"]) -async def test_setup_and_remove_config_entry( +async def test_setup_migrates_to_groups( hass: HomeAssistant, entity_registry: er.EntityRegistry, - platform: str, + freezer: FrozenDateTimeFactory, + snapshot: SnapshotAssertion, ) -> None: """Test setting up and removing a config entry.""" hass.states.async_set("sensor.input_one", "10") @@ -21,7 +23,7 @@ async def test_setup_and_remove_config_entry( input_sensors = ["sensor.input_one", "sensor.input_two"] - min_max_entity_id = f"{platform}.my_min_max" + min_max_entity_id = "sensor.my_min_max" # Setup the config entry config_entry = MockConfigEntry( @@ -37,19 +39,28 @@ async def test_setup_and_remove_config_entry( ) config_entry.add_to_hass(hass) assert await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) # Check the entity is registered in the entity registry - assert entity_registry.async_get(min_max_entity_id) is not None + entity = entity_registry.async_get(min_max_entity_id) + assert entity is not None + assert entity.config_entry_id is not None + assert entity.config_entry_id != config_entry.entry_id + assert entity.platform == "group" # Check the platform is setup correctly state = hass.states.get(min_max_entity_id) assert state.state == "20.0" - # Remove the config entry - assert await hass.config_entries.async_remove(config_entry.entry_id) - await hass.async_block_till_done() + config_entry = hass.config_entries.async_entries("group")[0] + assert config_entry.as_dict() == snapshot( + exclude=props("created_at", "entry_id", "modified_at") + ) + config_entry_min_max = hass.config_entries.async_entries(DOMAIN)[0] + assert config_entry_min_max - # Check the state and entity registry entry are removed - assert hass.states.get(min_max_entity_id) is None - assert entity_registry.async_get(min_max_entity_id) is None + freezer.tick(60 * 5) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + config_entry_min_max = hass.config_entries.async_entries(DOMAIN) + assert not config_entry_min_max diff --git a/tests/components/min_max/test_sensor.py b/tests/components/min_max/test_sensor.py index 6615fa407530..55a9e9178735 100644 --- a/tests/components/min_max/test_sensor.py +++ b/tests/components/min_max/test_sensor.py @@ -23,7 +23,7 @@ from homeassistant.const import ( UnitOfTemperature, ) 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 tests.common import get_fixture_path @@ -42,6 +42,28 @@ RANGE_4_DIGITS = round(max(VALUES) - min(VALUES), 4) SUM_VALUE = sum(VALUES) +async def test_deprecation_warning( + hass: HomeAssistant, issue_registry: ir.IssueRegistry +) -> None: + """Test the min sensor with a default name.""" + config = { + "sensor": { + "platform": "min_max", + "type": "min", + "entity_ids": ["sensor.test_1", "sensor.test_2", "sensor.test_3"], + } + } + + with patch("homeassistant.util.ulid.ulid", return_value="1234"): + assert await async_setup_component(hass, "sensor", config) + await hass.async_block_till_done() + + issue = issue_registry.async_get_issue(DOMAIN, "1234") + assert issue is not None + assert issue.severity == ir.IssueSeverity.WARNING + assert issue.translation_key == "yaml_deprecated" + + async def test_default_name_sensor(hass: HomeAssistant) -> None: """Test the min sensor with a default name.""" config = {