добавляет HALightBrightness
This commit is contained in:
@@ -10,18 +10,72 @@ IRLight::IRLight(IRsend *irsend) {
|
||||
state = false;
|
||||
}
|
||||
|
||||
void IRLight::setState(bool state) {
|
||||
if (state)
|
||||
on();
|
||||
else
|
||||
off();
|
||||
}
|
||||
|
||||
void IRLight::on() {
|
||||
state = true;
|
||||
irsend->sendRaw(rawDataOn, 67, 38);
|
||||
irsend->sendRaw(RDOn, 67, 38);
|
||||
}
|
||||
|
||||
void IRLight::off() {
|
||||
state = false;
|
||||
irsend->sendRaw(rawDataOff, 67, 38);
|
||||
irsend->sendRaw(RDOff, 67, 38);
|
||||
}
|
||||
|
||||
void IRLight::begin() { irsend->begin(); }
|
||||
void IRLight::setBrightness(int newBrightness) {
|
||||
if (newBrightness > brightness) {
|
||||
for (int i = 0; i < newBrightness - brightness; i++) {
|
||||
brightnessUp();
|
||||
delay(250);
|
||||
}
|
||||
} else if (newBrightness < brightness) {
|
||||
for (int i = 0; i < brightness - newBrightness; i++) {
|
||||
brightnessDown();
|
||||
delay(250);
|
||||
}
|
||||
}
|
||||
brightness = newBrightness;
|
||||
}
|
||||
|
||||
bool IRLight::getState() { return state; }
|
||||
void IRLight::brightnessUp() {
|
||||
if (brightness < getBrightnessScale()) {
|
||||
brightness++;
|
||||
irsend->sendRaw(RDBrightnessUp, 67, 38);
|
||||
}
|
||||
}
|
||||
|
||||
IRLight::~IRLight() { delete irsend; }
|
||||
void IRLight::brightnessDown() {
|
||||
if (brightness > 0) {
|
||||
brightness--;
|
||||
irsend->sendRaw(RDBrightnessDown, 67, 38);
|
||||
}
|
||||
}
|
||||
|
||||
void IRLight::begin() {
|
||||
irsend->begin();
|
||||
irsend->sendRaw(RDCentralButton, 67, 38);
|
||||
brightness = 14;
|
||||
delay(250);
|
||||
off();
|
||||
}
|
||||
|
||||
int IRLight::getBrightnessScale() {
|
||||
return 14;
|
||||
}
|
||||
|
||||
int IRLight::getBrightness() {
|
||||
return brightness;
|
||||
}
|
||||
|
||||
bool IRLight::getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
IRLight::~IRLight() {
|
||||
delete irsend;
|
||||
}
|
@@ -12,6 +12,12 @@ void IRLightController::setState(bool state) {
|
||||
}
|
||||
}
|
||||
|
||||
bool IRLightController::getState() { return irlight.getState(); }
|
||||
bool IRLightController::getState() {
|
||||
return irlight.getState();
|
||||
}
|
||||
|
||||
void IRLightController::setBrightness(int value) {
|
||||
|
||||
}
|
||||
|
||||
IRLightController::~IRLightController() {}
|
@@ -3,7 +3,7 @@
|
||||
#include <HomeAssistantDevices.hpp>
|
||||
#include <IRLight.h>
|
||||
|
||||
class IRLightController : public HALightController {
|
||||
class IRLightController : public HALightBrightnessController {
|
||||
private:
|
||||
IRLight &irlight;
|
||||
|
||||
@@ -12,5 +12,10 @@ public:
|
||||
void begin();
|
||||
void setState(bool state);
|
||||
bool getState();
|
||||
|
||||
void setBrightness(int value);
|
||||
int getBrightness();
|
||||
int getBrightnessScale();
|
||||
|
||||
~IRLightController();
|
||||
};
|
61
src/main.cpp
61
src/main.cpp
@@ -17,8 +17,7 @@ DHT dht(DHTPIN, DHTTYPE);
|
||||
|
||||
IRLight irlight(4);
|
||||
|
||||
IRLightController light(irlight);
|
||||
HALight halight(client, "light", "l-1", light);
|
||||
HALightBrightness halight(client, "light", "l-1", irlight);
|
||||
|
||||
void setup_wifi();
|
||||
void reconnect();
|
||||
@@ -45,11 +44,19 @@ void setup() {
|
||||
}
|
||||
|
||||
dht.begin();
|
||||
light.begin();
|
||||
irlight.begin();
|
||||
|
||||
client.subscribe("homeassistant/status");
|
||||
halight.subscribeToCommandTopic();
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
mqtt_publish_config();
|
||||
delay(100);
|
||||
// mqtt_publish_state();
|
||||
halight.sendState();
|
||||
}
|
||||
|
||||
/*
|
||||
mqtt_publish_config();
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
@@ -59,6 +66,7 @@ void setup() {
|
||||
mqtt_publish_state();
|
||||
halight.sendState();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@@ -67,15 +75,17 @@ void loop() {
|
||||
}
|
||||
client.loop();
|
||||
|
||||
/*
|
||||
long now = millis();
|
||||
|
||||
if (now - lastMsg > 60000) {
|
||||
lastMsg = now;
|
||||
temp = round2(dht.readTemperature());
|
||||
|
||||
hum = round2(dht.readHumidity());
|
||||
mqtt_publish_state();
|
||||
}
|
||||
|
||||
*/
|
||||
delay(100);
|
||||
}
|
||||
|
||||
@@ -93,7 +103,7 @@ void mqtt_callback(char *topic, byte *payload, unsigned int length) {
|
||||
mqtt_publish_config();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
halight.handle(topic, payload, length);
|
||||
}
|
||||
|
||||
@@ -137,51 +147,10 @@ void reconnect() {
|
||||
}
|
||||
|
||||
void mqtt_publish_config() {
|
||||
/*
|
||||
StaticJsonDocument<256> doc;
|
||||
char buffer[256];
|
||||
|
||||
doc["device_class"] = "temperature";
|
||||
doc["name"] = "Temperature";
|
||||
doc["unit_of_meas"] = "°C";
|
||||
doc["stat_t"] = HOMEASSISTANT_TOPIC "/state";
|
||||
doc["value_template"] = "{{value_json.temperature}}";
|
||||
doc["unique_id"] = "t";
|
||||
|
||||
serializeJson(doc, buffer);
|
||||
|
||||
client.publish(HOMEASSISTANT_TOPIC "/t/config", buffer, true);
|
||||
|
||||
doc["device_class"] = "humidity";
|
||||
doc["name"] = "Humidity";
|
||||
doc["unit_of_meas"] = "%";
|
||||
doc["stat_t"] = HOMEASSISTANT_TOPIC "/state";
|
||||
doc["value_template"] = "{{value_json.humidity}}";
|
||||
doc["unique_id"] = "h";
|
||||
|
||||
serializeJson(doc, buffer);
|
||||
|
||||
client.publish(HOMEASSISTANT_TOPIC "/h/config", buffer, true);
|
||||
*/
|
||||
halight.sendConfig();
|
||||
}
|
||||
|
||||
void mqtt_publish_state() {
|
||||
/*
|
||||
Serial.println("===== Sending Data =====");
|
||||
StaticJsonDocument<256> doc;
|
||||
char buffer[256];
|
||||
|
||||
doc["temperature"] = round2(temp);
|
||||
doc["humidity"] = round2(hum);
|
||||
|
||||
Serial.printf("Temperature: %f\n", round2(temp));
|
||||
Serial.printf("Humidity: %f\n", round2(hum));
|
||||
|
||||
serializeJson(doc, buffer);
|
||||
|
||||
client.publish(HOMEASSISTANT_TOPIC "/state", buffer, true);
|
||||
*/
|
||||
// Loop until we're reconnected
|
||||
while (!client.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
|
Reference in New Issue
Block a user