Skip to content

Commit 9dd02f0

Browse files
committed
Fix persisting and restoring of role info
1 parent d882fcc commit 9dd02f0

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

custom_components/powersensor/PowersensorEntity.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def _handle_update(self, event, message):
9898

9999
name_updated = False
100100
self._has_recently_received_update_message = True
101+
101102
role = message.get('role', None)
102103
if role is not None and role != self.role:
103104
self.role = role

custom_components/powersensor/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
# mac =,
2727
# host =,
2828
# port =,
29-
# role =,
3029
# }
3130
# with_solar =,
31+
# roles = {
32+
# mac = role,
33+
# }
3234
# }
3335
#
3436

@@ -83,7 +85,7 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
8385
if entry.version == 1:
8486
# Move device info into subkey, add with_solar key
8587
devices = { **entry.data }
86-
new_data = { 'devices': devices, 'with_solar': False }
88+
new_data = { 'devices': devices, 'with_solar': False, 'roles': {} }
8789
hass.config_entries.async_update_entry(entry, data=new_data, version=2, minor_version=1)
8890

8991
_LOGGER.debug("Upgrading config to %s.%s", entry.version, entry.minor_version)

custom_components/powersensor/config_flow.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ async def async_step_discovery_confirm(
145145
data={
146146
'devices': self.hass.data[DOMAIN]["discovered_plugs"],
147147
'with_solar': False,
148+
'roles': {},
148149
}
149150
)
150151
return self.async_show_form(step_id="discovery_confirm")

custom_components/powersensor/sensor.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,17 @@ async def handle_role_update(mac_address: str, new_role: str):
5252
persist_entry = False
5353
new_data = copy.deepcopy({ **entry.data })
5454

55+
# We only persist actual roles. If a device forgets its role, we want
56+
# to keep what we've previously learned.
5557
if new_role is not None:
56-
devices = new_data['devices']
57-
if mac_address in devices.keys():
58-
info = devices[mac_address]
59-
have_role = True if 'role' in info.keys() else False
60-
old_role = info['role'] if have_role else None
61-
if (not have_role) or (have_role and info['role'] != new_role):
62-
_LOGGER.debug(f"Updating role for {mac_address} from {old_role} to {new_role}")
63-
info['role'] = new_role
64-
persist_entry = True
58+
if 'roles' not in new_data.keys():
59+
new_data['roles'] = {}
60+
roles = new_data['roles']
61+
old_role = roles.get(mac_address, None)
62+
if old_role is None or old_role != new_role:
63+
_LOGGER.debug(f"Updating role for {mac_address} from {old_role} to {new_role}")
64+
roles[mac_address] = new_role
65+
persist_entry = True
6566

6667
if new_role == 'solar':
6768
new_data['with_solar'] = True # Remember for next time we start
@@ -104,6 +105,11 @@ async def handle_discovered_sensor(sensor_mac: str, sensor_role: str):
104105
]
105106
async_add_entities(new_sensors, True)
106107
async_dispatcher_send(hass, f"{DOMAIN}_sensor_added_to_homeassistant", sensor_mac, sensor_role)
108+
if sensor_role is None:
109+
persisted_role = entry.data.get('roles', {}).get(sensor_mac, None)
110+
if persisted_role is not None:
111+
_LOGGER.info(f"Restored role {persisted_role} for {sensor_mac} due to sensor not reporting role")
112+
sensor_role = persisted_role
107113
# Trigger initial entity role update
108114
async_dispatcher_send(hass, f"{POWER_SENSOR_UPDATE_SIGNAL}_{sensor_mac}_role", 'role', { 'role': sensor_role })
109115

0 commit comments

Comments
 (0)