Skip to content

Commit e378cca

Browse files
committed
Fix persisting with_solar, and use it on startup.
1 parent f0b616a commit e378cca

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

custom_components/powersensor/__init__.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,26 @@
1010

1111
from .PowersensorDiscoveryService import PowersensorDiscoveryService
1212
from .PowersensorMessageDispatcher import PowersensorMessageDispatcher
13+
from .config_flow import PowersensorConfigFlow
1314
from .const import DOMAIN
1415
_LOGGER = logging.getLogger(__name__)
1516

1617
PLATFORMS: 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

1934
async 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+

custom_components/powersensor/config_flow.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _extract_device_name(discovery_info) -> str:
5050
class PowersensorConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
5151
"""Handle a config flow."""
5252

53-
VERSION = 1
53+
VERSION = 2
5454

5555
def __init__(self):
5656
"""Initialize the config flow."""
@@ -142,6 +142,9 @@ async def async_step_discovery_confirm(
142142
_LOGGER.debug(self.hass.data[DOMAIN]["discovered_plugs"])
143143
return self.async_create_entry(
144144
title="Powersensor",
145-
data=self.hass.data[DOMAIN]["discovered_plugs"]
145+
data={
146+
'devices': self.hass.data[DOMAIN]["discovered_plugs"],
147+
'with_solar': False,
148+
}
146149
)
147-
return self.async_show_form(step_id="discovery_confirm")
150+
return self.async_show_form(step_id="discovery_confirm")

custom_components/powersensor/sensor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ async def handle_discovered_plug(plug_mac_address: str, host: str, port: int, na
5656

5757
async def handle_discovered_sensor(sensor_mac: str, sensor_role: str):
5858
if sensor_role == 'solar':
59-
entry.runtime_data["with_solar"] = True # Remember for next time we start
59+
new_data = { **entry.data }
60+
new_data['with_solar'] = True # Remember for next time we start
61+
hass.config_entries.async_update_entry(entry, data=new_data)
6062

6163
new_sensors = [
6264
PowersensorSensorEntity(hass, sensor_mac, SensorMeasurements.Battery),

0 commit comments

Comments
 (0)