Skip to content

Commit e35f7b1

Browse files
authored
Do not add generic_hygrostat config entry to source device (home-assistant#148727)
1 parent 1a1e9e9 commit e35f7b1

File tree

4 files changed

+289
-64
lines changed

4 files changed

+289
-64
lines changed

homeassistant/components/generic_hygrostat/__init__.py

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

3+
import logging
4+
35
import voluptuous as vol
46

57
from homeassistant.components.humidifier import HumidifierDeviceClass
@@ -16,7 +18,10 @@
1618
async_remove_stale_devices_links_keep_entity_device,
1719
)
1820
from homeassistant.helpers.event import async_track_entity_registry_updated_event
19-
from homeassistant.helpers.helper_integration import async_handle_source_entity_changes
21+
from homeassistant.helpers.helper_integration import (
22+
async_handle_source_entity_changes,
23+
async_remove_helper_config_entry_from_source_device,
24+
)
2025
from homeassistant.helpers.typing import ConfigType
2126

2227
DOMAIN = "generic_hygrostat"
@@ -70,6 +75,8 @@
7075
extra=vol.ALLOW_EXTRA,
7176
)
7277

78+
_LOGGER = logging.getLogger(__name__)
79+
7380

7481
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
7582
"""Set up the Generic Hygrostat component."""
@@ -89,6 +96,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
8996
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
9097
"""Set up from a config entry."""
9198

99+
# This can be removed in HA Core 2026.2
92100
async_remove_stale_devices_links_keep_entity_device(
93101
hass,
94102
entry.entry_id,
@@ -101,23 +109,19 @@ def set_humidifier_entity_id_or_uuid(source_entity_id: str) -> None:
101109
options={**entry.options, CONF_HUMIDIFIER: source_entity_id},
102110
)
103111

104-
async def source_entity_removed() -> None:
105-
# The source entity has been removed, we need to clean the device links.
106-
async_remove_stale_devices_links_keep_entity_device(hass, entry.entry_id, None)
107-
108112
entry.async_on_unload(
109113
# We use async_handle_source_entity_changes to track changes to the humidifer,
110114
# but not the humidity sensor because the generic_hygrostat adds itself to the
111115
# humidifier's device.
112116
async_handle_source_entity_changes(
113117
hass,
118+
add_helper_config_entry_to_device=False,
114119
helper_config_entry_id=entry.entry_id,
115120
set_source_entity_id_or_uuid=set_humidifier_entity_id_or_uuid,
116121
source_device_id=async_entity_id_to_device_id(
117122
hass, entry.options[CONF_HUMIDIFIER]
118123
),
119124
source_entity_id_or_uuid=entry.options[CONF_HUMIDIFIER],
120-
source_entity_removed=source_entity_removed,
121125
)
122126
)
123127

@@ -148,6 +152,40 @@ async def async_sensor_updated(
148152
return True
149153

150154

155+
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
156+
"""Migrate old entry."""
157+
_LOGGER.debug(
158+
"Migrating from version %s.%s", config_entry.version, config_entry.minor_version
159+
)
160+
161+
if config_entry.version > 1:
162+
# This means the user has downgraded from a future version
163+
return False
164+
if config_entry.version == 1:
165+
options = {**config_entry.options}
166+
if config_entry.minor_version < 2:
167+
# Remove the generic_hygrostat config entry from the source device
168+
if source_device_id := async_entity_id_to_device_id(
169+
hass, options[CONF_HUMIDIFIER]
170+
):
171+
async_remove_helper_config_entry_from_source_device(
172+
hass,
173+
helper_config_entry_id=config_entry.entry_id,
174+
source_device_id=source_device_id,
175+
)
176+
hass.config_entries.async_update_entry(
177+
config_entry, options=options, minor_version=2
178+
)
179+
180+
_LOGGER.debug(
181+
"Migration to version %s.%s successful",
182+
config_entry.version,
183+
config_entry.minor_version,
184+
)
185+
186+
return True
187+
188+
151189
async def config_entry_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
152190
"""Update listener, called when the config entry options are changed."""
153191
await hass.config_entries.async_reload(entry.entry_id)

homeassistant/components/generic_hygrostat/config_flow.py

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

95+
MINOR_VERSION = 2
96+
9597
config_flow = CONFIG_FLOW
9698
options_flow = OPTIONS_FLOW
9799

homeassistant/components/generic_hygrostat/humidifier.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
callback,
4343
)
4444
from homeassistant.helpers import condition, config_validation as cv
45-
from homeassistant.helpers.device import async_device_info_to_link_from_entity
45+
from homeassistant.helpers.device import async_entity_id_to_device
4646
from homeassistant.helpers.entity_platform import (
4747
AddConfigEntryEntitiesCallback,
4848
AddEntitiesCallback,
@@ -145,22 +145,22 @@ async def _async_setup_config(
145145
[
146146
GenericHygrostat(
147147
hass,
148-
name,
149-
switch_entity_id,
150-
sensor_entity_id,
151-
min_humidity,
152-
max_humidity,
153-
target_humidity,
154-
device_class,
155-
min_cycle_duration,
156-
dry_tolerance,
157-
wet_tolerance,
158-
keep_alive,
159-
initial_state,
160-
away_humidity,
161-
away_fixed,
162-
sensor_stale_duration,
163-
unique_id,
148+
name=name,
149+
switch_entity_id=switch_entity_id,
150+
sensor_entity_id=sensor_entity_id,
151+
min_humidity=min_humidity,
152+
max_humidity=max_humidity,
153+
target_humidity=target_humidity,
154+
device_class=device_class,
155+
min_cycle_duration=min_cycle_duration,
156+
dry_tolerance=dry_tolerance,
157+
wet_tolerance=wet_tolerance,
158+
keep_alive=keep_alive,
159+
initial_state=initial_state,
160+
away_humidity=away_humidity,
161+
away_fixed=away_fixed,
162+
sensor_stale_duration=sensor_stale_duration,
163+
unique_id=unique_id,
164164
)
165165
]
166166
)
@@ -174,6 +174,7 @@ class GenericHygrostat(HumidifierEntity, RestoreEntity):
174174
def __init__(
175175
self,
176176
hass: HomeAssistant,
177+
*,
177178
name: str,
178179
switch_entity_id: str,
179180
sensor_entity_id: str,
@@ -195,7 +196,7 @@ def __init__(
195196
self._name = name
196197
self._switch_entity_id = switch_entity_id
197198
self._sensor_entity_id = sensor_entity_id
198-
self._attr_device_info = async_device_info_to_link_from_entity(
199+
self.device_entry = async_entity_id_to_device(
199200
hass,
200201
switch_entity_id,
201202
)

0 commit comments

Comments
 (0)