OWNd2 0.7.49__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
OWNd/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ """ OWNd - an OpenWebNet daemon """ # pylint: disable=invalid-name
2
+ __version__ = "0.7.48"
OWNd/__main__.py ADDED
@@ -0,0 +1,179 @@
1
+ """ OWNd entry point when running it directly from CLI
2
+ (as opposed to imported into another project)
3
+ """
4
+ import argparse
5
+ import asyncio
6
+ import logging
7
+
8
+ from OWNd.message import OWNMessage
9
+
10
+ from .connection import OWNEventSession, OWNGateway, ZigbeeOWNGateway, zigbeeSession
11
+
12
+
13
+ async def main(arguments: dict, connection: OWNEventSession) -> None:
14
+ """Package entry point!"""
15
+
16
+ serialPort = (
17
+ arguments["serialPort"]
18
+ if "serialPort" in arguments and isinstance(arguments["serialPort"], str)
19
+ else None
20
+ )
21
+ address = (
22
+ arguments["address"]
23
+ if "address" in arguments and isinstance(arguments["address"], str)
24
+ else None
25
+ )
26
+ port = (
27
+ arguments["port"]
28
+ if "port" in arguments and isinstance(arguments["port"], int)
29
+ else None
30
+ )
31
+ password = (
32
+ arguments["password"]
33
+ if "password" in arguments and isinstance(arguments["password"], str)
34
+ else None
35
+ )
36
+ serial_number = (
37
+ arguments["serialNumber"]
38
+ if "serialNumber" in arguments and isinstance(arguments["serialNumber"], str)
39
+ else None
40
+ )
41
+ logger = (
42
+ arguments["logger"]
43
+ if "logger" in arguments and isinstance(arguments["logger"], logging.Logger)
44
+ else None
45
+ )
46
+
47
+ if serialPort is not None:
48
+ # case of zigbee
49
+ logger.info("Starting Zigbee/OPEN on serial port <%s>", serialPort)
50
+ gateway = await ZigbeeOWNGateway.build_from_discovery_info(
51
+ {
52
+ "serialPort": serialPort,
53
+ "port": port,
54
+ }
55
+ )
56
+ zb = zigbeeSession(gateway, _logger)
57
+ await zb.connect()
58
+ logger.info("Zigbee/OPEN Gateway ready.")
59
+ else:
60
+ logger.info("Starting discovery of a supported gateway via SSDP")
61
+ gateway = await OWNGateway.build_from_discovery_info(
62
+ {
63
+ "address": address,
64
+ "port": port,
65
+ "password": password,
66
+ "serialNumber": serial_number,
67
+ }
68
+ )
69
+
70
+ connection.gateway = gateway
71
+
72
+ if logger is not None:
73
+ connection.logger = logger
74
+
75
+ res = await connection.test_connection()
76
+ if res["Success"]:
77
+ logger.info("Starting connection to the discovered gateway")
78
+ await connection.connect()
79
+
80
+ logger.info("Now waiting for events from the gateway (e.g. a cover opening/closing)")
81
+ try:
82
+ while True:
83
+ message = await connection.get_next()
84
+ if message:
85
+ logger.debug("Received: %s", message)
86
+ if isinstance(message, OWNMessage) and message.is_event:
87
+ logger.info(message.human_readable_log)
88
+ finally:
89
+ if zb is not None:
90
+ await zb.close()
91
+ else:
92
+ logger.error("Error during test: %s", res["Message"])
93
+
94
+ if __name__ == "__main__":
95
+
96
+ parser = argparse.ArgumentParser()
97
+ parser.add_argument(
98
+ "-z", "--zigbee", type=str, help="Serial port of Zigbee/Open gateway"
99
+ )
100
+ parser.add_argument(
101
+ "-a", "--address", type=str, help="IP address of the OpenWebNet gateway"
102
+ )
103
+ parser.add_argument(
104
+ "-p",
105
+ "--port",
106
+ type=int,
107
+ help="TCP port to connectect the gateway, default is 20000",
108
+ )
109
+ parser.add_argument(
110
+ "-P",
111
+ "--password",
112
+ type=str,
113
+ help="Numeric password for the OpenWebNet connection, default is 12345",
114
+ )
115
+ parser.add_argument(
116
+ "-m",
117
+ "--mac",
118
+ type=str,
119
+ help="MAC address of the gateway (to be used as ID, if not found via SSDP)",
120
+ )
121
+ parser.add_argument(
122
+ "-v",
123
+ "--verbose",
124
+ type=int,
125
+ help="Change output verbosity [0 = WARNING; 1 = INFO (default); 2 = DEBUG]",
126
+ )
127
+ args = parser.parse_args()
128
+
129
+ # create logger with 'OWNd'
130
+ _logger = logging.getLogger("OWNd")
131
+ _logger.setLevel(logging.DEBUG)
132
+
133
+ # create console handler which logs even debug messages
134
+ log_stream_handler = logging.StreamHandler()
135
+
136
+ if args.verbose == 2:
137
+ log_stream_handler.setLevel(logging.DEBUG)
138
+ elif args.verbose == 0:
139
+ log_stream_handler.setLevel(logging.WARNING)
140
+ else:
141
+ log_stream_handler.setLevel(logging.INFO)
142
+
143
+ # create formatter and add it to the handlers
144
+ formatter = logging.Formatter(
145
+ "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
146
+ )
147
+ log_stream_handler.setFormatter(formatter)
148
+ # add the handlers to the logger
149
+ _logger.addHandler(log_stream_handler)
150
+
151
+ event_session = OWNEventSession(gateway=None, logger=_logger)
152
+ _arguments = {
153
+ "serialPort": args.zigbee,
154
+ "address": args.address,
155
+ "port": args.port,
156
+ "password": args.password,
157
+ "serialNumber": args.mac,
158
+ "logger": _logger,
159
+ }
160
+
161
+ loop = asyncio.get_event_loop()
162
+ main_task = asyncio.ensure_future(main(_arguments, event_session))
163
+ # loop.set_debug(True)
164
+
165
+ try:
166
+ _logger.info("Starting OWNd.")
167
+ loop.run_forever()
168
+ # asyncio.run(main(arguments))
169
+ except KeyboardInterrupt:
170
+ try:
171
+ _logger.info("Stoping OWNd.")
172
+ # get all task in current loop
173
+ main_task.cancel()
174
+ loop.run_until_complete(event_session.close())
175
+ finally:
176
+ loop.stop()
177
+ loop.close()
178
+ finally:
179
+ _logger.info("OWNd stopped.")