FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-sdk/examples/standalone/zigbee2mqtt/script.py at main · Enapter/python-sdk · GitHub
Enapter
/
python-sdk
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
6
Code
Issues
4
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-sdk
/
examples
/
standalone
/
zigbee2mqtt
/
script.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
68 lines (56 loc) · 2.11 KB
Breadcrumbs
python-sdk
/
examples
/
standalone
/
zigbee2mqtt
/
script.py
Copy path
File metadata and controls
68 lines (56 loc) · 2.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import
asyncio
import
json
import
os
import
enapter
async
def
main
():
async
with
enapter
.
mqtt
.
Client
(
hostname
=
os
.
environ
[
"ZIGBEE_MQTT_HOST"
],
port
=
int
(
os
.
environ
[
"ZIGBEE_MQTT_PORT"
]),
username
=
os
.
environ
.
get
(
"ZIGBEE_MQTT_USER"
),
password
=
os
.
environ
.
get
(
"ZIGBEE_MQTT_PASSWORD"
),
)
as
mqtt_client
:
zigbee_mqtt
=
ZigbeeMQTT
(
mqtt_client
=
mqtt_client
,
mqtt_topic
=
os
.
environ
[
"ZIGBEE_MQTT_TOPIC"
],
sensor_manufacturer
=
os
.
environ
[
"ZIGBEE_SENSOR_MANUFACTURER"
],
sensor_model
=
os
.
environ
[
"ZIGBEE_SENSOR_MODEL"
],
)
await
enapter
.
standalone
.
run
(
zigbee_mqtt
)
class
ZigbeeMQTT
(
enapter
.
standalone
.
Device
):
def
__init__
(
self
,
mqtt_client
,
mqtt_topic
,
sensor_manufacturer
,
sensor_model
):
super
().
__init__
()
self
.
telemetry
=
{}
self
.
mqtt_client
=
mqtt_client
self
.
mqtt_topic
=
mqtt_topic
self
.
sensor_manufacturer
=
sensor_manufacturer
self
.
sensor_model
=
sensor_model
async
def
run
(
self
):
async
with
asyncio
.
TaskGroup
()
as
tg
:
tg
.
create_task
(
self
.
mqtt_consumer
())
tg
.
create_task
(
self
.
telemetry_sender
())
tg
.
create_task
(
self
.
properties_sender
())
async
def
mqtt_consumer
(
self
):
async
with
self
.
mqtt_client
.
subscribe
(
self
.
mqtt_topic
)
as
messages
:
async
for
msg
in
messages
:
try
:
self
.
telemetry
=
json
.
loads
(
msg
.
payload
)
except
json
.
JSONDecodeError
as
e
:
await
self
.
log
.
error
(
f"failed to decode json payload:
{
e
}
"
)
async
def
telemetry_sender
(
self
):
while
True
:
await
self
.
send_telemetry
(
self
.
telemetry
)
await
asyncio
.
sleep
(
1
)
async
def
properties_sender
(
self
):
while
True
:
await
self
.
send_properties
(
{
"model"
:
self
.
sensor_model
,
"manufacturer"
:
self
.
sensor_manufacturer
,
}
)
await
asyncio
.
sleep
(
10
)
if
__name__
==
"__main__"
:
try
:
asyncio
.
run
(
main
())
except
KeyboardInterrupt
:
pass
Back
|
FazBrowse Home
|
New Git URL