-
Notifications
You must be signed in to change notification settings - Fork 22
/
gree.py
51 lines (37 loc) · 1.47 KB
/
gree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import argparse
import asyncio
import logging
from greeclimate.device import Device, DeviceInfo
from greeclimate.discovery import Discovery, Listener
logging.basicConfig(
level=logging.DEBUG, format="%(name)s - %(levelname)s - %(message)s"
)
_LOGGER = logging.getLogger(__name__)
class DiscoveryListener(Listener):
def __init__(self, bind):
"""Initialize the event handler."""
super().__init__()
self.bind = bind
"""Class to handle incoming device discovery events."""
async def device_found(self, device_info: DeviceInfo) -> None:
"""A new device was found on the network."""
if self.bind:
device = Device(device_info)
await device.bind()
await device.request_version()
_LOGGER.info(f"Device firmware: {device.hid}")
async def run_discovery(bind=False):
"""Run the device discovery process."""
_LOGGER.debug("Scanning network for Gree devices")
discovery = Discovery()
listener = DiscoveryListener(bind)
discovery.add_listener(listener)
await discovery.scan(wait_for=10)
_LOGGER.info("Done discovering devices")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Gree command line utility.")
parser.add_argument("--discovery", default=False, action="store_true")
parser.add_argument("--bind", default=False, action="store_true")
args = parser.parse_args()
if args.discovery:
asyncio.run(run_discovery(args.bind))