1010
1111from .PowersensorDiscoveryService import PowersensorDiscoveryService
1212from .PowersensorMessageDispatcher import PowersensorMessageDispatcher
13+ from .config_flow import PowersensorConfigFlow
1314from .const import DOMAIN
1415_LOGGER = logging .getLogger (__name__ )
1516
1617PLATFORMS : list [Platform ] = [Platform .SENSOR ]
1718
19+ #
20+ # config entry.data structure (version 2.1):
21+ # {
22+ # devices = {
23+ # mac = {
24+ # name =,
25+ # display_name =,
26+ # mac =,
27+ # host =,
28+ # port =,
29+ # }
30+ # with_solar =,
31+ # }
32+ #
1833
1934async def async_setup_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
2035 """Set up integration from a config entry."""
@@ -30,14 +45,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3045 await zeroconf_service .start ()
3146
3247 # Establish our virtual household
33- vhh = VirtualHousehold (False )
48+ vhh = VirtualHousehold (entry . data [ 'with_solar' ] )
3449
35-
36- # TODO: can we move the dispatcher into the entry.runtime_data dict?
50+ # Set up message dispatcher
3751 dispatcher = PowersensorMessageDispatcher (hass , vhh )
38- for mac , network_info in entry .data .items ():
52+ for mac , network_info in entry .data [ 'devices' ] .items ():
3953 await dispatcher .enqueue_plug_for_adding (network_info )
4054
55+
4156 entry .runtime_data = { "vhh" : vhh , "dispatcher" : dispatcher , "zeroconf" : zeroconf_service }
4257 await hass .config_entries .async_forward_entry_setups (entry , PLATFORMS )
4358 return True
@@ -52,5 +67,24 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
5267 if "zeroconf" in entry .runtime_data .keys ():
5368 await entry .runtime_data ["zeroconf" ].stop ()
5469
70+ hass .data [DOMAIN ].pop (entry .entry_id )
71+
5572 return unload_ok
5673
74+
75+ async def async_migrate_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
76+ """Migrate old config entry."""
77+ _LOGGER .debug ("Upgrading config from %s.%s" , entry .version , entry .minor_version )
78+ if entry .version > PowersensorConfigFlow .VERSION :
79+ # Downgrade from future version
80+ return False
81+
82+ if entry .version == 1 :
83+ # Move device info into subkey, add with_solar key
84+ devices = { ** entry .data }
85+ new_data = { 'devices' : devices , 'with_solar' : False }
86+ hass .config_entries .async_update_entry (entry , data = new_data , version = 2 , minor_version = 1 )
87+
88+ _LOGGER .debug ("Upgrading config to %s.%s" , entry .version , entry .minor_version )
89+ return True
90+
0 commit comments