diff --git a/common/binary_sensor/day_night.yaml b/common/binary_sensor/day_night.yaml new file mode 100644 index 0000000..7457f85 --- /dev/null +++ b/common/binary_sensor/day_night.yaml @@ -0,0 +1,3 @@ +platform: homeassistant +id: day_night +entity_id: input_boolean.day_night diff --git a/common/binary_sensor/gosund_dimmer_button.yaml b/common/binary_sensor/gosund_dimmer_button.yaml new file mode 100644 index 0000000..963fbe2 --- /dev/null +++ b/common/binary_sensor/gosund_dimmer_button.yaml @@ -0,0 +1,28 @@ +platform: gpio +pin: + number: GPIO4 + inverted: true +id: gosund_button +on_multi_click: + - timing: + - ON for at most 1s + - OFF for at most .4s + - ON for at most 1s + - OFF for at least 0.1s + then: + - script.execute: double_tap + - timing: + - ON for at most 1s + - OFF for at least 0.4s + then: + - if: + condition: + light.is_off: gosund_light + then: + - light.turn_on: + id: gosund_light + brightness: !lambda |- + return (id(day_night) ? ${day_level} : ${night_level}) / 100.0; + else: + - light.turn_off: gosund_light + - script.execute: single_tap diff --git a/common/common_gosund_dimmer.yaml b/common/common_gosund_dimmer.yaml new file mode 100644 index 0000000..edc6f23 --- /dev/null +++ b/common/common_gosund_dimmer.yaml @@ -0,0 +1,14 @@ +<<: !include wifi.yaml +<<: !include api.yaml +<<: !include ota.yaml + +<<: !include status_led/gosund_dimmer.yaml + +light: + - !include light/gosund_dimmer.yaml + +output: + - !include output/gosund_dimmer_led.yaml + +uart: + - !include uart/gosund_dimmer.yaml diff --git a/common/light/gosund_dimmer.yaml b/common/light/gosund_dimmer.yaml new file mode 100644 index 0000000..74840bb --- /dev/null +++ b/common/light/gosund_dimmer.yaml @@ -0,0 +1,8 @@ +platform: custom +lambda: |- + auto gosund_light = new GosundSW2Light(gosund_uart, gosund_dimmer_led); + App.register_component(gosund_light); + return {gosund_light}; +lights: + - id: gosund_light + name: ${device_name} diff --git a/common/output/gosund_dimmer_led.yaml b/common/output/gosund_dimmer_led.yaml new file mode 100644 index 0000000..e755e40 --- /dev/null +++ b/common/output/gosund_dimmer_led.yaml @@ -0,0 +1,5 @@ +platform: gpio +id: gosund_dimmer_led +pin: + number: GPIO13 + inverted: true diff --git a/common/status_led/gosund_dimmer.yaml b/common/status_led/gosund_dimmer.yaml new file mode 100644 index 0000000..6bdb50d --- /dev/null +++ b/common/status_led/gosund_dimmer.yaml @@ -0,0 +1,4 @@ +status_led: + pin: + number: GPIO12 + inverted: true diff --git a/common/uart/gosund_dimmer.yaml b/common/uart/gosund_dimmer.yaml new file mode 100644 index 0000000..e114999 --- /dev/null +++ b/common/uart/gosund_dimmer.yaml @@ -0,0 +1,4 @@ +id: gosund_uart +rx_pin: GPIO3 +tx_pin: GPIO1 +baud_rate: 115200 diff --git a/dining_room_light.yaml b/dining_room_light.yaml new file mode 100644 index 0000000..86f3974 --- /dev/null +++ b/dining_room_light.yaml @@ -0,0 +1,44 @@ +substitutions: + device_id: dining_room_light + device_name: Dining Room Light + platform: ESP8266 + board: esp01_1m + ip_address: !secret dining_room_light_ip + ota_pwd: !secret dining_room_light_ota_pwd + api_pwd: !secret dining_room_light_api_pwd + ap_wifi_pwd: !secret dining_room_light_ap_wifi_pwd + + day_level: '100' + night_level: '10' + +esphome: + <<: !include common/esphome.yaml + includes: + - gosund_sw2_dimmer.h + +<<: !include common/common_gosund_dimmer.yaml +<<: !include common/logger/logger_debug_no_serial.yaml + +binary_sensor: + - !include common/binary_sensor/status.yaml + - !include common/binary_sensor/day_night.yaml + - !include common/binary_sensor/gosund_dimmer_button.yaml + +sensor: + - !include common/sensor/uptime.yaml + - !include common/sensor/wifi.yaml + +switch: + - !include common/switch/restart.yaml + +text_sensor: + - !include common/text_sensor/version.yaml + - !include common/text_sensor/wifi.yaml + +script: + - id: single_tap + then: + - logger.log: "Single Tap" + - id: double_tap + then: + - logger.log: "Double Tap" diff --git a/gosund_sw2_dimmer.h b/gosund_sw2_dimmer.h new file mode 100644 index 0000000..27b0944 --- /dev/null +++ b/gosund_sw2_dimmer.h @@ -0,0 +1,56 @@ +#pragma once +#include "esphome.h" +#include "esphome/components/output/binary_output.h" +#include "esphome/components/light/light_state.h" +#include "esphome/components/light/light_output.h" +#include "esphome/components/uart/uart.h" + +class GosundSW2Light : public Component, public uart::UARTDevice, public light::LightOutput { + public: + GosundSW2Light(UARTComponent *uart, BinaryOutput *indicator) : UARTDevice(uart) { indicator_ = indicator; } + void setup() override {} + void loop() override; + LightTraits get_traits() override; + void setup_state(light::LightState *state) override { state_ = state; } + void write_state(light::LightState *state) override; + + protected: + light::LightState *state_; + output::BinaryOutput *indicator_; +}; + +LightTraits GosundSW2Light::get_traits() { + auto traits = LightTraits(); + traits.set_supports_brightness(true); + return traits; +} + +void GosundSW2Light::write_state(light::LightState *state) { + float brightness; + state->current_values_as_brightness(&brightness); + uint8_t command; + + if (brightness == 0.0f) { + command = 0; + indicator_->turn_off(); + } + else { + command = (127.0 * brightness) + 0x80; + indicator_->turn_on(); + } + + this->write_byte(command); +} + +void GosundSW2Light::loop() { + if (this->available() >= 5) { + uint8_t data[5]; + if (this->read_array(data, 5)) { + auto brightness = float(data[1] == 0 ? 1 : data[1] > 100 ? 100 : data[1]) / 100.0; + auto call = this->state_->make_call(); + call.set_brightness(brightness); + call.set_transition_length(0); + call.perform(); + } + } +} diff --git a/living_room_lights.yaml b/living_room_lights.yaml new file mode 100644 index 0000000..f7fbab1 --- /dev/null +++ b/living_room_lights.yaml @@ -0,0 +1,44 @@ +substitutions: + device_id: living_room_lights + device_name: Living Room Lights + platform: ESP8266 + board: esp01_1m + ip_address: !secret living_room_lights_ip + ota_pwd: !secret living_room_lights_ota_pwd + api_pwd: !secret living_room_lights_api_pwd + ap_wifi_pwd: !secret living_room_lights_ap_wifi_pwd + + day_level: '100' + night_level: '10' + +esphome: + <<: !include common/esphome.yaml + includes: + - gosund_sw2_dimmer.h + +<<: !include common/common_gosund_dimmer.yaml +<<: !include common/logger/logger_debug_no_serial.yaml + +binary_sensor: + - !include common/binary_sensor/status.yaml + - !include common/binary_sensor/day_night.yaml + - !include common/binary_sensor/gosund_dimmer_button.yaml + +sensor: + - !include common/sensor/uptime.yaml + - !include common/sensor/wifi.yaml + +switch: + - !include common/switch/restart.yaml + +text_sensor: + - !include common/text_sensor/version.yaml + - !include common/text_sensor/wifi.yaml + +script: + - id: single_tap + then: + - logger.log: "Single Tap" + - id: double_tap + then: + - logger.log: "Double Tap"