|
1 |
| -"""Run an example script to quickly test any MyQ account.""" |
| 1 | +"""Run an example script to quickly test.""" |
2 | 2 | import asyncio
|
3 | 3 | import logging
|
4 |
| -import json |
5 | 4 |
|
6 | 5 | from aiohttp import ClientSession
|
7 | 6 |
|
8 |
| -import pymyq |
9 |
| -from pymyq.device import STATE_CLOSED, STATE_OPEN |
10 |
| -from pymyq.errors import MyQError |
| 7 | +from pymyq import login |
| 8 | +from pymyq.errors import MyQError, RequestError |
11 | 9 |
|
12 |
| -# Provide your email and password account details for MyQ. |
13 |
| -MYQ_ACCOUNT_EMAIL = '<EMAIL>' |
14 |
| -MYQ_ACCOUNT_PASSWORD = '<PASSWORD>' |
| 10 | +_LOGGER = logging.getLogger() |
15 | 11 |
|
16 |
| -# BRAND can be one of the following: |
17 |
| -# liftmaster |
18 |
| -# chamberlain |
19 |
| -# craftsmaster |
20 |
| -# merlin |
21 |
| -MYQ_BRAND = '<BRAND>' |
22 |
| -LOGLEVEL = 'ERROR' |
23 |
| - |
24 |
| -# Set JSON_DUMP to True to dump all the device information retrieved, |
25 |
| -# this can be helpful to determine what else is available. |
26 |
| -# Set JSON_DUMP to False to open/close the doors instead. i.e.: |
27 |
| -# JSON_DUMP = False |
28 |
| -JSON_DUMP = True |
| 12 | +EMAIL = "<EMAIL>" |
| 13 | +PASSWORD = "<PASSWORD>" |
29 | 14 |
|
30 | 15 |
|
31 | 16 | async def main() -> None:
|
32 | 17 | """Create the aiohttp session and run the example."""
|
33 |
| - |
34 |
| - loglevels = dict((logging.getLevelName(level), level) |
35 |
| - for level in [10, 20, 30, 40, 50]) |
36 |
| - |
37 |
| - logging.basicConfig( |
38 |
| - level=loglevels[LOGLEVEL], |
39 |
| - format='%(asctime)s:%(levelname)s:\t%(name)s\t%(message)s') |
40 |
| - |
| 18 | + logging.basicConfig(level=logging.INFO) |
41 | 19 | async with ClientSession() as websession:
|
42 | 20 | try:
|
43 |
| - myq = await pymyq.login( |
44 |
| - MYQ_ACCOUNT_EMAIL, MYQ_ACCOUNT_PASSWORD, MYQ_BRAND, websession) |
45 |
| - |
46 |
| - devices = await myq.get_devices() |
47 |
| - for idx, device in enumerate(devices): |
48 |
| - print('Device #{0}: {1}'.format(idx + 1, device.name)) |
49 |
| - print('--------') |
50 |
| - print('Brand: {0}'.format(device.brand)) |
51 |
| - print('Type: {0}'.format(device.type)) |
52 |
| - print('Serial: {0}'.format(device.serial)) |
53 |
| - print('Device ID: {0}'.format(device.device_id)) |
54 |
| - print('Parent ID: {0}'.format(device.parent_id)) |
55 |
| - print('Online: {0}'.format(device.available)) |
56 |
| - print('Unattended Open: {0}'.format(device.open_allowed)) |
57 |
| - print('Unattended Close: {0}'.format(device.close_allowed)) |
58 |
| - print() |
59 |
| - print('Current State: {0}'.format(device.state)) |
60 |
| - if JSON_DUMP: |
61 |
| - print(json.dumps(device._device, indent=4)) |
62 |
| - else: |
63 |
| - if device.state != STATE_OPEN: |
64 |
| - print('Opening the device...') |
65 |
| - await device.open() |
66 |
| - print(' 0 Current State: {0}'.format(device.state)) |
67 |
| - for waited in range(1, 30): |
68 |
| - if device.state == STATE_OPEN: |
69 |
| - break |
70 |
| - await asyncio.sleep(1) |
71 |
| - await device.update() |
72 |
| - print(' {} Current State: {}'.format( |
73 |
| - waited, device.state)) |
74 |
| - |
75 |
| - await asyncio.sleep(10) |
76 |
| - await device.update() |
77 |
| - print() |
78 |
| - print('Current State: {0}'.format(device.state)) |
79 |
| - |
80 |
| - if device.state != STATE_CLOSED: |
81 |
| - print('Closing the device...') |
82 |
| - await device.close() |
83 |
| - print(' 0 Current State: {0}'.format(device.state)) |
84 |
| - for waited in range(1, 30): |
85 |
| - if device.state == STATE_CLOSED: |
86 |
| - break |
87 |
| - await asyncio.sleep(1) |
88 |
| - await device.update() |
89 |
| - print(' {} Current State: {}'.format( |
90 |
| - waited, device.state)) |
91 |
| - |
92 |
| - await asyncio.sleep(10) |
93 |
| - await device.update() |
94 |
| - print() |
95 |
| - print('Current State: {0}'.format(device.state)) |
| 21 | + # Create an API object: |
| 22 | + api = await login(EMAIL, PASSWORD, websession) |
| 23 | + |
| 24 | + # Get the account ID: |
| 25 | + _LOGGER.info("Account ID: %s", api.account_id) |
| 26 | + |
| 27 | + # Get all devices listed with this account – note that you can use |
| 28 | + # api.covers to only examine covers: |
| 29 | + for idx, device_id in enumerate(api.devices): |
| 30 | + device = api.devices[device_id] |
| 31 | + _LOGGER.info("---------") |
| 32 | + _LOGGER.info("Device %s: %s", idx + 1, device.name) |
| 33 | + _LOGGER.info("Device Online: %s", device.online) |
| 34 | + _LOGGER.info("Device ID: %s", device.device_id) |
| 35 | + _LOGGER.info("Parent Device ID: %s", device.parent_device_id) |
| 36 | + _LOGGER.info("Device Family: %s", device.device_family) |
| 37 | + _LOGGER.info("Device Platform: %s", device.device_platform) |
| 38 | + _LOGGER.info("Device Type: %s", device.device_type) |
| 39 | + _LOGGER.info("Firmware Version: %s", device.firmware_version) |
| 40 | + _LOGGER.info("Open Allowed: %s", device.open_allowed) |
| 41 | + _LOGGER.info("Close Allowed: %s", device.close_allowed) |
| 42 | + _LOGGER.info("Current State: %s", device.state) |
| 43 | + |
| 44 | + try: |
| 45 | + await device.open() |
| 46 | + await asyncio.sleep(15) |
| 47 | + await device.close() |
| 48 | + except RequestError as err: |
| 49 | + _LOGGER.error(err) |
96 | 50 | except MyQError as err:
|
97 |
| - print(err) |
| 51 | + _LOGGER.error("There was an error: %s", err) |
98 | 52 |
|
99 | 53 |
|
100 | 54 | asyncio.get_event_loop().run_until_complete(main())
|
0 commit comments