-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path__init__.py
166 lines (141 loc) · 5.26 KB
/
__init__.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from esphome.cpp_types import Component
from esphome.core import Lambda
from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor, esp32_ble_tracker, sensor, text_sensor
from esphome.const import (
CONF_BATTERY_LEVEL,
CONF_BATTERY_VOLTAGE,
CONF_ID,
CONF_LAMBDA,
CONF_MAC_ADDRESS,
CONF_THEN,
CONF_TRIGGER_ID,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_VOLTAGE,
ENTITY_CATEGORY_DIAGNOSTIC,
PLATFORM_ESP32,
STATE_CLASS_MEASUREMENT,
UNIT_PERCENT,
UNIT_VOLT,
)
from .. import xiaomi_account # pylint: disable=relative-beyond-top-level
CODEOWNERS = ["@dentra"]
ESP_PLATFORMS = [PLATFORM_ESP32]
AUTO_LOAD = ["esp32_ble_tracker", "sensor"]
CONF_MIOT_ID = "miot_id"
CONF_ON_MIOT_ADVERTISE = "on_miot_advertise"
CONF_PRODUCT_ID = "product_id"
CONF_RSSI = "rssi"
CONF_DEBUG = "debug"
miot_ns = cg.esphome_ns.namespace("miot")
MiBeaconTracker = miot_ns.class_(
"MiBeaconTracker", esp32_ble_tracker.ESPBTDeviceListener, Component
)
MiotListener = miot_ns.class_("MiotListener")
MiotComponent = miot_ns.class_("MiotComponent", MiotListener, Component)
BLEObjectConstRef = miot_ns.class_("BLEObject").operator("ref").operator("const")
MiotAdvertiseTrigger = miot_ns.class_(
"MiotAdvertiseTrigger", automation.Trigger.template(BLEObjectConstRef)
)
MIOT_BLE_DEVICE_CORE_SCHEMA = (
cv.Schema(
{
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
}
)
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
.extend(cv.COMPONENT_SCHEMA)
.extend(xiaomi_account.BINDKEY_SCHEMA)
)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(MiBeaconTracker),
cv.Optional(CONF_ON_MIOT_ADVERTISE): automation.validate_automation(
cv.Schema(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
MiotAdvertiseTrigger
),
cv.Optional(CONF_PRODUCT_ID): cv.uint16_t,
cv.Optional(CONF_DEBUG): cv.boolean,
cv.Optional(
CONF_THEN, default={CONF_LAMBDA: Lambda("")}
): automation.validate_action_list,
}
).extend(MIOT_BLE_DEVICE_CORE_SCHEMA),
),
}
)
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
.extend(cv.COMPONENT_SCHEMA)
.extend(xiaomi_account.XIAOMI_ACCOUNT_SCHEMA)
)
MIOT_BLE_DEVICE_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_MIOT_ID): cv.use_id(MiBeaconTracker),
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=0,
device_class=DEVICE_CLASS_BATTERY,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Optional(CONF_BATTERY_VOLTAGE): sensor.sensor_schema(
unit_of_measurement=UNIT_VOLT,
accuracy_decimals=3,
device_class=DEVICE_CLASS_VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
}
).extend(MIOT_BLE_DEVICE_CORE_SCHEMA)
async def register_miot_device(var, config):
parent = await cg.get_variable(config[CONF_MIOT_ID])
cg.add(parent.register_listener(var))
return var
async def setup_device_core_(var, config):
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
xiaomi_account.set_bindkey(config, config[CONF_MAC_ADDRESS], var.set_bindkey)
async def new_device(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await register_miot_device(var, config)
await setup_device_core_(var, config)
if CONF_BATTERY_LEVEL in config:
conf = config[CONF_BATTERY_LEVEL]
sens = await sensor.new_sensor(conf)
cg.add(var.set_battery_level(sens))
if CONF_BATTERY_VOLTAGE in config:
conf = config[CONF_BATTERY_VOLTAGE]
sens = await sensor.new_sensor(conf)
cg.add(var.set_battery_voltage(sens))
return var
async def new_sensor_device(config):
var = await new_device(config)
await sensor.register_sensor(var, config)
return var
async def new_binary_sensor_device(config):
var = await new_device(config)
await binary_sensor.register_binary_sensor(var, config)
return var
async def new_text_sensor_device(config):
var = await new_device(config)
await text_sensor.register_text_sensor(var, config)
return var
async def to_code(config):
"""Code generation entry point"""
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await esp32_ble_tracker.register_ble_device(var, config)
for conf in config.get(CONF_ON_MIOT_ADVERTISE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
cg.add(var.register_listener(trigger))
await setup_device_core_(trigger, conf)
if CONF_PRODUCT_ID in conf:
cg.add(trigger.set_product_id(conf[CONF_PRODUCT_ID]))
if conf.get(CONF_DEBUG, False):
cg.add(trigger.set_debug(True))
await automation.build_automation(trigger, [(BLEObjectConstRef, "x")], conf)