Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi committed Dec 26, 2021
1 parent 015d10c commit 3218399
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 119 deletions.
7 changes: 6 additions & 1 deletion components/xiaomi_ble/xiaomi_ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ bool parse_xiaomi_value(uint8_t value_type, const uint8_t *data, uint8_t value_l
// remote control key code, 3 bytes
if ((value_type == 0x01) && (value_length == 3)) {
result.keycode = data[0];
result.is_long_press = data[2] == 2;
result.dimmer = data[1];
result.press_type = data[2];
ESP_LOGD(TAG, "Key code: %d", data[0]); // button
ESP_LOGD(TAG, "Dimmer: %d", data[1]); // value
ESP_LOGD(TAG, "Press type: %d", data[2]); // 0: single press, 1: double press, 2: long press, 3: ???,
// 4: dimmer <= 127 = rotate right / else: rotate left
}
// motion detection, 1 byte, 8-bit unsigned integer
else if ((value_type == 0x03) && (value_length == 1)) {
Expand Down
3 changes: 2 additions & 1 deletion components/xiaomi_ble/xiaomi_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ struct XiaomiParseResult {
} type;
std::string name;
optional<int> keycode;
optional<int> dimmer;
optional<int> press_type;
optional<float> temperature;
optional<float> humidity;
optional<float> moisture;
Expand All @@ -42,7 +44,6 @@ struct XiaomiParseResult {
optional<bool> is_active;
optional<bool> has_motion;
optional<bool> is_light;
optional<bool> is_long_press;
bool has_data; // 0x40
bool has_capability; // 0x20
bool has_encryption; // 0x08
Expand Down
50 changes: 0 additions & 50 deletions components/xiaomi_ylkg07yl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,9 @@

CONF_LAST_BUTTON_PRESSED = "last_button_pressed"
CONF_ON_BUTTON_ON = "on_button_on"
CONF_ON_BUTTON_OFF = "on_button_off"
CONF_ON_BUTTON_SUN = "on_button_sun"
CONF_ON_BUTTON_M = "on_button_m"
CONF_ON_BUTTON_PLUS = "on_button_plus"
CONF_ON_BUTTON_MINUS = "on_button_minus"

ON_PRESS_ACTIONS = [
CONF_ON_BUTTON_ON,
CONF_ON_BUTTON_OFF,
CONF_ON_BUTTON_SUN,
CONF_ON_BUTTON_M,
CONF_ON_BUTTON_PLUS,
CONF_ON_BUTTON_MINUS,
]

xiaomi_ylkg07yl_ns = cg.esphome_ns.namespace("xiaomi_ylkg07yl")
Expand All @@ -42,21 +32,6 @@
OnButtonOnTrigger = xiaomi_ylkg07yl_ns.class_(
"OnButtonOnTrigger", automation.Trigger.template()
)
OnButtonOffTrigger = xiaomi_ylkg07yl_ns.class_(
"OnButtonOffTrigger", automation.Trigger.template()
)
OnButtonSunTrigger = xiaomi_ylkg07yl_ns.class_(
"OnButtonSunTrigger", automation.Trigger.template()
)
OnButtonMTrigger = xiaomi_ylkg07yl_ns.class_(
"OnButtonMTrigger", automation.Trigger.template()
)
OnButtonPlusTrigger = xiaomi_ylkg07yl_ns.class_(
"OnButtonPlusTrigger", automation.Trigger.template()
)
OnButtonMinusTrigger = xiaomi_ylkg07yl_ns.class_(
"OnButtonMinusTrigger", automation.Trigger.template()
)


def validate_short_bind_key(value):
Expand Down Expand Up @@ -91,31 +66,6 @@ def validate_short_bind_key(value):
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OnButtonOnTrigger),
}
),
cv.Optional(CONF_ON_BUTTON_OFF): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OnButtonOffTrigger),
}
),
cv.Optional(CONF_ON_BUTTON_SUN): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OnButtonSunTrigger),
}
),
cv.Optional(CONF_ON_BUTTON_M): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OnButtonMTrigger),
}
),
cv.Optional(CONF_ON_BUTTON_PLUS): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OnButtonPlusTrigger),
}
),
cv.Optional(CONF_ON_BUTTON_MINUS): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OnButtonMinusTrigger),
}
),
}
)
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
Expand Down
55 changes: 0 additions & 55 deletions components/xiaomi_ylkg07yl/xiaomi_ylkg07yl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,61 +50,6 @@ class OnButtonOnTrigger : public Trigger<> {
}
};

class OnButtonOffTrigger : public Trigger<> {
public:
OnButtonOffTrigger(XiaomiYLKG07YL *a_remote) {
a_remote->add_on_receive_callback([this](int keycode) {
if (keycode == BUTTON_OFF) {
this->trigger();
}
});
}
};

class OnButtonSunTrigger : public Trigger<> {
public:
OnButtonSunTrigger(XiaomiYLKG07YL *a_remote) {
a_remote->add_on_receive_callback([this](int keycode) {
if (keycode == BUTTON_SUN) {
this->trigger();
}
});
}
};

class OnButtonMTrigger : public Trigger<> {
public:
OnButtonMTrigger(XiaomiYLKG07YL *a_remote) {
a_remote->add_on_receive_callback([this](int keycode) {
if (keycode == BUTTON_M) {
this->trigger();
}
});
}
};

class OnButtonPlusTrigger : public Trigger<> {
public:
OnButtonPlusTrigger(XiaomiYLKG07YL *a_remote) {
a_remote->add_on_receive_callback([this](int keycode) {
if (keycode == BUTTON_PLUS) {
this->trigger();
}
});
}
};

class OnButtonMinusTrigger : public Trigger<> {
public:
OnButtonMinusTrigger(XiaomiYLKG07YL *a_remote) {
a_remote->add_on_receive_callback([this](int keycode) {
if (keycode == BUTTON_MINUS) {
this->trigger();
}
});
}
};

} // namespace xiaomi_ylkg07yl
} // namespace esphome

Expand Down
17 changes: 5 additions & 12 deletions yeedimmer_ylkg07yl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,20 @@ external_components:
# refresh: 0s

wifi:
ssid: !secret wifi_ssid_front
password: !secret wifi_password_front
ssid: !secret wifi_ssid_back
password: !secret wifi_password_back
fast_connect: true

ota:
api:

logger:
level: VERY_VERBOSE
logs:
uart_esp32: WARN
api.service: WARN
ota: WARN
sensor: WARN
esp32_ble_tracker: WARN
wifi: WARN
level: DEBUG

esp32_ble_tracker:
scan_parameters:
interval: 150ms
window: 150ms
interval: 250ms
window: 250ms
duration: 1min
active: false

Expand Down

0 comments on commit 3218399

Please sign in to comment.