-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
sensor.py
67 lines (59 loc) · 2.21 KB
/
sensor.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
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.const import (
CONF_ON_CLICK,
CONF_ON_DOUBLE_CLICK,
CONF_TRIGGER_ID,
)
from .. import miot # pylint: disable=relative-beyond-top-level
CONF_BUTTON = "button"
CONF_ON_LONG_PRESS = "on_long_press"
CODEOWNERS = ["@dentra"]
AUTO_LOAD = ["miot"]
miot_ylai003_ns = cg.esphome_ns.namespace("miot_ylai003")
MiotYLAI003 = miot_ylai003_ns.class_("MiotYLAI003", miot.MiotComponent)
MiotYLAI003Trigger = miot_ylai003_ns.class_(
"MiotYLAI003Trigger", automation.Trigger.template(), miot.MiotListener
)
ButtonEventType = miot.miot_ns.namespace("ButtonEvent").enum("Type")
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(MiotYLAI003),
cv.Optional(CONF_ON_CLICK): automation.validate_automation(
cv.Schema(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(MiotYLAI003Trigger),
}
),
),
cv.Optional(CONF_ON_DOUBLE_CLICK): automation.validate_automation(
cv.Schema(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(MiotYLAI003Trigger),
}
),
),
cv.Optional(CONF_ON_LONG_PRESS): automation.validate_automation(
cv.Schema(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(MiotYLAI003Trigger),
}
),
),
},
).extend(miot.MIOT_BLE_DEVICE_SCHEMA)
async def new_trigger_(config, field, button_type):
for conf in config.get(field, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], button_type)
await miot.register_miot_device(trigger, config)
await miot.setup_device_core_(trigger, config)
await automation.build_automation(trigger, [], conf)
async def to_code(config):
"""Code generation entry point"""
await miot.new_device(config)
await new_trigger_(config, CONF_ON_CLICK, ButtonEventType.BUTTON_CLICK)
await new_trigger_(
config, CONF_ON_DOUBLE_CLICK, ButtonEventType.BUTTON_DOUBLE_CLICK
)
await new_trigger_(config, CONF_ON_LONG_PRESS, ButtonEventType.BUTTON_LONG_PRESS)