From 3218399bbc1ea07f8595fd3a3c37a683e0d96758 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 26 Dec 2021 19:34:49 +0100 Subject: [PATCH] Refactoring --- components/xiaomi_ble/xiaomi_ble.cpp | 7 ++- components/xiaomi_ble/xiaomi_ble.h | 3 +- components/xiaomi_ylkg07yl/__init__.py | 50 ------------------ components/xiaomi_ylkg07yl/xiaomi_ylkg07yl.h | 55 -------------------- yeedimmer_ylkg07yl.yaml | 17 ++---- 5 files changed, 13 insertions(+), 119 deletions(-) diff --git a/components/xiaomi_ble/xiaomi_ble.cpp b/components/xiaomi_ble/xiaomi_ble.cpp index 4131748..ff4f59d 100644 --- a/components/xiaomi_ble/xiaomi_ble.cpp +++ b/components/xiaomi_ble/xiaomi_ble.cpp @@ -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)) { diff --git a/components/xiaomi_ble/xiaomi_ble.h b/components/xiaomi_ble/xiaomi_ble.h index cec1f7a..f32b69b 100644 --- a/components/xiaomi_ble/xiaomi_ble.h +++ b/components/xiaomi_ble/xiaomi_ble.h @@ -30,6 +30,8 @@ struct XiaomiParseResult { } type; std::string name; optional keycode; + optional dimmer; + optional press_type; optional temperature; optional humidity; optional moisture; @@ -42,7 +44,6 @@ struct XiaomiParseResult { optional is_active; optional has_motion; optional is_light; - optional is_long_press; bool has_data; // 0x40 bool has_capability; // 0x20 bool has_encryption; // 0x08 diff --git a/components/xiaomi_ylkg07yl/__init__.py b/components/xiaomi_ylkg07yl/__init__.py index 5d8ae2a..11f274a 100644 --- a/components/xiaomi_ylkg07yl/__init__.py +++ b/components/xiaomi_ylkg07yl/__init__.py @@ -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") @@ -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): @@ -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) diff --git a/components/xiaomi_ylkg07yl/xiaomi_ylkg07yl.h b/components/xiaomi_ylkg07yl/xiaomi_ylkg07yl.h index a8ce149..8bed957 100644 --- a/components/xiaomi_ylkg07yl/xiaomi_ylkg07yl.h +++ b/components/xiaomi_ylkg07yl/xiaomi_ylkg07yl.h @@ -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 diff --git a/yeedimmer_ylkg07yl.yaml b/yeedimmer_ylkg07yl.yaml index 736fc41..6382db2 100644 --- a/yeedimmer_ylkg07yl.yaml +++ b/yeedimmer_ylkg07yl.yaml @@ -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