Kaynağa Gözat

feat: defer initialisation if the device is not responding

Require the device to have returned state before continuing with
initialisation.

This will allow `hidden: unavailable` to work correctly, since the
data to determine it will be available immediately when the entity
is created.

- add filtering of swing_mode and swing_horizontal_mode availability
  depending on values being available for the dps. This allows configs
  such as goldair_portable_airconditioner that support many variants
  based on flags to define the dps, but condition them on availability
  of the feature according to the feature flags. This also requires
  the initialisation to hold until the device is communicating.

Issue #3496
Jason Rumney 7 ay önce
ebeveyn
işleme
befd353380

+ 6 - 1
custom_components/tuya_local/__init__.py

@@ -641,10 +641,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
     )
     config = {**entry.data, **entry.options, "name": entry.title}
     try:
-        await hass.async_add_executor_job(setup_device, hass, config)
+        device = await hass.async_add_executor_job(setup_device, hass, config)
+        await device.async_refresh()
+
     except Exception as e:
         raise ConfigEntryNotReady("tuya-local device not ready") from e
 
+    if not device.has_returned_state:
+        raise ConfigEntryNotReady("tuya-local device offline")
+
     device_conf = await hass.async_add_executor_job(
         get_config,
         entry.data[CONF_TYPE],

+ 6 - 2
custom_components/tuya_local/climate.py

@@ -112,9 +112,13 @@ class TuyaLocalClimate(TuyaLocalEntity, ClimateEntity):
         if self._preset_mode_dps:
             self._attr_supported_features |= ClimateEntityFeature.PRESET_MODE
         if self._swing_mode_dps:
-            self._attr_supported_features |= ClimateEntityFeature.SWING_MODE
+            if self._swing_mode_dps.values(device):
+                self._attr_supported_features |= ClimateEntityFeature.SWING_MODE
         if self._swing_horizontal_mode_dps:
-            self._attr_supported_features |= ClimateEntityFeature.SWING_HORIZONTAL_MODE
+            if self._swing_horizontal_mode_dps.values(device):
+                self._attr_supported_features |= (
+                    ClimateEntityFeature.SWING_HORIZONTAL_MODE
+                )
         if self._temp_high_dps and self._temp_low_dps:
             self._attr_supported_features |= (
                 ClimateEntityFeature.TARGET_TEMPERATURE_RANGE

+ 1 - 1
custom_components/tuya_local/helpers/device_config.py

@@ -379,7 +379,7 @@ class TuyaEntityConfig:
                     self._device.config_type,
                     self.name,
                 )
-            hidden = device.has_returned_state and not self.available(device)
+            hidden = not self.available(device)
         return not hidden and not self.deprecated