ESPHome 2025.6.3
Loading...
Searching...
No Matches
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
36#include "esphome/core/log.h"
41
42namespace esphome {
43namespace sonoff_d1 {
44
46 public:
47 // LightOutput methods
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};
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
Interface to write LightStates to hardware.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:63
This class is used to represent the capabilities of a light.
bool read_command_(uint8_t *cmd, size_t &len)
Definition sonoff_d1.cpp:80
void set_max_value(const uint8_t max_value)
Definition sonoff_d1.h:61
void process_command_(const uint8_t *cmd, size_t len)
uint8_t calc_checksum_(const uint8_t *cmd, size_t len)
Definition sonoff_d1.cpp:51
bool control_dimmer_(bool binary, uint8_t brightness)
void populate_checksum_(uint8_t *cmd, size_t len)
Definition sonoff_d1.cpp:59
void set_use_rm433_remote(const bool use_rm433_remote)
Definition sonoff_d1.h:59
void set_min_value(const uint8_t min_value)
Definition sonoff_d1.h:60
light::LightState * light_state_
Definition sonoff_d1.h:71
void write_state(light::LightState *state) override
void publish_state_(bool is_on, uint8_t brightness)
float get_setup_priority() const override
Definition sonoff_d1.h:56
void setup_state(light::LightState *state) override
Definition sonoff_d1.h:49
bool read_ack_(const uint8_t *cmd, size_t len)
light::LightTraits get_traits() override
bool write_command_(uint8_t *cmd, size_t len, bool needs_ack=true)
bool state
Definition fan.h:0
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:302