1
0
mirror of https://github.com/esphome/esphome.git synced 2025-04-13 14:20:29 +01:00

pimoroni led switch moved to hub

This commit is contained in:
Anton Viktorov 2024-04-07 21:12:02 +02:00
parent fdec3c7156
commit cd1642dc90
7 changed files with 46 additions and 63 deletions

View File

@ -278,5 +278,12 @@ void BH1745Component::publish_data_() {
}
}
void BH1745Component::set_pimoroni_led_switch(switch_::Switch *led_switch) {
this->led_switch_ = led_switch;
if (this->led_switch_ != nullptr) {
static_cast<BH1745SwitchLed *>(this->led_switch_)->set_bh1745(this);
}
}
} // namespace bh1745
} // namespace esphome

View File

@ -2,6 +2,7 @@
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/switch/switch.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
@ -100,6 +101,7 @@ class BH1745Component : public PollingComponent, public i2c::I2CDevice {
void set_clear_counts_sensor(sensor::Sensor *clear) { this->clear_counts_sensor_ = clear; }
void set_illuminance_sensor(sensor::Sensor *illuminance) { this->illuminance_sensor_ = illuminance; }
void set_color_temperature_sensor(sensor::Sensor *cct) { this->color_temperature_sensor_ = cct; }
void set_pimoroni_led_switch(switch_::Switch *led_switch);
// only for Pimoroni board
void switch_led(bool on_off);
@ -116,6 +118,8 @@ class BH1745Component : public PollingComponent, public i2c::I2CDevice {
sensor::Sensor *illuminance_sensor_{nullptr};
sensor::Sensor *color_temperature_sensor_{nullptr};
switch_::Switch *led_switch_{nullptr};
enum class State : uint8_t {
NOT_INITIALIZED,
INITIAL_SETUP_COMPLETED,
@ -150,5 +154,19 @@ class BH1745Component : public PollingComponent, public i2c::I2CDevice {
void publish_data_();
};
class BH1745SwitchLed : public switch_::Switch, public Component {
public:
void set_bh1745(BH1745Component *bh1745) { this->bh1745_ = bh1745; }
protected:
void write_state(bool state) override {
if (this->bh1745_ != nullptr) {
this->bh1745_->switch_led(state);
publish_state(state);
}
};
BH1745Component *bh1745_;
};
} // namespace bh1745
} // namespace esphome

View File

@ -1,6 +1,6 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.components import i2c, sensor, switch
from esphome.const import (
CONF_ID,
CONF_COLOR_TEMPERATURE,
@ -19,6 +19,7 @@ from esphome.const import (
CODEOWNERS = ["@latonita"]
DEPENDENCIES = ["i2c"]
AUTO_LOAD = ["sensor", "switch"]
CONF_BH1745_ID = "bh1745_id"
@ -29,12 +30,16 @@ CONF_GREEN_CHANNEL = "green_channel"
CONF_BLUE_CHANNEL = "blue_channel"
CONF_CLEAR_CHANNEL = "clear_channel"
CONF_PIMORONI_LED_SWITCH = "pimoroni_led_switch"
bh1745_ns = cg.esphome_ns.namespace("bh1745")
BH1745SComponent = bh1745_ns.class_(
"BH1745Component", cg.PollingComponent, i2c.I2CDevice
)
BH1745SSwitchLed = bh1745_ns.class_("BH1745SwitchLed", switch.Switch, cg.Component)
AdcGain = bh1745_ns.enum("AdcGain")
ADC_GAINS = {
"1X": AdcGain.GAIN_1X,
@ -102,6 +107,16 @@ CONFIG_SCHEMA = cv.All(
),
key=CONF_NAME,
),
cv.Optional(CONF_PIMORONI_LED_SWITCH): cv.maybe_simple_value(
switch.SWITCH_SCHEMA.extend(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(BH1745SSwitchLed),
}
)
),
key=CONF_NAME,
),
}
)
.extend(cv.polling_component_schema("60s"))
@ -136,3 +151,7 @@ async def to_code(config):
if CONF_COLOR_TEMPERATURE in config:
sens = await sensor.new_sensor(config[CONF_COLOR_TEMPERATURE])
cg.add(var.set_color_temperature_sensor(sens))
if CONF_PIMORONI_LED_SWITCH in config:
sw = await switch.new_switch(config[CONF_PIMORONI_LED_SWITCH])
cg.add(var.set_pimoroni_led_switch(sw))

View File

@ -1,20 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import switch
from ..sensor import bh1745_ns, BH1745SComponent, CONF_BH1745_ID
BH1745SwitchLed = bh1745_ns.class_("BH1745SwitchLed", switch.Switch, cg.Component)
CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(BH1745SwitchLed),
cv.GenerateID(CONF_BH1745_ID): cv.use_id(BH1745SComponent),
}
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
var = await switch.new_switch(config)
await cg.register_component(var, config)
hub = await cg.get_variable(config[CONF_BH1745_ID])
cg.add(var.set_bh1745(hub))

View File

@ -1,17 +0,0 @@
#include "bh1745_switch_led.h"
#include "esphome/core/log.h"
namespace esphome {
namespace bh1745 {
static const char *const TAG = "bh1745.led";
void BH1745SwitchLed::write_state(bool state) {
bh1745_->switch_led(state);
publish_state(state);
}
void BH1745SwitchLed::dump_config() { LOG_SWITCH("", "BH1745 Pimoroni LED", this); }
} // namespace bh1745
} // namespace esphome

View File

@ -1,21 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/switch/switch.h"
#include "../bh1745.h"
namespace esphome {
namespace bh1745 {
class BH1745SwitchLed : public switch_::Switch, public Component {
public:
void dump_config() override;
void set_bh1745(BH1745Component *bh1745) { this->bh1745_ = bh1745; }
protected:
void write_state(bool state) override;
BH1745Component *bh1745_;
};
} // namespace bh1745
} // namespace esphome

View File

@ -10,7 +10,4 @@ sensor:
clear_channel: Clear channel
illuminance: Illuminance
color_temperature: Color temperature
switch:
- platform: bh1745
name: BH1754 LED
pimoroni_led_switch: LED Switch