Skip to content

Commit

Permalink
add dynamic configuration for min_brightness
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoIIT committed Aug 13, 2024
1 parent d714396 commit 3bf45b7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
5 changes: 4 additions & 1 deletion components/ble_adv_controller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ ble_adv_controller:
# variant: variant of the encoding
# For ZhiJia: Can be v0 (MSC16), v1 (MSC26) or v2 (MSC26A), default is v2
# For Fanlamp: Can be any of 'v1a', 'v1b', 'v2' or 'v3', depending on how old your lamp is... Default is 'v3'
# Can be configured dynamically in HA directly, device 'Configuration' section, "Encoding".
variant: v3
# max_duration (default 3000, range 300 -> 10000): the maximum duration in ms during which the command is advertized.
# if a command is received before the 'max_duration' but after the 'duration', it is processed immediately
Expand All @@ -108,6 +109,7 @@ ble_adv_controller:
# if a command is received before the 'duration' it is queued and processed later,
# if there is already a similar command pending, in this case the pending command is removed from the queue
# Increasing this parameter will make the combination of commands slower. See 'Dynamic Configuration'.
# Can be configured dynamically in HA directly, device 'Configuration' section, "Duration".
duration: 200
# reversed: reversing the cold / warm at encoding time, needed for some controllers
# default to false
Expand All @@ -131,6 +133,7 @@ light:
# just setup this value to 0, then test your lamp by decreasing the brightness percent by percent.
# when it switches off, you have the min_brightness to setup here.
# Default to 1%
# Can be configured dynamically in HA directly, device 'Configuration' section, "Min Brightness".
min_brightness: 1%
# constant_brightness (default to false): the natural white is usually brighter than the cold or warm color
# if you setup constant_brightness to true, the natural white will have same brightness than cold and warm ones
Expand Down Expand Up @@ -194,7 +197,7 @@ If this component works, but the cold and warm temperatures are reversed (that i
If the brightness or color temperature does not work for your Zhi Jia v1 or v2 lamp, please setup the `separate_dim_cct` option to true and try again.

### Minimum Brightness
If the minimum brightness is too bright, and you know that your light can go darker - try changing the minimum brightness via the `min_brightness` configuration option (it takes a percentage).
If the minimum brightness is too bright, and you know that your light can go darker - try changing the minimum brightness via the `min_brightness` configuration option (it takes a percentage) or directly via the dynamic configuration in HA `Min Brightness`.

### Saving state on ESP32 reboot
Fan and Light entities are inheriting properties from their ESPHome parent [Fan](https://esphome.io/components/fan/index.html) and [Light](https://esphome.io/components/light/index.html), in particular they implement the `restore_mode` which has default value `ALWAYS_OFF`. Just adding it to your config with value `RESTORE_DEFAULT_OFF` will have the Fan or Light remember its last state (ON/OFF, but also brightness, color temperature and fan speed).
Expand Down
1 change: 1 addition & 0 deletions components/ble_adv_controller/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ async def to_code(config):
cg.add(var.set_constant_brightness(config[CONF_CONSTANT_BRIGHTNESS]))
cg.add(var.set_split_dim_cct(config[CONF_BLE_ADV_SPLIT_DIM_CCT]))
cg.add(var.set_min_brightness(config[CONF_MIN_BRIGHTNESS]))
cg.add(cg.App.register_number(var.get_number_min_brightness()))
14 changes: 12 additions & 2 deletions components/ble_adv_controller/light/ble_adv_light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ float ensure_range(float f) {
return (f > 1.0) ? 1.0 : ( (f < 0.0) ? 0.0 : f );
}

void BleAdvLight::setup() {
// init number for Min Brightness
this->number_min_brightness_.set_id("Min Brightness", this->get_name());
this->number_min_brightness_.set_entity_category(EntityCategory::ENTITY_CATEGORY_CONFIG);
this->number_min_brightness_.traits.set_min_value(0);
this->number_min_brightness_.traits.set_max_value(100);
this->number_min_brightness_.traits.set_step(1);
this->number_min_brightness_.publish_state(this->number_min_brightness_.state);
}

light::LightTraits BleAdvLight::get_traits() {
auto traits = light::LightTraits();

Expand All @@ -27,7 +37,7 @@ void BleAdvLight::dump_config() {
ESP_LOGCONFIG(TAG, " Cold White Temperature: %f mireds", this->cold_white_temperature_);
ESP_LOGCONFIG(TAG, " Warm White Temperature: %f mireds", this->warm_white_temperature_);
ESP_LOGCONFIG(TAG, " Constant Brightness: %s", this->constant_brightness_ ? "true" : "false");
ESP_LOGCONFIG(TAG, " Minimum Brightness: 0x%.2X", this->min_brightness_);
ESP_LOGCONFIG(TAG, " Minimum Brightness: 0x%.2X", this->get_min_brightness());
}

void BleAdvLight::write_state(light::LightState *state) {
Expand All @@ -47,7 +57,7 @@ void BleAdvLight::write_state(light::LightState *state) {
}

// Compute Corrected Brigtness / Warm Color Temperature (potentially reversed) as float: 0 -> 1
float updated_brf = ensure_range(this->min_brightness_ + state->current_values.get_brightness() * (1.f - this->min_brightness_));
float updated_brf = ensure_range(this->get_min_brightness() + state->current_values.get_brightness() * (1.f - this->get_min_brightness()));
float updated_ctf = ensure_range((state->current_values.get_color_temperature() - this->cold_white_temperature_) / (this->warm_white_temperature_ - this->cold_white_temperature_));
updated_ctf = this->get_parent()->is_reversed() ? 1.0 - updated_ctf : updated_ctf;

Expand Down
8 changes: 6 additions & 2 deletions components/ble_adv_controller/light/ble_adv_light.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ namespace bleadvcontroller {
class BleAdvLight : public light::LightOutput, public BleAdvEntity, public EntityBase
{
public:
void setup() override;
void dump_config() override;

void set_cold_white_temperature(float cold_white_temperature) { this->cold_white_temperature_ = cold_white_temperature; }
void set_warm_white_temperature(float warm_white_temperature) { this->warm_white_temperature_ = warm_white_temperature; }
void set_constant_brightness(bool constant_brightness) { this->constant_brightness_ = constant_brightness; }
void set_min_brightness(float min_brightness) { this->min_brightness_ = min_brightness; }
void set_min_brightness(float min_brightness) { this->number_min_brightness_.state = min_brightness * 100; }
void set_split_dim_cct(bool split_dim_cct) { this->split_dim_cct_ = split_dim_cct; }

number::Number * get_number_min_brightness() { return &(this->number_min_brightness_); }
float get_min_brightness() { return ((float)this->number_min_brightness_.state) / 100.0f; }

void setup_state(light::LightState *state) override { this->state_ = state; };
void write_state(light::LightState *state) override;
light::LightTraits get_traits() override;
Expand All @@ -27,7 +31,7 @@ class BleAdvLight : public light::LightOutput, public BleAdvEntity, public Entit
float cold_white_temperature_;
float warm_white_temperature_;
bool constant_brightness_;
float min_brightness_;
BleAdvNumber number_min_brightness_;
bool split_dim_cct_;

bool is_off_{true};
Expand Down

0 comments on commit 3bf45b7

Please sign in to comment.