From 72a8040d5f663af00e9ecbb05431ffb02a79f101 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Sun, 9 May 2021 21:18:25 +0200 Subject: [PATCH] Add workaround for energy sensors with wrong number of phases (#89) --- hatasmota/sensor.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/hatasmota/sensor.py b/hatasmota/sensor.py index 9af36c1..2a4dc1e 100644 --- a/hatasmota/sensor.py +++ b/hatasmota/sensor.py @@ -257,11 +257,24 @@ async def subscribe_topics(self): def state_message_received(msg): """Handle new MQTT state messages.""" if msg.topic == self._cfg.state_topic1: - state = get_value_by_path(msg.payload, self._cfg.value_path) + state = get_value_by_path(msg.payload, self._cfg.value_path[:-1]) + last_node = self._cfg.value_path[-1] if msg.topic == self._cfg.state_topic2: value_path = ["StatusSNS"] + self._cfg.value_path - state = get_value_by_path(msg.payload, value_path) + state = get_value_by_path(msg.payload, value_path[:-1]) + last_node = value_path[-1] if state is not None: + # Indexed sensors may be announced with more indices than present in + # the status. Handle this gracefully wihtout throwing. This is a + # workaround for energy sensors which are announced with multiple phases + # but where the actual sensor sends updates with fewer phases. + try: + if hasattr(state, "__getitem__"): + state = state[last_node] + elif last_node != 0: + state = None + except (IndexError, KeyError): + state = None self._on_state_callback(state) availability_topics = self.get_availability_topics()