From 2cc05d300d6515b48e8b04f6de14d224b535b7ad Mon Sep 17 00:00:00 2001 From: Simon Hu Date: Thu, 22 Aug 2024 13:50:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=8F=E5=B0=91=E4=B8=8D=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E7=9A=84=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6=EF=BC=8C=E7=8E=B0?= =?UTF-8?q?=E5=9C=A8=E5=8F=AA=E9=9C=80=E8=A6=81=E4=BC=A0=E6=84=9F=E5=99=A8?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6=EF=BC=8C?= =?UTF-8?q?climate=E4=B8=80=E4=B8=AA=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom_components/airtub_udp/sensor.py | 27 ++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/custom_components/airtub_udp/sensor.py b/custom_components/airtub_udp/sensor.py index 10df194..674b2d8 100755 --- a/custom_components/airtub_udp/sensor.py +++ b/custom_components/airtub_udp/sensor.py @@ -38,6 +38,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities): async_add_entities(entities, update_before_add=True) + @callback + def handle_data_changed_event(event): + """Handle the custom event and notify all entities.""" + _LOGGER.debug("AIRTUB: Notifying entities about data change event.") + for entity in entities: + entity.handle_event(event) + + hass.bus.async_listen(EVENT_NEW_DATA, handle_data_changed_event) + class UDPMulticastSensor(SensorEntity): """Representation of a UDP Multicast sensor.""" @@ -57,6 +66,10 @@ def __init__( self._name = f"boiler_{device}_{key}" self._state = self._convert_to_number(initial_value) self._entity_id = entity_id + self._setup_attributes(key) + + def _setup_attributes(self, key): + """Setup sensor attributes based on key.""" if key in ["cct", "cdt", "tct", "tdt", "odt", "tdf"]: self._attr_unit_of_measurement = UnitOfTemperature.CELSIUS self._attr_icon = "mdi:thermometer" @@ -81,7 +94,6 @@ def __init__( self._attr_device_class = None self._attr_state_class = "measurement" self._attr_precision = 0 # 默认精度为0 - hass.bus.async_listen(EVENT_NEW_DATA, self._handle_data_changed_event) @property def name(self): @@ -133,9 +145,9 @@ def _convert_to_number(value): _LOGGER.debug(f"AIRTUB: Conversion failed for value: {value}, returning 0") return 0 - @callback - def _handle_data_changed_event(self, event): - """Handle the custom event and update state""" + def handle_event(self, event): + """Handle the custom event and update state.""" + _LOGGER.debug(f"AIRTUB: {self._name} received event data.") self.async_schedule_update_ha_state(True) async def async_update(self): @@ -170,7 +182,6 @@ def __init__( self._state = self._convert_to_boolean(initial_value) self._entity_id = entity_id self._attr_icon = "mdi:toggle-switch-variant" - hass.bus.async_listen(EVENT_NEW_DATA, self._handle_data_changed_event) @property def name(self): @@ -202,9 +213,9 @@ def _convert_to_boolean(value): _LOGGER.debug(f"AIRTUB: Attempting to convert value to boolean: {value}") return value == 1 - @callback - def _handle_data_changed_event(self, event): - """Handle the custom event and update state""" + def handle_event(self, event): + """Handle the custom event and update state.""" + _LOGGER.debug(f"AIRTUB: {self._name} received event data.") self.async_schedule_update_ha_state(True) async def async_update(self):