Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. from meross_iot.manager import MerossManager
  2. from meross_iot.meross_event import MerossEventType
  3. from meross_iot.cloud.devices.light_bulbs import GenericBulb
  4. from meross_iot.cloud.devices.power_plugs import GenericPlug
  5. from meross_iot.cloud.devices.door_openers import GenericGarageDoorOpener
  6. import time
  7. import os
  8.  
  9.  
  10. EMAIL = os.environ.get('MEROSS_EMAIL') or "YOUR_MEROSS_CLOUD_EMAIL"
  11. PASSWORD = os.environ.get('MEROSS_PASSWORD') or "YOUR_MEROSS_CLOUD_PASSWORD"
  12.  
  13.  
  14. def event_handler(eventobj):
  15. if eventobj.event_type == MerossEventType.DEVICE_ONLINE_STATUS:
  16. print("Device online status changed: %s went %s" % (eventobj.device.name, eventobj.status))
  17. pass
  18. elif eventobj.event_type == MerossEventType.DEVICE_SWITCH_STATUS:
  19. print("Switch state changed: Device %s (channel %d) went %s" % (eventobj.device.name, eventobj.channel_id,
  20. eventobj.switch_state))
  21. else:
  22. print("Unknown event!")
  23.  
  24.  
  25. if __name__=='__main__':
  26. # Initiates the Meross Cloud Manager. This is in charge of handling the communication with the remote endpoint
  27. manager = MerossManager(meross_email=EMAIL, meross_password=PASSWORD)
  28.  
  29. # Register event handlers for the manager...
  30. manager.register_event_handler(event_handler)
  31.  
  32. # Starts the manager
  33. manager.start()
  34.  
  35. # You can retrieve the device you are looking for in various ways:
  36. # By kind
  37. bulbs = manager.get_devices_by_kind(GenericBulb)
  38. plugs = manager.get_devices_by_kind(GenericPlug)
  39. door_openers = manager.get_devices_by_kind(GenericGarageDoorOpener)
  40. all_devices = manager.get_supported_devices()
  41.  
  42. # Print some basic specs about the discovered devices
  43. print("All the bulbs I found:")
  44. for b in bulbs:
  45. print(b)
  46.  
  47. print("All the plugs I found:")
  48. for p in plugs:
  49. print(p)
  50.  
  51. print("All the garage openers I found:")
  52. for g in door_openers:
  53. print(g)
  54.  
  55. print("All the supported devices I found:")
  56. for d in all_devices:
  57. print(d)
  58.  
  59. # You can also retrieve devices by the UUID/name
  60. # a_device = manager.get_device_by_name("My Plug")
  61. # a_device = manager.get_device_by_uuid("My Plug")
  62.  
  63. # Or you can retrieve all the device by the HW type
  64. # all_mss310 = manager.get_devices_by_type("mss310")
  65.  
  66. # ------------------------------
  67. # Let's play the garage openers.
  68. # ------------------------------
  69. for g in door_openers:
  70. if not g.online:
  71. print("The garage controller %s seems to be offline. Cannot play with that..." % g.name)
  72. continue
  73.  
  74. print("Opening door %s..." % g.name)
  75. g.open_door()
  76. print("Closing door %s..." % g.name)
  77. g.close_door()
  78.  
  79. # ---------------------
  80. # Let's play with bulbs
  81. # ---------------------
  82. for b in bulbs: # type: GenericBulb
  83. if not b.online:
  84. print("The bulb %s seems to be offline. Cannot play with that..." % b.name)
  85. continue
  86.  
  87. print("Let's play with bulb %s" % b.name)
  88. if not b.supports_light_control():
  89. print("Too bad bulb %s does not support light control %s" % b.name)
  90. else:
  91. # Let's make it red!
  92. b.set_light_color(rgb=(255, 0, 0))
  93.  
  94. b.turn_on()
  95. time.sleep(1)
  96. b.turn_off()
  97.  
  98. # ---------------------------
  99. # Let's play with smart plugs
  100. # ---------------------------
  101. for p in plugs: # type: GenericPlug
  102. if not p.online:
  103. print("The plug %s seems to be offline. Cannot play with that..." % p.name)
  104. continue
  105.  
  106. print("Let's play with smart plug %s" % p.name)
  107.  
  108. channels = len(p.get_channels())
  109. print("The plug %s supports %d channels." % (p.name, channels))
  110. for i in range(0, channels):
  111. print("Turning on channel %d of %s" % (i, p.name))
  112. p.turn_on_channel(i)
  113.  
  114. time.sleep(1)
  115.  
  116. print("Turning off channel %d of %s" % (i, p.name))
  117. p.turn_off_channel(i)
  118.  
  119. usb_channel = p.get_usb_channel_index()
  120. if usb_channel is not None:
  121. print("Awesome! This device also supports USB power.")
  122. p.enable_usb()
  123. time.sleep(1)
  124. p.disable_usb()
  125.  
  126. if p.supports_electricity_reading():
  127. print("Awesome! This device also supports power consumption reading.")
  128. print("Current consumption is: %s" % str(p.get_electricity()))
  129.  
  130. # At this point, we are all done playing with the library, so we gracefully disconnect and clean resources.
  131. print("We are done playing. Cleaning resources...")
  132. manager.stop()
  133.  
  134. print("Bye bye!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement