diff --git a/homeassistant/components/generic_thermostat/climate.py b/homeassistant/components/generic_thermostat/climate.py index 76e8285f1ed5..2ec4c42cc9e2 100644 --- a/homeassistant/components/generic_thermostat/climate.py +++ b/homeassistant/components/generic_thermostat/climate.py @@ -51,6 +51,7 @@ from homeassistant.core import ( ) from homeassistant.helpers import config_validation as cv from homeassistant.helpers.device import async_entity_id_to_device +from homeassistant.helpers.device_registry import DeviceEntry from homeassistant.helpers.entity import CONTEXT_RECENT_TIME_SECONDS from homeassistant.helpers.entity_platform import ( AddConfigEntryEntitiesCallback, @@ -143,6 +144,7 @@ async def async_setup_entry( PLATFORM_SCHEMA_COMMON(dict(config_entry.options)), config_entry.entry_id, async_add_entities, + device=async_entity_id_to_device(hass, config_entry.options[CONF_HEATER]), ) @@ -165,6 +167,7 @@ async def _async_setup_config( config: Mapping[str, Any], unique_id: str | None, async_add_entities: AddEntitiesCallback | AddConfigEntryEntitiesCallback, + device: DeviceEntry | None = None, ) -> None: """Set up the generic thermostat platform.""" @@ -192,7 +195,6 @@ async def _async_setup_config( async_add_entities( [ GenericThermostat( - hass, name=name, heater_entity_id=heater_entity_id, sensor_entity_id=sensor_entity_id, @@ -212,6 +214,7 @@ async def _async_setup_config( target_temperature_step=target_temperature_step, unit=unit, unique_id=unique_id, + device=device, ) ] ) @@ -224,7 +227,6 @@ class GenericThermostat(ClimateEntity, RestoreEntity): def __init__( self, - hass: HomeAssistant, *, name: str, heater_entity_id: str, @@ -245,15 +247,13 @@ class GenericThermostat(ClimateEntity, RestoreEntity): target_temperature_step: float | None, unit: UnitOfTemperature, unique_id: str | None, + device: DeviceEntry | None = None, ) -> None: """Initialize the thermostat.""" self._attr_name = name self.heater_entity_id = heater_entity_id self.sensor_entity_id = sensor_entity_id - self.device_entry = async_entity_id_to_device( - hass, - heater_entity_id, - ) + self.device_entry = device self.ac_mode = ac_mode self.min_cycle_duration = min_cycle_duration or timedelta() self.max_cycle_duration = max_cycle_duration diff --git a/tests/components/generic_thermostat/test_climate.py b/tests/components/generic_thermostat/test_climate.py index 2ec5681f1c7e..8b5072931bca 100644 --- a/tests/components/generic_thermostat/test_climate.py +++ b/tests/components/generic_thermostat/test_climate.py @@ -1873,6 +1873,50 @@ async def test_device_id( assert helper_entity.device_id == source_entity.device_id +async def test_device_id_yaml( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + device_registry: dr.DeviceRegistry, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test no device is set for a YAML-configured thermostat.""" + source_config_entry = MockConfigEntry() + source_config_entry.add_to_hass(hass) + source_device_entry = device_registry.async_get_or_create( + config_entry_id=source_config_entry.entry_id, + identifiers={("switch", "identifier_test")}, + connections={("mac", "30:31:32:33:34:35")}, + ) + entity_registry.async_get_or_create( + "switch", + "test", + "source", + config_entry=source_config_entry, + device_id=source_device_entry.id, + ) + await hass.async_block_till_done() + + assert await async_setup_component( + hass, + CLIMATE_DOMAIN, + { + "climate": { + "platform": "generic_thermostat", + "name": "test", + "heater": "switch.test_source", + "target_sensor": ENT_SENSOR, + "unique_id": "generic_thermostat_yaml", + } + }, + ) + await hass.async_block_till_done() + + helper_entity = entity_registry.async_get("climate.test") + assert helper_entity is not None + assert helper_entity.device_id is None + assert "attempts to attach a device to an entity" not in caplog.text + + @pytest.mark.usefixtures("setup_comp_1") async def test_hvac_mode_change_user_context( hass: HomeAssistant, hass_admin_user: MockUser