13
13
from homeassistant .core import HomeAssistant
14
14
from homeassistant .helpers .typing import ConfigType
15
15
from homeassistant .config_entries import ConfigEntry
16
- from .const import CONF_LANGUAGE , CONF_MODE , CONF_SCAN_INTERVAL_API , DOMAIN , PLATFORMS
16
+ from .const import (
17
+ CONF_LANGUAGE ,
18
+ CONF_MODE ,
19
+ CONF_SCAN_INTERVAL_API ,
20
+ DOMAIN ,
21
+ PLATFORMS ,
22
+ _LOGGER ,
23
+ DEFAULT_CONF_SCAN_INTERVAL_API_VALUE ,
24
+ DEFAULT_CONF_SCAN_INTERVAL_VALUE ,
25
+ )
17
26
from .coordinator import WemPortalDataUpdateCoordinator
18
27
from .wemportalapi import WemPortalApi
19
-
20
-
21
- # CONFIG_SCHEMA = vol.Schema(
22
- # {
23
- # DOMAIN: vol.Schema(
24
- # {
25
- # vol.Optional(
26
- # CONF_SCAN_INTERVAL, default=timedelta(minutes=30)
27
- # ): config_validation.time_period,
28
- # vol.Optional(
29
- # CONF_SCAN_INTERVAL_API, default=timedelta(minutes=5)
30
- # ): config_validation.time_period,
31
- # vol.Optional(CONF_LANGUAGE, default="en"): config_validation.string,
32
- # vol.Optional(CONF_MODE, default="api"): config_validation.string,
33
- # vol.Required(CONF_USERNAME): config_validation.string,
34
- # vol.Required(CONF_PASSWORD): config_validation.string,
35
- # }
36
- # )
37
- # },
38
- # extra=vol.ALLOW_EXTRA,
39
- # )
28
+ import homeassistant .helpers .entity_registry as entity_registry
40
29
41
30
42
31
def get_wemportal_unique_id (config_entry_id : str , device_id : str , name : str ):
@@ -50,82 +39,94 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
50
39
return True
51
40
52
41
53
- # # Set proper update_interval, based on selected mode
54
- # if config[DOMAIN].get(CONF_MODE) == "web":
55
- # update_interval = config[DOMAIN].get(CONF_SCAN_INTERVAL)
56
-
57
- # elif config[DOMAIN].get(CONF_MODE) == "api":
58
- # update_interval = config[DOMAIN].get(CONF_SCAN_INTERVAL_API)
59
- # else:
60
- # update_interval = min(
61
- # config[DOMAIN].get(CONF_SCAN_INTERVAL),
62
- # config[DOMAIN].get(CONF_SCAN_INTERVAL_API),
63
- # )
64
- # # Creatie API object
65
- # api = WemPortalApi(config[DOMAIN])
66
- # # Create custom coordinator
67
- # coordinator = WemPortalDataUpdateCoordinator(hass, api, update_interval)
68
-
69
- # hass.data[DOMAIN] = {
70
- # "api": api,
71
- # "coordinator": coordinator,
72
- # }
73
-
74
- # await coordinator.async_config_entry_first_refresh()
75
-
76
- # # Initialize platforms
77
- # for platform in PLATFORMS:
78
- # hass.helpers.discovery.load_platform(platform, DOMAIN, {}, config)
79
- # return True
42
+ # Migrate values from previous versions
43
+ async def migrate_unique_ids (
44
+ hass : HomeAssistant , config_entry : ConfigEntry , coordinator
45
+ ):
46
+ er = await entity_registry .async_get_registry (hass )
47
+ # Do migration for first device if we have multiple
48
+ device_id = list (coordinator .data .keys ())[0 ]
49
+ data = coordinator .data [device_id ]
50
+
51
+ change = False
52
+ for unique_id , values in data .items ():
53
+ name_id = er .async_get_entity_id (values ["platform" ], DOMAIN , unique_id )
54
+ new_id = get_wemportal_unique_id (config_entry .entry_id , device_id , unique_id )
55
+ if name_id is not None :
56
+ _LOGGER .info (
57
+ f"Found entity with old id ({ name_id } ). Updating to new unique_id ({ new_id } )."
58
+ )
59
+ _LOGGER .error (unique_id )
60
+ # check if there already is a new one
61
+ new_entity_id = er .async_get_entity_id (values ["platform" ], DOMAIN , new_id )
62
+ if new_entity_id is not None :
63
+ _LOGGER .info (
64
+ "Found entity with old id and an entity with a new unique_id. Preserving old entity..."
65
+ )
66
+ er .async_remove (new_entity_id )
67
+ er .async_update_entity (
68
+ name_id ,
69
+ new_unique_id = new_id ,
70
+ )
71
+ change = True
72
+ if change :
73
+ await coordinator .async_config_entry_first_refresh ()
80
74
81
75
82
76
async def async_setup_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
83
77
"""Set up the wemportal component."""
84
78
# Set proper update_interval, based on selected mode
85
- if entry .data .get (CONF_MODE ) == "web" :
86
- update_interval = entry .data .get (CONF_SCAN_INTERVAL )
79
+ if entry .options .get (CONF_MODE ) == "web" :
80
+ update_interval = entry .options .get (
81
+ CONF_SCAN_INTERVAL , DEFAULT_CONF_SCAN_INTERVAL_VALUE
82
+ )
87
83
88
- elif entry .data .get (CONF_MODE ) == "api" :
89
- update_interval = entry .data .get (CONF_SCAN_INTERVAL_API )
84
+ elif entry .options .get (CONF_MODE ) == "api" :
85
+ update_interval = entry .options .get (
86
+ CONF_SCAN_INTERVAL_API , DEFAULT_CONF_SCAN_INTERVAL_API_VALUE
87
+ )
90
88
else :
91
89
update_interval = min (
92
- entry .data .get (CONF_SCAN_INTERVAL ),
93
- entry .data .get (CONF_SCAN_INTERVAL_API ),
90
+ entry .options .get (CONF_SCAN_INTERVAL , DEFAULT_CONF_SCAN_INTERVAL_VALUE ),
91
+ entry .options .get (
92
+ CONF_SCAN_INTERVAL_API , DEFAULT_CONF_SCAN_INTERVAL_API_VALUE
93
+ ),
94
94
)
95
95
# Creatie API object
96
- api = WemPortalApi (entry .data )
96
+
97
+ api = WemPortalApi (
98
+ entry .data .get (CONF_USERNAME ), entry .data .get (CONF_PASSWORD ), entry .options
99
+ )
97
100
# Create custom coordinator
98
101
coordinator = WemPortalDataUpdateCoordinator (
99
102
hass , api , timedelta (seconds = update_interval )
100
103
)
101
104
102
105
await coordinator .async_config_entry_first_refresh ()
103
106
107
+ try :
108
+ version = entry .version
109
+ if version < 2 :
110
+ await migrate_unique_ids (hass , entry , coordinator )
111
+ except Exception :
112
+ await migrate_unique_ids (hass , entry , coordinator )
113
+
104
114
hass .data [DOMAIN ][entry .entry_id ] = {
105
115
"api" : api ,
106
116
# "config": entry.data,
107
117
"coordinator" : coordinator ,
108
118
}
109
119
110
- # TODO: Implement removal of outdated entries
111
- # current_devices: set[tuple[str, str]] = set({(DOMAIN, entry.entry_id)})
112
-
113
- # device_registry = dr.async_get(hass)
114
- # for device_entry in dr.async_entries_for_config_entry(
115
- # device_registry, entry.entry_id
116
- # ):
117
- # for identifier in device_entry.identifiers:
118
- # if identifier in current_devices:
119
- # break
120
- # else:
121
- # device_registry.async_remove_device(device_entry.id)
122
-
123
120
await hass .config_entries .async_forward_entry_setups (entry , PLATFORMS )
124
121
entry .async_on_unload (entry .add_update_listener (_async_entry_updated ))
125
122
126
123
return True
127
124
128
125
126
+ async def async_migrate_entry (hass : HomeAssistant , config_entry : ConfigEntry ) -> bool :
127
+ return True
128
+
129
+
129
130
async def _async_entry_updated (hass : HomeAssistant , config_entry : ConfigEntry ) -> None :
130
131
"""Handle entry updates."""
131
132
await hass .config_entries .async_reload (config_entry .entry_id )
0 commit comments