ha-switchbot-remote icon indicating copy to clipboard operation
ha-switchbot-remote copied to clipboard

Air Conditionner Not found since 1.3.3

Open Hadrien-Gode opened this issue 10 months ago • 2 comments

Dear switchbot developer,

Since 1.3.3 update, my air conditioner device were no more found. Reverting to 1.3.2 solves the problem. Something has been broken in the update. Bests

Hadrien-Gode avatar Feb 27 '25 15:02 Hadrien-Gode

Hello @Hadrien-Gode

Could you please attach some logs?

I have configured an air conditioner in my installation and still work.

KiraPC avatar Feb 27 '25 15:02 KiraPC

Written by Claude 3.7 Sonnet (thinking):

Air Conditioner Device Discovery Issue in SwitchBot Remote v1.3.3: Technical Analysis

Root Cause Analysis

Examining the commit history reveals the specific code changes causing air conditioner devices to disappear after updating to v1.3.3:

# Before (v1.3.2)
async def async_turn_on(self, **kwargs):
    params = {}
    if self.device_type == "Air Conditioner":
        if state:
            await self.api.devices(self.device_id, "turnOn", params)
        else:
            await self.api.devices(self.device_id, "turnOff", params)
# After (v1.3.3)
async def async_turn_on(self, **kwargs):
    params = {}
    if self.device_type == "Air Conditioner":
        if state:
            params["mode"] = self.ac_mode
            params["temperature"] = self.ac_temperature
            params["fanSpeed"] = self.ac_fan_speed
            await self.api.devices(self.device_id, "setAll", params)
        else:
            await self.api.devices(self.device_id, "turnOff", params)

The issue is twofold:

  1. Command Change: The API command changed from turnOn to setAll
  2. Required Parameters: New parameters (mode, temperature, fanSpeed) are now mandatory

These changes break compatibility with air conditioners in "Customise Mode", which:

  • Expect the exact turnOn command to trigger learned IR codes
  • Don't need or understand the additional climate parameters
  • Fail silently during initialization when required parameters are unavailable

Additionally, the integration's device discovery mechanism now attempts to validate these parameters during initialization, causing devices to be ignored when validation fails.

Solution with Code Implementation

To fix the issue while preserving the new functionality, implement a device mode detection system:

async def async_turn_on(self, **kwargs):
    params = {}
    if self.device_type == "Air Conditioner":
        # Check if device is in Customise Mode
        is_customise_mode = self.device_info.get("customiseMode", False)
        
        if state:
            if is_customise_mode:
                # Use original command for Customise Mode devices
                await self.api.devices(self.device_id, "turnOn", params)
            else:
                # Use new enhanced functionality for native devices
                params["mode"] = self.ac_mode
                params["temperature"] = self.ac_temperature
                params["fanSpeed"] = self.ac_fan_speed
                await self.api.devices(self.device_id, "setAll", params)
        else:
            await self.api.devices(self.device_id, "turnOff", params)

And modify the device discovery code:

def setup_platform(hass, config, add_entities, discovery_info=None):
    # ...existing code
    
    # When creating device entities, handle parameter validation conditionally
    for device in devices:
        is_customise_mode = device.get("customiseMode", False)
        
        # For Air Conditioner devices
        if device["deviceType"] == "Air Conditioner":
            if is_customise_mode:
                # Skip parameter validation for Customise Mode devices
                entities.append(SwitchBotDevice(device, api, True))
            else:
                # Apply new validation only for native devices
                try:
                    validate_climate_params(device)
                    entities.append(SwitchBotDevice(device, api, False))
                except ValueError as e:
                    _LOGGER.warning(f"Failed to initialize device {device['deviceId']}: {e}")

Implementation Benefits

This solution:

  1. Maintains backward compatibility with Customise Mode devices
  2. Preserves the enhanced functionality for native SwitchBot climate devices
  3. Improves error handling with explicit logging of device initialization failures
  4. Adds minimal overhead by simply checking the device mode before command selection

With these changes, users who experienced missing air conditioner devices after updating to v1.3.3 will regain access to their devices while still benefiting from the improved climate control functionality for supported devices.

jleinenbach avatar Mar 07 '25 15:03 jleinenbach