Skip to content

Commit ea1e320

Browse files
committed
Centralise role update message sending
1 parent 8907050 commit ea1e320

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

custom_components/powersensor/PowersensorEntity.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,8 @@ def _handle_update(self, event, message):
130130

131131
# event is not presently used, but is passed to maintain flexibility for future development
132132

133-
name_updated = False
134133
self._has_recently_received_update_message = True
135134

136-
role = message.get('role', None)
137-
if role is not None and role != self._role:
138-
async_dispatcher_send(self._hass, ROLE_UPDATE_SIGNAL, self._mac, role)
139-
140135
if self._message_key in message.keys():
141136
if self._message_callback:
142137
self._attr_native_value = self._message_callback( message[self._message_key])

custom_components/powersensor/PowersensorMessageDispatcher.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
CREATE_SENSOR_SIGNAL,
1515
DATA_UPDATE_SIGNAL_FMT_MAC_EVENT,
1616
PLUG_ADDED_TO_HA_SIGNAL,
17+
ROLE_UPDATE_SIGNAL,
1718
SENSOR_ADDED_TO_HA_SIGNAL,
1819
SOLAR_ADDED_TO_VHH_SIGNAL,
1920
SOLAR_SENSOR_DETECTED_SIGNAL,
@@ -175,9 +176,13 @@ def cancel_any_pending_removal(self, mac, source):
175176

176177
async def handle_message(self, event: str, message: dict):
177178
mac = message['mac']
178-
role = message.get('role', None)
179-
if role is None:
180-
role = self._entry.data.get('roles', {}).get(mac, None)
179+
persisted_role = self._entry.data.get('roles', {}).get(mac, None)
180+
role = message.get('role', persisted_role)
181+
message['role'] = role
182+
183+
if role != persisted_role:
184+
async_dispatcher_send(self._hass, ROLE_UPDATE_SIGNAL, self._mac, role)
185+
181186
self.cancel_any_pending_removal(mac, "new message received from plug")
182187

183188
if mac not in self.plugs.keys():
@@ -199,12 +204,14 @@ async def handle_message(self, event: str, message: dict):
199204
self._last_request_to_notify_about_solar = new_time
200205
_LOGGER.debug("Notifying integration that solar is present.")
201206
async_dispatcher_send(self._hass, SOLAR_SENSOR_DETECTED_SIGNAL)
207+
202208
async_dispatcher_send(self._hass,
203209
DATA_UPDATE_SIGNAL_FMT_MAC_EVENT % (mac, event), event, message)
204-
if role is not None:
205-
async_dispatcher_send(
206-
self._hass, DATA_UPDATE_SIGNAL_FMT_MAC_EVENT % (mac, role),
207-
'role', { 'role': role })
210+
211+
# Synthesise a role type message for the role diagnostic entity
212+
async_dispatcher_send(
213+
self._hass, DATA_UPDATE_SIGNAL_FMT_MAC_EVENT % (mac, role),
214+
'role', { 'role': role })
208215

209216
async def disconnect(self):
210217
for _ in range(len(self.plugs)):

0 commit comments

Comments
 (0)