From c05210683e39b2d2e264caa832c9f8bd0aef4469 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Tue, 17 Mar 2026 16:37:21 +0000 Subject: [PATCH] Demo valve registry entry and device (#165803) Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- homeassistant/components/demo/valve.py | 20 +++++++++++++----- tests/components/demo/test_valve.py | 28 ++++++++++++++++++-------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/demo/valve.py b/homeassistant/components/demo/valve.py index eb415e8475c..4e90b10ada5 100644 --- a/homeassistant/components/demo/valve.py +++ b/homeassistant/components/demo/valve.py @@ -9,9 +9,12 @@ from typing import Any from homeassistant.components.valve import ValveEntity, ValveEntityFeature, ValveState from homeassistant.config_entries import ConfigEntry from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback +from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.event import async_track_utc_time_change +from . import DOMAIN + OPEN_CLOSE_DELAY = 2 # Used to give a realistic open/close experience in frontend @@ -23,10 +26,10 @@ async def async_setup_entry( """Set up the Demo config entry.""" async_add_entities( [ - DemoValve("Front Garden", ValveState.OPEN), - DemoValve("Orchard", ValveState.CLOSED), - DemoValve("Back Garden", ValveState.CLOSED, position=70), - DemoValve("Trees", ValveState.CLOSED, position=30), + DemoValve("valve_1", "Front Garden", ValveState.OPEN), + DemoValve("valve_2", "Orchard", ValveState.CLOSED), + DemoValve("valve_3", "Back Garden", ValveState.CLOSED, position=70), + DemoValve("valve_4", "Trees", ValveState.CLOSED, position=30), ] ) @@ -34,17 +37,24 @@ async def async_setup_entry( class DemoValve(ValveEntity): """Representation of a Demo valve.""" + _attr_has_entity_name = True + _attr_name = None _attr_should_poll = False def __init__( self, + unique_id: str, name: str, state: str, moveable: bool = True, position: int | None = None, ) -> None: """Initialize the valve.""" - self._attr_name = name + self._attr_unique_id = unique_id + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, unique_id)}, + name=name, + ) if moveable: self._attr_supported_features = ( ValveEntityFeature.OPEN | ValveEntityFeature.CLOSE diff --git a/tests/components/demo/test_valve.py b/tests/components/demo/test_valve.py index ea2aca5344e..de580cc31b0 100644 --- a/tests/components/demo/test_valve.py +++ b/tests/components/demo/test_valve.py @@ -1,5 +1,6 @@ """The tests for the Demo valve platform.""" +from collections.abc import Generator from datetime import timedelta from unittest.mock import patch @@ -17,10 +18,9 @@ from homeassistant.components.valve import ( ) from homeassistant.const import ATTR_ENTITY_ID, EVENT_STATE_CHANGED, Platform from homeassistant.core import HomeAssistant -from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util -from tests.common import async_capture_events, async_fire_time_changed +from tests.common import MockConfigEntry, async_capture_events, async_fire_time_changed FRONT_GARDEN = "valve.front_garden" ORCHARD = "valve.orchard" @@ -28,7 +28,7 @@ BACK_GARDEN = "valve.back_garden" @pytest.fixture -async def valve_only() -> None: +def valve_only() -> Generator[None]: """Enable only the valve platform.""" with patch( "homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM", @@ -38,11 +38,12 @@ async def valve_only() -> None: @pytest.fixture(autouse=True) -async def setup_comp(hass: HomeAssistant, valve_only: None): - """Set up demo component.""" - assert await async_setup_component( - hass, VALVE_DOMAIN, {VALVE_DOMAIN: {"platform": DOMAIN}} - ) +async def setup_comp(hass: HomeAssistant, valve_only: None) -> None: + """Set up demo component from config entry.""" + config_entry = MockConfigEntry(domain=DOMAIN) + config_entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() @@ -50,6 +51,7 @@ async def setup_comp(hass: HomeAssistant, valve_only: None): async def test_closing(hass: HomeAssistant) -> None: """Test the closing of a valve.""" state = hass.states.get(FRONT_GARDEN) + assert state is not None assert state.state == ValveState.OPEN await hass.async_block_till_done() @@ -63,9 +65,11 @@ async def test_closing(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert state_changes[0].data["entity_id"] == FRONT_GARDEN + assert state_changes[0].data["new_state"] is not None assert state_changes[0].data["new_state"].state == ValveState.CLOSING assert state_changes[1].data["entity_id"] == FRONT_GARDEN + assert state_changes[1].data["new_state"] is not None assert state_changes[1].data["new_state"].state == ValveState.CLOSED @@ -73,6 +77,7 @@ async def test_closing(hass: HomeAssistant) -> None: async def test_opening(hass: HomeAssistant) -> None: """Test the opening of a valve.""" state = hass.states.get(ORCHARD) + assert state is not None assert state.state == ValveState.CLOSED await hass.async_block_till_done() @@ -83,15 +88,18 @@ async def test_opening(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert state_changes[0].data["entity_id"] == ORCHARD + assert state_changes[0].data["new_state"] is not None assert state_changes[0].data["new_state"].state == ValveState.OPENING assert state_changes[1].data["entity_id"] == ORCHARD + assert state_changes[1].data["new_state"] is not None assert state_changes[1].data["new_state"].state == ValveState.OPEN async def test_set_valve_position(hass: HomeAssistant) -> None: """Test moving the valve to a specific position.""" state = hass.states.get(BACK_GARDEN) + assert state is not None assert state.attributes[ATTR_CURRENT_POSITION] == 70 # close to 10% @@ -102,6 +110,7 @@ async def test_set_valve_position(hass: HomeAssistant) -> None: blocking=True, ) state = hass.states.get(BACK_GARDEN) + assert state is not None assert state.state == ValveState.CLOSING for _ in range(6): @@ -110,6 +119,7 @@ async def test_set_valve_position(hass: HomeAssistant) -> None: await hass.async_block_till_done() state = hass.states.get(BACK_GARDEN) + assert state is not None assert state.attributes[ATTR_CURRENT_POSITION] == 10 assert state.state == ValveState.OPEN @@ -121,6 +131,7 @@ async def test_set_valve_position(hass: HomeAssistant) -> None: blocking=True, ) state = hass.states.get(BACK_GARDEN) + assert state is not None assert state.state == ValveState.OPENING for _ in range(7): @@ -129,6 +140,7 @@ async def test_set_valve_position(hass: HomeAssistant) -> None: await hass.async_block_till_done() state = hass.states.get(BACK_GARDEN) + assert state is not None assert state.attributes[ATTR_CURRENT_POSITION] == 80 assert state.state == ValveState.OPEN