ESPHome  2024.3.1
sonoff_d1.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*
4  sonoff_d1.h - Sonoff D1 Dimmer support for ESPHome
5 
6  Copyright © 2021 Anatoly Savchenkov
7  Copyright © 2020 Jeff Rescignano
8 
9  Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10  and associated documentation files (the “Software”), to deal in the Software without
11  restriction, including without limitation the rights to use, copy, modify, merge, publish,
12  distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
13  the Software is furnished to do so, subject to the following conditions:
14 
15  The above copyright notice and this permission notice shall be included in all copies or
16  substantial portions of the Software.
17 
18  THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
19  BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24  -----
25 
26  If modifying this file, in addition to the license above, please ensure to include links back to the original code:
27  https://jeffresc.dev/blog/2020-10-10
28  https://github.com/JeffResc/Sonoff-D1-Dimmer
29  https://github.com/arendst/Tasmota/blob/2d4a6a29ebc7153dbe2717e3615574ac1c84ba1d/tasmota/xdrv_37_sonoff_d1.ino#L119-L131
30 
31  -----
32 */
33 
34 #include "esphome/core/log.h"
35 #include "esphome/core/helpers.h"
36 #include "esphome/core/component.h"
41 
42 namespace esphome {
43 namespace sonoff_d1 {
44 
46  public:
47  // LightOutput methods
48  light::LightTraits get_traits() override;
49  void setup_state(light::LightState *state) override { this->light_state_ = state; }
50  void write_state(light::LightState *state) override;
51 
52  // Component methods
53  void setup() override{};
54  void loop() override;
55  void dump_config() override;
56  float get_setup_priority() const override { return esphome::setup_priority::DATA; }
57 
58  // Custom methods
59  void set_use_rm433_remote(const bool use_rm433_remote) { this->use_rm433_remote_ = use_rm433_remote; }
60  void set_min_value(const uint8_t min_value) { this->min_value_ = min_value; }
61  void set_max_value(const uint8_t max_value) { this->max_value_ = max_value; }
62 
63  protected:
64  uint8_t min_value_{0};
65  uint8_t max_value_{100};
66  bool use_rm433_remote_{false};
67  bool last_binary_{false};
68  uint8_t last_brightness_{0};
69  int write_count_{0};
70  int read_count_{0};
72 
73  uint8_t calc_checksum_(const uint8_t *cmd, size_t len);
74  void populate_checksum_(uint8_t *cmd, size_t len);
75  void skip_command_();
76  bool read_command_(uint8_t *cmd, size_t &len);
77  bool read_ack_(const uint8_t *cmd, size_t len);
78  bool write_command_(uint8_t *cmd, size_t len, bool needs_ack = true);
79  bool control_dimmer_(bool binary, uint8_t brightness);
80  void process_command_(const uint8_t *cmd, size_t len);
81  void publish_state_(bool is_on, uint8_t brightness);
82 };
83 
84 } // namespace sonoff_d1
85 } // namespace esphome
bool control_dimmer_(bool binary, uint8_t brightness)
Definition: sonoff_d1.cpp:193
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
Interface to write LightStates to hardware.
Definition: light_output.h:12
bool read_command_(uint8_t *cmd, size_t &len)
Definition: sonoff_d1.cpp:80
uint8_t calc_checksum_(const uint8_t *cmd, size_t len)
Definition: sonoff_d1.cpp:51
void publish_state_(bool is_on, uint8_t brightness)
Definition: sonoff_d1.cpp:233
void set_min_value(const uint8_t min_value)
Definition: sonoff_d1.h:60
void process_command_(const uint8_t *cmd, size_t len)
Definition: sonoff_d1.cpp:206
void set_max_value(const uint8_t max_value)
Definition: sonoff_d1.h:61
bool write_command_(uint8_t *cmd, size_t len, bool needs_ack=true)
Definition: sonoff_d1.cpp:154
light::LightTraits get_traits() override
Definition: sonoff_d1.cpp:251
This class is used to represent the capabilities of a light.
Definition: light_traits.h:11
bool read_ack_(const uint8_t *cmd, size_t len)
Definition: sonoff_d1.cpp:127
float get_setup_priority() const override
Definition: sonoff_d1.h:56
void populate_checksum_(uint8_t *cmd, size_t len)
Definition: sonoff_d1.cpp:59
std::string size_t len
Definition: helpers.h:292
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
light::LightState * light_state_
Definition: sonoff_d1.h:71
void set_use_rm433_remote(const bool use_rm433_remote)
Definition: sonoff_d1.h:59
void setup_state(light::LightState *state) override
Definition: sonoff_d1.h:49
void write_state(light::LightState *state) override
Definition: sonoff_d1.cpp:257
stm32_cmd_t * cmd
Definition: stm32flash.h:96
bool state
Definition: fan.h:34