33
33
DEFAULT_BAUD = 9600
34
34
DEFAULT_ADC_MAX_VOLTS = 1.2
35
35
36
- # Copied from xbee_helper during setup()
37
- GPIO_DIGITAL_OUTPUT_LOW = None
38
- GPIO_DIGITAL_OUTPUT_HIGH = None
39
- ADC_PERCENTAGE = None
40
- DIGITAL_PINS = None
41
- ANALOG_PINS = None
42
- CONVERT_ADC = None
43
- ZIGBEE_EXCEPTION = None
44
- ZIGBEE_TX_FAILURE = None
45
-
46
36
ATTR_FRAME = "frame"
47
37
48
- DEVICE = None
49
-
50
38
CONFIG_SCHEMA = vol .Schema (
51
39
{
52
40
DOMAIN : vol .Schema (
71
59
72
60
def setup (hass , config ):
73
61
"""Set up the connection to the Zigbee device."""
74
- global DEVICE # pylint: disable=global-statement
75
- global GPIO_DIGITAL_OUTPUT_LOW # pylint: disable=global-statement
76
- global GPIO_DIGITAL_OUTPUT_HIGH # pylint: disable=global-statement
77
- global ADC_PERCENTAGE # pylint: disable=global-statement
78
- global DIGITAL_PINS # pylint: disable=global-statement
79
- global ANALOG_PINS # pylint: disable=global-statement
80
- global CONVERT_ADC # pylint: disable=global-statement
81
- global ZIGBEE_EXCEPTION # pylint: disable=global-statement
82
- global ZIGBEE_TX_FAILURE # pylint: disable=global-statement
83
-
84
- GPIO_DIGITAL_OUTPUT_LOW = xb_const .GPIO_DIGITAL_OUTPUT_LOW
85
- GPIO_DIGITAL_OUTPUT_HIGH = xb_const .GPIO_DIGITAL_OUTPUT_HIGH
86
- ADC_PERCENTAGE = xb_const .ADC_PERCENTAGE
87
- DIGITAL_PINS = xb_const .DIGITAL_PINS
88
- ANALOG_PINS = xb_const .ANALOG_PINS
89
- CONVERT_ADC = convert_adc
90
- ZIGBEE_EXCEPTION = ZigBeeException
91
- ZIGBEE_TX_FAILURE = ZigBeeTxFailure
92
62
93
63
usb_device = config [DOMAIN ].get (CONF_DEVICE , DEFAULT_DEVICE )
94
64
baud = int (config [DOMAIN ].get (CONF_BAUD , DEFAULT_BAUD ))
@@ -97,8 +67,11 @@ def setup(hass, config):
97
67
except SerialException as exc :
98
68
_LOGGER .exception ("Unable to open serial port for Zigbee: %s" , exc )
99
69
return False
100
- DEVICE = ZigBee (ser )
101
- hass .bus .listen_once (EVENT_HOMEASSISTANT_STOP , close_serial_port )
70
+ zigbee_device = ZigBee (ser )
71
+
72
+ def close_serial_port (* args ):
73
+ """Close the serial port we're using to communicate with the Zigbee."""
74
+ zigbee_device .zb .serial .close ()
102
75
103
76
def _frame_received (frame ):
104
77
"""Run when a Zigbee frame is received.
@@ -108,16 +81,13 @@ def _frame_received(frame):
108
81
"""
109
82
dispatcher_send (hass , SIGNAL_ZIGBEE_FRAME_RECEIVED , frame )
110
83
111
- DEVICE .add_frame_rx_handler (_frame_received )
84
+ hass .data [DOMAIN ] = zigbee_device
85
+ hass .bus .listen_once (EVENT_HOMEASSISTANT_STOP , close_serial_port )
86
+ zigbee_device .add_frame_rx_handler (_frame_received )
112
87
113
88
return True
114
89
115
90
116
- def close_serial_port (* args ):
117
- """Close the serial port we're using to communicate with the Zigbee."""
118
- DEVICE .zb .serial .close ()
119
-
120
-
121
91
def frame_is_relevant (entity , frame ):
122
92
"""Test whether the frame is relevant to the entity."""
123
93
if frame .get ("source_addr_long" ) != entity .config .address :
@@ -229,13 +199,13 @@ def boolean_maps(self):
229
199
"""
230
200
if self ._config .get ("on_state" , "" ).lower () == "low" :
231
201
bool2state = {
232
- True : GPIO_DIGITAL_OUTPUT_LOW ,
233
- False : GPIO_DIGITAL_OUTPUT_HIGH ,
202
+ True : xb_const . GPIO_DIGITAL_OUTPUT_LOW ,
203
+ False : xb_const . GPIO_DIGITAL_OUTPUT_HIGH ,
234
204
}
235
205
else :
236
206
bool2state = {
237
- True : GPIO_DIGITAL_OUTPUT_HIGH ,
238
- False : GPIO_DIGITAL_OUTPUT_LOW ,
207
+ True : xb_const . GPIO_DIGITAL_OUTPUT_HIGH ,
208
+ False : xb_const . GPIO_DIGITAL_OUTPUT_LOW ,
239
209
}
240
210
state2bool = {v : k for k , v in bool2state .items ()}
241
211
return bool2state , state2bool
@@ -269,9 +239,10 @@ def max_voltage(self):
269
239
class ZigBeeDigitalIn (Entity ):
270
240
"""Representation of a GPIO pin configured as a digital input."""
271
241
272
- def __init__ (self , hass , config ):
242
+ def __init__ (self , config , device ):
273
243
"""Initialize the device."""
274
244
self ._config = config
245
+ self ._device = device
275
246
self ._state = False
276
247
277
248
async def async_added_to_hass (self ):
@@ -286,7 +257,7 @@ def handle_frame(frame):
286
257
if not frame_is_relevant (self , frame ):
287
258
return
288
259
sample = next (iter (frame ["samples" ]))
289
- pin_name = DIGITAL_PINS [self ._config .pin ]
260
+ pin_name = xb_const . DIGITAL_PINS [self ._config .pin ]
290
261
if pin_name not in sample :
291
262
# Doesn't contain information about our pin
292
263
return
@@ -322,18 +293,18 @@ def is_on(self):
322
293
def update (self ):
323
294
"""Ask the Zigbee device what state its input pin is in."""
324
295
try :
325
- sample = DEVICE .get_sample (self ._config .address )
326
- except ZIGBEE_TX_FAILURE :
296
+ sample = self . _device .get_sample (self ._config .address )
297
+ except ZigBeeTxFailure :
327
298
_LOGGER .warning (
328
299
"Transmission failure when attempting to get sample from "
329
300
"Zigbee device at address: %s" ,
330
301
hexlify (self ._config .address ),
331
302
)
332
303
return
333
- except ZIGBEE_EXCEPTION as exc :
304
+ except ZigBeeException as exc :
334
305
_LOGGER .exception ("Unable to get sample from Zigbee device: %s" , exc )
335
306
return
336
- pin_name = DIGITAL_PINS [self ._config .pin ]
307
+ pin_name = xb_const . DIGITAL_PINS [self ._config .pin ]
337
308
if pin_name not in sample :
338
309
_LOGGER .warning (
339
310
"Pin %s (%s) was not in the sample provided by Zigbee device %s." ,
@@ -351,17 +322,17 @@ class ZigBeeDigitalOut(ZigBeeDigitalIn):
351
322
def _set_state (self , state ):
352
323
"""Initialize the Zigbee digital out device."""
353
324
try :
354
- DEVICE .set_gpio_pin (
325
+ self . _device .set_gpio_pin (
355
326
self ._config .pin , self ._config .bool2state [state ], self ._config .address
356
327
)
357
- except ZIGBEE_TX_FAILURE :
328
+ except ZigBeeTxFailure :
358
329
_LOGGER .warning (
359
330
"Transmission failure when attempting to set output pin on "
360
331
"Zigbee device at address: %s" ,
361
332
hexlify (self ._config .address ),
362
333
)
363
334
return
364
- except ZIGBEE_EXCEPTION as exc :
335
+ except ZigBeeException as exc :
365
336
_LOGGER .exception ("Unable to set digital pin on Zigbee device: %s" , exc )
366
337
return
367
338
self ._state = state
@@ -379,15 +350,17 @@ def turn_off(self, **kwargs):
379
350
def update (self ):
380
351
"""Ask the Zigbee device what its output is set to."""
381
352
try :
382
- pin_state = DEVICE .get_gpio_pin (self ._config .pin , self ._config .address )
383
- except ZIGBEE_TX_FAILURE :
353
+ pin_state = self ._device .get_gpio_pin (
354
+ self ._config .pin , self ._config .address
355
+ )
356
+ except ZigBeeTxFailure :
384
357
_LOGGER .warning (
385
358
"Transmission failure when attempting to get output pin status"
386
359
" from Zigbee device at address: %s" ,
387
360
hexlify (self ._config .address ),
388
361
)
389
362
return
390
- except ZIGBEE_EXCEPTION as exc :
363
+ except ZigBeeException as exc :
391
364
_LOGGER .exception (
392
365
"Unable to get output pin status from Zigbee device: %s" , exc
393
366
)
@@ -398,9 +371,10 @@ def update(self):
398
371
class ZigBeeAnalogIn (Entity ):
399
372
"""Representation of a GPIO pin configured as an analog input."""
400
373
401
- def __init__ (self , hass , config ):
374
+ def __init__ (self , config , device ):
402
375
"""Initialize the ZigBee analog in device."""
403
376
self ._config = config
377
+ self ._device = device
404
378
self ._value = None
405
379
406
380
async def async_added_to_hass (self ):
@@ -415,12 +389,12 @@ def handle_frame(frame):
415
389
if not frame_is_relevant (self , frame ):
416
390
return
417
391
sample = frame ["samples" ].pop ()
418
- pin_name = ANALOG_PINS [self ._config .pin ]
392
+ pin_name = xb_const . ANALOG_PINS [self ._config .pin ]
419
393
if pin_name not in sample :
420
394
# Doesn't contain information about our pin
421
395
return
422
- self ._value = CONVERT_ADC (
423
- sample [pin_name ], ADC_PERCENTAGE , self ._config .max_voltage
396
+ self ._value = convert_adc (
397
+ sample [pin_name ], xb_const . ADC_PERCENTAGE , self ._config .max_voltage
424
398
)
425
399
self .schedule_update_ha_state ()
426
400
@@ -454,17 +428,17 @@ def unit_of_measurement(self):
454
428
def update (self ):
455
429
"""Get the latest reading from the ADC."""
456
430
try :
457
- self ._value = DEVICE .read_analog_pin (
431
+ self ._value = self . _device .read_analog_pin (
458
432
self ._config .pin ,
459
433
self ._config .max_voltage ,
460
434
self ._config .address ,
461
- ADC_PERCENTAGE ,
435
+ xb_const . ADC_PERCENTAGE ,
462
436
)
463
- except ZIGBEE_TX_FAILURE :
437
+ except ZigBeeTxFailure :
464
438
_LOGGER .warning (
465
439
"Transmission failure when attempting to get sample from "
466
440
"Zigbee device at address: %s" ,
467
441
hexlify (self ._config .address ),
468
442
)
469
- except ZIGBEE_EXCEPTION as exc :
443
+ except ZigBeeException as exc :
470
444
_LOGGER .exception ("Unable to get sample from Zigbee device: %s" , exc )
0 commit comments