From 93023e8bc5217d54a2466a57016d43f5b9c9c82e Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Thu, 5 Aug 2021 15:33:17 +0200 Subject: [PATCH 1/2] Implemented compatibility with color mode support in the latest Home Assistant + ESPHome. --- CHANGELOG.md | 7 +++++++ README.md | 2 +- components/xiaomi_bslamp2/light/color_rgb_light.h | 2 +- components/xiaomi_bslamp2/light/color_white_light.h | 2 +- components/xiaomi_bslamp2/light/light_output.h | 6 +----- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 962d8fe..f18f1eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.2.0-RC1] + +**Note**: This release requires ESPHome v1.21.0 and Home Assistant 2021.8.0 or newer. + +### Changed +- The code has been made compatible with the new color mode support in Home Assistant. + ## [1.1.0] **Note**: This release requires ESPHome v1.20.0 or newer. diff --git a/README.md b/README.md index 448b147..028dbce 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ aspect of the lamp and to integrate the lamp in your Home Assistant setup. For those who have experience with flashing ESPHome onto devices: -* Make sure you are using ESPHome v1.20.0 or newer. +* Make sure you are using ESPHome v1.21.0 or newer. * Copy [`example.yaml`](example.yaml) to `/.yaml`. * Modify the configuration to your needs (see the [configuration guide](doc/configuration.md)). * Compile the `firmware.bin` file and download it to the device to which you have connected your diff --git a/components/xiaomi_bslamp2/light/color_rgb_light.h b/components/xiaomi_bslamp2/light/color_rgb_light.h index 1a6e880..b608921 100644 --- a/components/xiaomi_bslamp2/light/color_rgb_light.h +++ b/components/xiaomi_bslamp2/light/color_rgb_light.h @@ -252,7 +252,7 @@ class ColorRGBLight : public GPIOOutputs { bool set_light_color_values(light::LightColorValues v) { light_mode = LIGHT_MODE_RGB; - if (v.get_white() > 0.0f) { + if (v.get_color_mode() != light::ColorMode::RGB) { return false; } diff --git a/components/xiaomi_bslamp2/light/color_white_light.h b/components/xiaomi_bslamp2/light/color_white_light.h index ffdbb61..fbf62ca 100644 --- a/components/xiaomi_bslamp2/light/color_white_light.h +++ b/components/xiaomi_bslamp2/light/color_white_light.h @@ -80,7 +80,7 @@ class ColorWhiteLight : public GPIOOutputs { bool set_light_color_values(light::LightColorValues v) { light_mode = LIGHT_MODE_WHITE; - if (v.get_white() == 0.0f) { + if (v.get_color_mode() != light::ColorMode::COLOR_TEMPERATURE) { return false; } diff --git a/components/xiaomi_bslamp2/light/light_output.h b/components/xiaomi_bslamp2/light/light_output.h index 0b53da0..3a8c37b 100644 --- a/components/xiaomi_bslamp2/light/light_output.h +++ b/components/xiaomi_bslamp2/light/light_output.h @@ -28,11 +28,7 @@ class XiaomiBslamp2LightOutput : public Component, public light::LightOutput { */ light::LightTraits get_traits() override { auto traits = light::LightTraits(); - traits.set_supports_rgb(true); - traits.set_supports_color_temperature(true); - traits.set_supports_brightness(true); - traits.set_supports_rgb_white_value(false); - traits.set_supports_color_interlock(true); + traits.set_supported_color_modes({light::ColorMode::RGB, light::ColorMode::COLOR_TEMPERATURE}); traits.set_min_mireds(MIRED_MIN); traits.set_max_mireds(MIRED_MAX); return traits; From 8e16c668b9d88020b8eee5783f96cde298c159ef Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Tue, 10 Aug 2021 14:44:47 +0200 Subject: [PATCH 2/2] Made the code compile with both v1.20.* and v.1.21.0. --- CHANGELOG.md | 3 +++ components/xiaomi_bslamp2/common.h | 2 ++ components/xiaomi_bslamp2/light/__init__.py | 2 ++ components/xiaomi_bslamp2/light/color_rgb_light.h | 6 ++++++ components/xiaomi_bslamp2/light/color_white_light.h | 6 ++++++ components/xiaomi_bslamp2/light/light_output.h | 8 ++++++++ 6 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f18f1eb..2e9a3e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [1.2.0-RC1] **Note**: This release requires ESPHome v1.21.0 and Home Assistant 2021.8.0 or newer. +The code will compile with ESPHome v1.20.0, but the lamp will not be controllable +through the Home Assistant GUI when using Home Assistant 2021.8.0 or newer. +Only turn on/off and brightness will be available. ### Changed - The code has been made compatible with the new color mode support in Home Assistant. diff --git a/components/xiaomi_bslamp2/common.h b/components/xiaomi_bslamp2/common.h index 3952317..914e8c7 100644 --- a/components/xiaomi_bslamp2/common.h +++ b/components/xiaomi_bslamp2/common.h @@ -1,5 +1,7 @@ #pragma once +#include "esphome/core/defines.h" + namespace esphome { namespace xiaomi { namespace bslamp2 { diff --git a/components/xiaomi_bslamp2/light/__init__.py b/components/xiaomi_bslamp2/light/__init__.py index 327bf16..11a74f1 100644 --- a/components/xiaomi_bslamp2/light/__init__.py +++ b/components/xiaomi_bslamp2/light/__init__.py @@ -213,6 +213,8 @@ def light_output_to_code(config): yield light.register_light(light_output_var, config) light_hal_var = yield cg.get_variable(config[CONF_LIGHT_HAL_ID]) cg.add(light_output_var.set_parent(light_hal_var)) + if hasattr(light, "types") and hasattr(light.types, "COLOR_MODES"): + cg.add_define('HAS_COLOR_MODES') @coroutine def on_brightness_to_code(config): diff --git a/components/xiaomi_bslamp2/light/color_rgb_light.h b/components/xiaomi_bslamp2/light/color_rgb_light.h index b608921..757d048 100644 --- a/components/xiaomi_bslamp2/light/color_rgb_light.h +++ b/components/xiaomi_bslamp2/light/color_rgb_light.h @@ -252,9 +252,15 @@ class ColorRGBLight : public GPIOOutputs { bool set_light_color_values(light::LightColorValues v) { light_mode = LIGHT_MODE_RGB; +#ifdef HAS_COLOR_MODES if (v.get_color_mode() != light::ColorMode::RGB) { return false; } +#else + if (v.get_white() > 0.0f) { + return false; + } +#endif // Determine the ring level for the color. This is a value between 0 // and 7, determining in what ring of the RGB circle the requested diff --git a/components/xiaomi_bslamp2/light/color_white_light.h b/components/xiaomi_bslamp2/light/color_white_light.h index fbf62ca..365339c 100644 --- a/components/xiaomi_bslamp2/light/color_white_light.h +++ b/components/xiaomi_bslamp2/light/color_white_light.h @@ -80,9 +80,15 @@ class ColorWhiteLight : public GPIOOutputs { bool set_light_color_values(light::LightColorValues v) { light_mode = LIGHT_MODE_WHITE; +#ifdef HAS_COLOR_MODES if (v.get_color_mode() != light::ColorMode::COLOR_TEMPERATURE) { return false; } +#else + if (v.get_white() == 0.0f) { + return false; + } +#endif auto temperature = clamp_temperature_(v.get_color_temperature()); auto brightness = clamp_brightness_(v.get_brightness()); diff --git a/components/xiaomi_bslamp2/light/light_output.h b/components/xiaomi_bslamp2/light/light_output.h index 3a8c37b..4a399c8 100644 --- a/components/xiaomi_bslamp2/light/light_output.h +++ b/components/xiaomi_bslamp2/light/light_output.h @@ -28,7 +28,15 @@ class XiaomiBslamp2LightOutput : public Component, public light::LightOutput { */ light::LightTraits get_traits() override { auto traits = light::LightTraits(); +#ifdef HAS_COLOR_MODES traits.set_supported_color_modes({light::ColorMode::RGB, light::ColorMode::COLOR_TEMPERATURE}); +#else + traits.set_supports_rgb(true); + traits.set_supports_color_temperature(true); + traits.set_supports_brightness(true); + traits.set_supports_rgb_white_value(false); + traits.set_supports_color_interlock(true); +#endif traits.set_min_mireds(MIRED_MIN); traits.set_max_mireds(MIRED_MAX); return traits;