{"id":9110,"library":"midea-local","title":"Midea Local Control","description":"midea-local is a Python library designed to control Midea M-Smart appliances directly via the local area network, bypassing the cloud. It provides an interface to discover, connect to, and manage various Midea devices such as air conditioners, dehumidifiers, and water heaters. The library is actively maintained, with frequent minor releases (typically monthly or bi-monthly) to add new features and support for more device models. The current version is 6.6.0, requiring Python >=3.11.","status":"active","version":"6.6.0","language":"en","source_language":"en","source_url":"https://github.com/midea-lan/midea-local","tags":["smart home","IoT","Midea","local control","appliance"],"install":[{"cmd":"pip install midea-local","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"Appliance","correct":"from midea_local.appliance import Appliance"},{"note":"MSmartHomeAppliance was the primary cloud client class before v6.0.0; it was replaced by MSmartHome.","wrong":"from midea_local.cloud import MSmartHomeAppliance","symbol":"MSmartHome","correct":"from midea_local.cloud import MSmartHome"},{"symbol":"MideaError","correct":"from midea_local.exceptions import MideaError"}],"quickstart":{"code":"import asyncio\nimport os\nfrom midea_local.appliance import Appliance\nfrom midea_local.exceptions import MideaError\n\nasync def control_midea_appliance():\n    # Replace with your appliance's actual IP, ID, token, and key\n    # These credentials are often difficult to obtain and may require special tools.\n    device_ip = os.environ.get('MIDEA_DEVICE_IP', '192.168.1.100')\n    device_id = int(os.environ.get('MIDEA_DEVICE_ID', '12345678901'))\n    token = os.environ.get('MIDEA_DEVICE_TOKEN', 'your_32_byte_token_hex_string') # e.g., 'AABBCCDD...' (64 chars)\n    key = os.environ.get('MIDEA_DEVICE_KEY', 'your_16_byte_key_hex_string')   # e.g., '11223344...' (32 chars)\n\n    if not all([device_ip, device_id, token, key]):\n        print(\"Please set MIDEA_DEVICE_IP, MIDEA_DEVICE_ID, MIDEA_DEVICE_TOKEN, and MIDEA_DEVICE_KEY environment variables.\")\n        return\n\n    try:\n        # Find and create the appliance instance\n        # protocol_version can be 1 or 3, 3 is default and for newer devices.\n        appliance = await Appliance.find_and_create_appliance(\n            device_ip, device_id, token, key, protocol_version=3\n        )\n\n        if appliance:\n            print(f\"Connected to appliance: {appliance.name} ({appliance.ip_address})\")\n            print(f\"Current power state: {appliance.power_state}\")\n\n            # Example: Turn on the appliance if it's off\n            if not appliance.power_state:\n                print(\"Turning appliance ON...\")\n                await appliance.set_attribute(\"power_state\", True)\n                await appliance.refresh()\n                print(f\"New power state: {appliance.power_state}\")\n\n            # Example: Set target temperature (for AC units)\n            if hasattr(appliance, 'target_temperature'):\n                current_temp = appliance.target_temperature\n                new_temp = current_temp + 1 if current_temp < 30 else 20 # Cycle temperature\n                print(f\"Setting target temperature from {current_temp}°C to {new_temp}°C...\")\n                await appliance.set_attribute(\"target_temperature\", new_temp)\n                await appliance.refresh()\n                print(f\"New target temperature: {appliance.target_temperature}°C\")\n\n            # Example: Print all available attributes and their values\n            print(\"\\n--- Appliance Attributes ---\")\n            for attr in dir(appliance):\n                if not attr.startswith('_') and not callable(getattr(appliance, attr)):\n                    try:\n                        print(f\"{attr}: {getattr(appliance, attr)}\")\n                    except Exception:\n                        pass # Some attributes might not be directly readable\n\n        else:\n            print(\"Failed to find or connect to the appliance.\")\n\n    except MideaError as e:\n        print(f\"MideaError: {e}\")\n    except Exception as e:\n        print(f\"An unexpected error occurred: {e}\")\n\nif __name__ == \"__main__\":\n    asyncio.run(control_midea_appliance())\n","lang":"python","description":"This quickstart demonstrates how to connect to a Midea appliance using its IP address, device ID, token, and key, then read its power state and toggle it, and set the target temperature for AC units. It also prints all available attributes. Remember to replace the placeholder credentials with your actual device details, which often require prior setup using other tools (like midea-discover) to extract from the official Midea app."},"warnings":[{"fix":"Update your import statement and class instantiation from `from midea_local.cloud import MSmartHomeAppliance` to `from midea_local.cloud import MSmartHome`.","message":"The primary class for Midea cloud interactions (`midea_local.cloud`) was refactored in v6.0.0. If you were directly using `midea_local.cloud.MSmartHomeAppliance` (pre-6.0.0) for cloud operations (e.g., discovery or obtaining credentials), you must now use `midea_local.cloud.MSmartHome`.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Consult the `midea-local` GitHub repository's README or community forums for methods to acquire your device's `token` and `key` before attempting to use the library.","message":"Obtaining the `token` (32-byte hex string) and `key` (16-byte hex string) for Midea devices is the most common hurdle for local control. These credentials are device-specific and are not easily found in the official Midea app. Users often resort to third-party tools (e.g., midea-discover), network traffic sniffing, or older versions of the Midea app to extract them.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If a feature is not working or an attribute is missing, check the device type/model specific discussions in the `midea-local` GitHub issues. You might need to experiment with `protocol_version` (e.g., `protocol_version=1` for older devices) or update your library version for newer device support.","message":"Midea appliances exhibit significant variations in their supported features, firmware, and communication protocols. Not all devices or all features of a device may be fully supported by the library, or they might require a specific `protocol_version` when instantiating the `Appliance` object.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Replace `MSmartHomeAppliance` with `MSmartHome` in your import statement and code: `from midea_local.cloud import MSmartHome`.","cause":"Using the deprecated class name `MSmartHomeAppliance` for cloud interactions after `midea-local` v6.0.0.","error":"ImportError: cannot import name 'MSmartHomeAppliance' from 'midea_local.cloud'"},{"fix":"Verify the appliance's IP address (it might have changed via DHCP) and its exact Device ID. Ensure the device is powered on, connected to the local network, and no firewall on your network or host machine is blocking UDP traffic on port 6445 (the default Midea protocol port).","cause":"The library could not establish a connection or verify the appliance using the provided IP address and Device ID. This often points to network issues or incorrect identification.","error":"midea_local.exceptions.MideaError: Error creating appliance: Appliance not found or not responding. Check IP/ID."},{"fix":"You need to re-obtain the correct and current `token` and `key` for your specific appliance. Refer to the `midea-local` documentation or community tools (e.g., `midea-discover`) for methods to extract these credentials.","cause":"The `token` or `key` provided for your Midea appliance is incorrect, malformed, or has expired. These credentials are vital for authenticating local control commands.","error":"midea_local.exceptions.MideaError: Invalid token or key for appliance."},{"fix":"Check the `midea-local` documentation or GitHub issues for support regarding your specific appliance model. Update the library to the latest version to ensure you have the most recent device support. Not all Midea devices support every feature exposed by other models.","cause":"Attempting to access a feature or attribute that is not supported by the specific model of your Midea appliance or its current firmware, or the library version does not yet support it.","error":"AttributeError: 'Appliance' object has no attribute 'some_feature_name'"}]}