Skip to content

Commit 3ae9ea3

Browse files
authored
Do not add generic_thermostat config entry to source device (home-assistant#148728)
1 parent e35f7b1 commit 3ae9ea3

File tree

4 files changed

+292
-64
lines changed

4 files changed

+292
-64
lines changed

homeassistant/components/generic_thermostat/__init__.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""The generic_thermostat component."""
22

3+
import logging
4+
35
from homeassistant.config_entries import ConfigEntry
46
from homeassistant.core import Event, HomeAssistant
57
from homeassistant.helpers import entity_registry as er
@@ -8,14 +10,20 @@
810
async_remove_stale_devices_links_keep_entity_device,
911
)
1012
from homeassistant.helpers.event import async_track_entity_registry_updated_event
11-
from homeassistant.helpers.helper_integration import async_handle_source_entity_changes
13+
from homeassistant.helpers.helper_integration import (
14+
async_handle_source_entity_changes,
15+
async_remove_helper_config_entry_from_source_device,
16+
)
1217

1318
from .const import CONF_HEATER, CONF_SENSOR, PLATFORMS
1419

20+
_LOGGER = logging.getLogger(__name__)
21+
1522

1623
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
1724
"""Set up from a config entry."""
1825

26+
# This can be removed in HA Core 2026.2
1927
async_remove_stale_devices_links_keep_entity_device(
2028
hass,
2129
entry.entry_id,
@@ -28,23 +36,19 @@ def set_humidifier_entity_id_or_uuid(source_entity_id: str) -> None:
2836
options={**entry.options, CONF_HEATER: source_entity_id},
2937
)
3038

31-
async def source_entity_removed() -> None:
32-
# The source entity has been removed, we need to clean the device links.
33-
async_remove_stale_devices_links_keep_entity_device(hass, entry.entry_id, None)
34-
3539
entry.async_on_unload(
3640
# We use async_handle_source_entity_changes to track changes to the heater, but
3741
# not the temperature sensor because the generic_hygrostat adds itself to the
3842
# heater's device.
3943
async_handle_source_entity_changes(
4044
hass,
45+
add_helper_config_entry_to_device=False,
4146
helper_config_entry_id=entry.entry_id,
4247
set_source_entity_id_or_uuid=set_humidifier_entity_id_or_uuid,
4348
source_device_id=async_entity_id_to_device_id(
4449
hass, entry.options[CONF_HEATER]
4550
),
4651
source_entity_id_or_uuid=entry.options[CONF_HEATER],
47-
source_entity_removed=source_entity_removed,
4852
)
4953
)
5054

@@ -75,6 +79,40 @@ async def async_sensor_updated(
7579
return True
7680

7781

82+
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
83+
"""Migrate old entry."""
84+
_LOGGER.debug(
85+
"Migrating from version %s.%s", config_entry.version, config_entry.minor_version
86+
)
87+
88+
if config_entry.version > 1:
89+
# This means the user has downgraded from a future version
90+
return False
91+
if config_entry.version == 1:
92+
options = {**config_entry.options}
93+
if config_entry.minor_version < 2:
94+
# Remove the generic_thermostat config entry from the source device
95+
if source_device_id := async_entity_id_to_device_id(
96+
hass, options[CONF_HEATER]
97+
):
98+
async_remove_helper_config_entry_from_source_device(
99+
hass,
100+
helper_config_entry_id=config_entry.entry_id,
101+
source_device_id=source_device_id,
102+
)
103+
hass.config_entries.async_update_entry(
104+
config_entry, options=options, minor_version=2
105+
)
106+
107+
_LOGGER.debug(
108+
"Migration to version %s.%s successful",
109+
config_entry.version,
110+
config_entry.minor_version,
111+
)
112+
113+
return True
114+
115+
78116
async def config_entry_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
79117
"""Update listener, called when the config entry options are changed."""
80118
await hass.config_entries.async_reload(entry.entry_id)

homeassistant/components/generic_thermostat/climate.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
)
4949
from homeassistant.exceptions import ConditionError
5050
from homeassistant.helpers import condition, config_validation as cv
51-
from homeassistant.helpers.device import async_device_info_to_link_from_entity
51+
from homeassistant.helpers.device import async_entity_id_to_device
5252
from homeassistant.helpers.entity_platform import (
5353
AddConfigEntryEntitiesCallback,
5454
AddEntitiesCallback,
@@ -182,23 +182,23 @@ async def _async_setup_config(
182182
[
183183
GenericThermostat(
184184
hass,
185-
name,
186-
heater_entity_id,
187-
sensor_entity_id,
188-
min_temp,
189-
max_temp,
190-
target_temp,
191-
ac_mode,
192-
min_cycle_duration,
193-
cold_tolerance,
194-
hot_tolerance,
195-
keep_alive,
196-
initial_hvac_mode,
197-
presets,
198-
precision,
199-
target_temperature_step,
200-
unit,
201-
unique_id,
185+
name=name,
186+
heater_entity_id=heater_entity_id,
187+
sensor_entity_id=sensor_entity_id,
188+
min_temp=min_temp,
189+
max_temp=max_temp,
190+
target_temp=target_temp,
191+
ac_mode=ac_mode,
192+
min_cycle_duration=min_cycle_duration,
193+
cold_tolerance=cold_tolerance,
194+
hot_tolerance=hot_tolerance,
195+
keep_alive=keep_alive,
196+
initial_hvac_mode=initial_hvac_mode,
197+
presets=presets,
198+
precision=precision,
199+
target_temperature_step=target_temperature_step,
200+
unit=unit,
201+
unique_id=unique_id,
202202
)
203203
]
204204
)
@@ -212,6 +212,7 @@ class GenericThermostat(ClimateEntity, RestoreEntity):
212212
def __init__(
213213
self,
214214
hass: HomeAssistant,
215+
*,
215216
name: str,
216217
heater_entity_id: str,
217218
sensor_entity_id: str,
@@ -234,7 +235,7 @@ def __init__(
234235
self._attr_name = name
235236
self.heater_entity_id = heater_entity_id
236237
self.sensor_entity_id = sensor_entity_id
237-
self._attr_device_info = async_device_info_to_link_from_entity(
238+
self.device_entry = async_entity_id_to_device(
238239
hass,
239240
heater_entity_id,
240241
)

homeassistant/components/generic_thermostat/config_flow.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
class ConfigFlowHandler(SchemaConfigFlowHandler, domain=DOMAIN):
101101
"""Handle a config or options flow."""
102102

103+
MINOR_VERSION = 2
104+
103105
config_flow = CONFIG_FLOW
104106
options_flow = OPTIONS_FLOW
105107

0 commit comments

Comments
 (0)