albertogeniola / MerossIot

Async Python library for controlling Meross devices
https://albertogeniola.github.io/MerossIot/
MIT License
489 stars 89 forks source link

Operation of multiple MSS110 smart plugs #368

Closed LinerSeven closed 7 months ago

LinerSeven commented 8 months ago

Hi, @albertogeniola,

I'm using your meross-iot library to control the meross smartplug mss110.

That works fine for one unit, though, If there are multiple mss110s connected to the same WIFI SSID, the dev.async_turn_on and dev.async_turn_off will cause multiple smart plugs to switch at the same time.

Could this be made to work individually?

Best Regards, Liner Seven,

albertogeniola commented 7 months ago

Hi @LinerSeven ,

Can you post the code you are using to turn devices on and off? I'm under the impression there is something wrong in your code that is triggering multiple devices...

LinerSeven commented 7 months ago

Hi, @albertogeniola,

Thank you for your response, My Python Source is follows:

import asyncio import os

from meross_iot.http_api import MerossHttpClient from meross_iot.manager import MerossManager

EMAIL = os.environ.get('MEROSS_EMAIL') or "YOUR_MEROSS_CLOUD_EMAIL" PASSWORD = os.environ.get('MEROSS_PASSWORD') or "YOUR_MEROSS_CLOUD_PASSWORD"

async def main():

Setup the HTTP client API from user-password

# When choosing the API_BASE_URL env var, choose from one of the above based on your location.
# Asia-Pacific: "iotx-ap.meross.com"
# Europe: "iotx-eu.meross.com"
# US: "iotx-us.meross.com"
http_api_client = await MerossHttpClient.async_from_user_password(api_base_url='iotx-ap.meross.com',
                                                                  email=EMAIL, 
                                                                  password=PASSWORD)

# Setup and start the device manager
manager = MerossManager(http_client=http_api_client)
await manager.async_init()

# Retrieve all the MSS310 devices that are registered on this account
await manager.async_device_discovery()
plugs = manager.find_devices(device_type="mss110")

if len(plugs) < 1:
    print("No MSS110 plugs found...")
else:
    # Turn it on channel 0
    # Note that channel argument is optional for MSS310 as they only have one channel
    dev = plugs[0]

    # The first time we play with a device, we must update its status
    await dev.async_update()

    # We can now start playing with that
    await dev.async_toggle(channel=0)

# Close the manager and logout from http_api
manager.close()
await http_api_client.async_logout()

if name == 'main':

Windows and python 3.8 requires to set up a specific event_loop_policy.

#  On Linux and MacOSX this is not necessary.
if os.name == 'nt':
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.stop()

Best Regards, Liner Seven,

LinerSeven commented 7 months ago

@albertogeniola , Now it is working by getting and specifying the uuid and narrowing down the devices as follows

FYI,

#!/usr/sbin/python

import asyncio
import os

from meross_iot.http_api import MerossHttpClient
from meross_iot.manager import MerossManager

EMAIL = os.environ.get('MEROSS_EMAIL') or "YOUR_MEROSS_CLOUD_EMAIL"
PASSWORD = os.environ.get('MEROSS_PASSWORD') or "YOUR_MEROSS_CLOUD_PASSWORD"

async def main():
    # Setup the HTTP client API from user-password
    # When choosing the API_BASE_URL env var, choose from one of the above based on your location.
    # Asia-Pacific: "iotx-ap.meross.com"
    # Europe: "iotx-eu.meross.com"
    # US: "iotx-us.meross.com"
    http_api_client = await MerossHttpClient.async_from_user_password(api_base_url='https://iotx-ap.meross.com',
                                                                      email=EMAIL, 
                                                                      password=PASSWORD)

    # Setup and start the device manager
    manager = MerossManager(http_client=http_api_client)
    await manager.async_init()

    # Retrieve all the MSS310 devices that are registered on this account
    await manager.async_device_discovery()
    plugs = manager.find_devices(device_uuids="***********************************")
    # plugs = manager.find_devices(device_type="mss110")

    if len(plugs) < 1:
        print("No MSS110 plugs found...")
    else:
        # Turn it on channel 0
        # Note that channel argument is optional for MSS310 as they only have one channel
        dev = plugs[0]

        # The first time we play with a device, we must update its status
        await dev.async_update()

        # We can now start playing with that
        # print(f"Turing off {dev.uuid}")
        print(f"Turing off {dev.name}")
        await dev.async_turn_off(channel=0)

    # Close the manager and logout from http_api
    manager.close()
    await http_api_client.async_logout()

if __name__ == '__main__':
    # Windows and python 3.8 requires to set up a specific event_loop_policy.
    #  On Linux and MacOSX this is not necessary.
    if os.name == 'nt':
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    loop.stop()
albertogeniola commented 7 months ago

Glad you solved it !