barban-dev / homeassistant-midea-dehumidifier

Home Assistant Custom Integration for EVA II PRO WiFi Smart Dehumidifier appliance by Midea/Inventor.
GNU General Public License v3.0
69 stars 27 forks source link

MideaClient does not support multiple device due "static" content #11

Open gianlucasullazzo opened 3 years ago

gianlucasullazzo commented 3 years ago

MideaClient cannot support multiple device (like 2 dehumi) because hass.data[MIDEA_API_CLIENT] is the same for all devices. When hass queries first device status, all other ones will assume that specific status. I "solved" in my custom_component by creating new client inside MideaDehumidifierDevice constructor (instead of receiving it) but this is not a good solution. Are you planning to develop multi device integration?

HVPereira commented 3 years ago

Could you detail how you did it? I'm interested in your solution.

gianlucasullazzo commented 3 years ago

Hi, in init.py, I pushed into hass.data new client for each appliace:

    if not deviceId:
        if appliances is not None:
            for a in appliances:
                if a["type"] == "0xA1":
                    deviceId = str(a["id"])
                    targetDevice = a
                    client_for_device = MideaClient(username, password, sha256password, cacheTimeInSeconds = 0)
                    res = await hass.async_add_executor_job(client_for_device.login)
                    if res != -1:
                        sessionId = client_for_device.current["sessionId"]
                        _LOGGER.info("midea-dehumi: login success, sessionId=%s", sessionId)
                        hass.data[MIDEA_API_CLIENT + "_" + a["id"]] = client_for_device
                    _LOGGER.info("midea-dehumidifier: loading humidifier entity sub-component...")
                    load_platform(hass, 'humidifier', DOMAIN, {MIDEA_TARGET_DEVICE: targetDevice}, config)
                    _LOGGER.info("midea-dehumidifier: loading sensor entity sub-component...")
                    load_platform(hass, 'sensor', DOMAIN, {MIDEA_TARGET_DEVICE: targetDevice}, config)
                    _LOGGER.info("midea_dehumidifier: platform successfuly initialized.")
    else:
        if appliances is not None:
            for a in appliances:
                if a["type"] == "0xA1" and deviceId == str(a["id"]):
                    targetDevice = a

the key is: hass.data[MIDEA_API_CLIENT + "_" + a["id"]] = client_for_device

In humidifier.py, I changed the way to get client:

self._client = hass.data[MIDEA_API_CLIENT + "_" + targetDevice['id']]

Hope it helps. If you need further clarification, please ask me.

Regards, Gianluca

HVPereira commented 3 years ago

Ok I'll try it soon and revert back to you thanks a lot.... I think this solution it's way better than the one i was proposing..... Maybe barban push this change to his git

flos76 commented 3 years ago

Hi, thanks for the work. Unfortunately I can't get your solution to work for me. I have inserted your code - I hope - in the right location in init.py (line 95, in the code-block following #The first appliance having type="0xA1" is returned...) which seems to work fine. But as soon as I change humidifier.py my home assistant starts throwing 2 error messages about unretrievable task errors...

Is it correct to simply paste your code in the block following `class MideaDehumidifierDevice(HumidifierEntity): """Representation of a Midea/Inventor dehumidifier device."""

def __init__(self, hass, client, targetDevice):`

like this

    #Device modes
    self._modes_dict = DEHUMI_MODES_DICT
    self._available_modes = DEHUMI_MODES_LIST

    #self._client = client
    self._client = hass.data[MIDEA_API_CLIENT + "_" + targetDevice['id']
    self._device = targetDevice
    self._name = "midea_dehumidifier_"+targetDevice['id']
    self._unique_id = 'midea_dehumidifier_' + targetDevice['id']

Unfortunately I don't really understand python-code... am I missing something? Regards, Florian

gianlucasullazzo commented 3 years ago

Hi Florian attached both files. init.py.txt humidifier.py.txt

Regards, Gianluca

HVPereira commented 3 years ago

It's working :) I have done all the changes and it is working quite well. Congratulations! And thanks a lot you should share your code with the community

https://community.home-assistant.io/t/midea-dehumi-custom-component-midea-inventor-invmate-eva-ii-pro-wifi-dehumidifier/89638/89

I've added part of your code but you should take the merit for these findings 👍

HVPereira commented 3 years ago

Thanks a lot! :) by the way

gianlucasullazzo commented 3 years ago

Please, feel free to share my code. I'm new on github and I don't know its dynamics..so please share :)

flos76 commented 3 years ago

Thank a lot Gianluca! Your code works like a charm! :-)

Bimboboy007 commented 2 years ago

Hello, today the error occurred again that the integration Midea Dehumidifier Lan did not load after a new type of server. Then I immediately took a screenshot.

Bildschirmfoto 2022-01-03 um 23 11 45