Przeglądaj źródła

Implements new async_track_state_change_event

Vassilis Panos 1 rok temu
rodzic
commit
878df57d82

+ 1 - 1
custom_components/smartir/__init__.py

@@ -19,7 +19,7 @@ from homeassistant.helpers.typing import ConfigType
 _LOGGER = logging.getLogger(__name__)
 
 DOMAIN = 'smartir'
-VERSION = '1.18.0'
+VERSION = '1.18.1'
 MANIFEST_URL = (
     "https://raw.githubusercontent.com/"
     "smartHomeHub/SmartIR/{}/"

+ 25 - 15
custom_components/smartir/climate.py

@@ -12,8 +12,8 @@ from homeassistant.components.climate.const import (
 from homeassistant.const import (
     CONF_NAME, STATE_ON, STATE_OFF, STATE_UNKNOWN, STATE_UNAVAILABLE, ATTR_TEMPERATURE,
     PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE)
-from homeassistant.core import callback
-from homeassistant.helpers.event import async_track_state_change
+from homeassistant.core import Event, EventStateChangedData, callback
+from homeassistant.helpers.event import async_track_state_change, async_track_state_change_event
 import homeassistant.helpers.config_validation as cv
 from homeassistant.helpers.restore_state import RestoreEntity
 from . import COMPONENT_ABS_DIR, Helper
@@ -173,24 +173,24 @@ class SmartIRClimate(ClimateEntity, RestoreEntity):
                 self._last_on_operation = last_state.attributes['last_on_operation']
 
         if self._temperature_sensor:
-            async_track_state_change(self.hass, self._temperature_sensor, 
-                                     self._async_temp_sensor_changed)
+            async_track_state_change_event(self.hass, self._temperature_sensor, 
+                                           self._async_temp_sensor_changed)
 
             temp_sensor_state = self.hass.states.get(self._temperature_sensor)
             if temp_sensor_state and temp_sensor_state.state != STATE_UNKNOWN:
                 self._async_update_temp(temp_sensor_state)
 
         if self._humidity_sensor:
-            async_track_state_change(self.hass, self._humidity_sensor, 
-                                     self._async_humidity_sensor_changed)
+            async_track_state_change_event(self.hass, self._humidity_sensor, 
+                                           self._async_humidity_sensor_changed)
 
             humidity_sensor_state = self.hass.states.get(self._humidity_sensor)
             if humidity_sensor_state and humidity_sensor_state.state != STATE_UNKNOWN:
                 self._async_update_humidity(humidity_sensor_state)
 
         if self._power_sensor:
-            async_track_state_change(self.hass, self._power_sensor, 
-                                     self._async_power_sensor_changed)
+            async_track_state_change_event(self.hass, self._power_sensor, 
+                                           self._async_power_sensor_changed)
 
     @property
     def unique_id(self):
@@ -385,25 +385,35 @@ class SmartIRClimate(ClimateEntity, RestoreEntity):
 
             except Exception as e:
                 _LOGGER.exception(e)
-            
-    async def _async_temp_sensor_changed(self, entity_id, old_state, new_state):
+                
+    @callback
+    async def _async_temp_sensor_changed(self, event: Event[EventStateChangedData]) -> None:
         """Handle temperature sensor changes."""
+        new_state = event.data["new_state"]        
+
         if new_state is None:
             return
 
         self._async_update_temp(new_state)
         self.async_write_ha_state()
 
-    async def _async_humidity_sensor_changed(self, entity_id, old_state, new_state):
+    @callback
+    async def _async_humidity_sensor_changed(self, event: Event[EventStateChangedData]) -> None:
         """Handle humidity sensor changes."""
+        new_state = event.data["new_state"]
+
         if new_state is None:
             return
 
         self._async_update_humidity(new_state)
         self.async_write_ha_state()
-
-    async def _async_power_sensor_changed(self, entity_id, old_state, new_state):
-        """Handle power sensor changes."""
+        
+    @callback
+    async def _async_power_sensor_changed(self, event: Event[EventStateChangedData]) -> None:
+        entity_id = event.data["entity_id"]
+        old_state = event.data["old_state"]
+        new_state = event.data["new_state"]
+    
         if new_state is None:
             return
 
@@ -441,4 +451,4 @@ class SmartIRClimate(ClimateEntity, RestoreEntity):
             if state.state != STATE_UNKNOWN and state.state != STATE_UNAVAILABLE:
                 self._current_humidity = float(state.state)
         except ValueError as ex:
-            _LOGGER.error("Unable to update from humidity sensor: %s", ex)
+            _LOGGER.error("Unable to update from humidity sensor: %s", ex)

+ 10 - 5
custom_components/smartir/fan.py

@@ -11,8 +11,8 @@ from homeassistant.components.fan import (
     PLATFORM_SCHEMA, DIRECTION_REVERSE, DIRECTION_FORWARD)
 from homeassistant.const import (
     CONF_NAME, STATE_OFF, STATE_ON, STATE_UNKNOWN)
-from homeassistant.core import callback
-from homeassistant.helpers.event import async_track_state_change
+from homeassistant.core import Event, EventStateChangedData, callback
+from homeassistant.helpers.event import async_track_state_change, async_track_state_change_event
 import homeassistant.helpers.config_validation as cv
 from homeassistant.helpers.restore_state import RestoreEntity
 from homeassistant.util.percentage import (
@@ -155,8 +155,8 @@ class SmartIRFan(FanEntity, RestoreEntity):
                 self._last_on_speed = last_state.attributes['last_on_speed']
 
             if self._power_sensor:
-                async_track_state_change(self.hass, self._power_sensor, 
-                                         self._async_power_sensor_changed)
+                async_track_state_change_event(self.hass, self._power_sensor, 
+                                               self._async_power_sensor_changed)
 
     @property
     def unique_id(self):
@@ -282,8 +282,13 @@ class SmartIRFan(FanEntity, RestoreEntity):
             except Exception as e:
                 _LOGGER.exception(e)
 
-    async def _async_power_sensor_changed(self, entity_id, old_state, new_state):
+    @callback
+    async def _async_power_sensor_changed(self, event: Event[EventStateChangedData]) -> None:
         """Handle power sensor changes."""
+        entity_id = event.data["entity_id"]
+        old_state = event.data["old_state"]
+        new_state = event.data["new_state"]
+
         if new_state is None:
             return
 

+ 4 - 4
custom_components/smartir/manifest.json

@@ -5,11 +5,11 @@
   "dependencies": [],
   "codeowners": ["@smartHomeHub"],
   "requirements": ["aiofiles>=0.6.0"],
-  "homeassistant": "2024.10.0",
-  "version": "1.18.0",
+  "homeassistant": "2025.5.0",
+  "version": "1.18.1",
   "updater": {
-    "version": "1.18.0",
-    "releaseNotes": "-- Adds support for light devices",
+    "version": "1.18.1",
+    "releaseNotes": "-- Implements new async_track_state_change_event",
     "files": [
       "__init__.py",
       "climate.py",

+ 1 - 1
hacs.json

@@ -1,5 +1,5 @@
 {
   "name": "SmartIR",
-  "homeassistant": "2025.1.0",
+  "homeassistant": "2025.5.0",
   "persistent_directory": "codes"
 }