Browse Source

Compatiblity new coroutine (#36)

Compatiblity fix for new coroutine setup in ESPHome.
pull/44/head
Maurice Makaay 3 years ago
committed by GitHub
parent
commit
239b9f9934
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 29 deletions
  1. +20
    -0
      CHANGELOG.md
  2. +23
    -28
      components/xiaomi_bslamp2/__init__.py
  3. +17
    -1
      example.yaml

+ 20
- 0
CHANGELOG.md View File

@ -4,6 +4,26 @@ 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/), 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). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Not yet released]
### Added
- Implemented support for visual feedback during the OTA flashing process in the
`example.yaml` file: the light becomes blue during the process, the brightness bar
represents the update progress, when updating fails the light flashes red and when it
completes successfuly, the light flashes green.
## [1.1.0]
### Changed
- Made it possible to use lambdas with the `preset.activate` automation. This makes it
possible to link the action to an api service, which exposes the preset functionality
to Home Assistant. The `example.yaml` has been updated with an example for this.
- Fixed a rounding error in the slider sensor component. When using custom "range from" / "range to"
settings, the maximum value could exceed the "range to" value due to rounding errors.
Thanks to Jos for the heads up!
- Made the codebase compatible with ESPHome v1.19.0
([PR #1657: Introduce new async-def coroutine syntax](https://github.com/esphome/esphome/pull/1657))
## [1.0.0] ## [1.0.0]
**Note**: This release requires ESPHome v1.18.0 or newer. **Note**: This release requires ESPHome v1.18.0 or newer.


+ 23
- 28
components/xiaomi_bslamp2/__init__.py View File

@ -63,35 +63,31 @@ CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend({
), ),
}) })
@coroutine
def make_gpio(number, mode="OUTPUT"):
yield from cg.gpio_pin_expression({ "number": number, "mode": mode });
async def make_gpio(number, mode="OUTPUT"):
return await cg.gpio_pin_expression({ "number": number, "mode": mode });
@coroutine
def make_gpio_binary_output(id_, number):
gpio_var = yield make_gpio(number)
async def make_gpio_binary_output(id_, number):
gpio_var = await make_gpio(number)
output_var = cg.new_Pvariable(id_) output_var = cg.new_Pvariable(id_)
cg.add(output_var.set_pin(gpio_var)) cg.add(output_var.set_pin(gpio_var))
yield from cg.register_component(output_var, {})
return await cg.register_component(output_var, {})
@coroutine
def make_ledc_output(id_, number, frequency, channel):
gpio_var = yield make_gpio(number)
async def make_ledc_output(id_, number, frequency, channel):
gpio_var = await make_gpio(number)
ledc_var = cg.new_Pvariable(id_, gpio_var) ledc_var = cg.new_Pvariable(id_, gpio_var)
cg.add(ledc_var.set_frequency(frequency)); cg.add(ledc_var.set_frequency(frequency));
cg.add(ledc_var.set_channel(channel)); cg.add(ledc_var.set_channel(channel));
yield from cg.register_component(ledc_var, {})
return await cg.register_component(ledc_var, {})
@coroutine
def make_light_hal(config):
r_var = yield make_ledc_output(config[CONF_RED_ID], config[CONF_RED], 3000, 0)
g_var = yield make_ledc_output(config[CONF_GREEN_ID], config[CONF_GREEN], 3000, 1)
b_var = yield make_ledc_output(config[CONF_BLUE_ID], config[CONF_BLUE], 3000, 2)
w_var = yield make_ledc_output(config[CONF_WHITE_ID], config[CONF_WHITE], 10000, 4)
m1_var = yield make_gpio_binary_output(config[CONF_MASTER1_ID], config[CONF_MASTER1])
m2_var = yield make_gpio_binary_output(config[CONF_MASTER2_ID], config[CONF_MASTER2])
async def make_light_hal(config):
r_var = await make_ledc_output(config[CONF_RED_ID], config[CONF_RED], 3000, 0)
g_var = await make_ledc_output(config[CONF_GREEN_ID], config[CONF_GREEN], 3000, 1)
b_var = await make_ledc_output(config[CONF_BLUE_ID], config[CONF_BLUE], 3000, 2)
w_var = await make_ledc_output(config[CONF_WHITE_ID], config[CONF_WHITE], 10000, 4)
m1_var = await make_gpio_binary_output(config[CONF_MASTER1_ID], config[CONF_MASTER1])
m2_var = await make_gpio_binary_output(config[CONF_MASTER2_ID], config[CONF_MASTER2])
light_hal = cg.new_Pvariable(config[CONF_LIGHT_HAL_ID]) light_hal = cg.new_Pvariable(config[CONF_LIGHT_HAL_ID])
yield cg.register_component(light_hal, config)
await cg.register_component(light_hal, config)
cg.add(light_hal.set_red_pin(r_var)) cg.add(light_hal.set_red_pin(r_var))
cg.add(light_hal.set_green_pin(g_var)) cg.add(light_hal.set_green_pin(g_var))
cg.add(light_hal.set_blue_pin(b_var)) cg.add(light_hal.set_blue_pin(b_var))
@ -99,29 +95,28 @@ def make_light_hal(config):
cg.add(light_hal.set_master1_pin(m1_var)) cg.add(light_hal.set_master1_pin(m1_var))
cg.add(light_hal.set_master2_pin(m2_var)) cg.add(light_hal.set_master2_pin(m2_var))
@coroutine
def make_front_panel_hal(config):
trigger_pin = yield make_gpio(config[CONF_TRIGGER_PIN], "INPUT")
async def make_front_panel_hal(config):
trigger_pin = await make_gpio(config[CONF_TRIGGER_PIN], "INPUT")
fp_hal = cg.new_Pvariable(config[CONF_FRONT_PANEL_HAL_ID]) fp_hal = cg.new_Pvariable(config[CONF_FRONT_PANEL_HAL_ID])
yield cg.register_component(fp_hal, config)
await cg.register_component(fp_hal, config)
cg.add(fp_hal.set_trigger_pin(trigger_pin)) cg.add(fp_hal.set_trigger_pin(trigger_pin))
# The i2c component automatically sets up one I2C bus. # The i2c component automatically sets up one I2C bus.
# Take that bus and update is to make it work for the # Take that bus and update is to make it work for the
# front panel I2C communication. # front panel I2C communication.
fp_i2c_var = yield cg.get_variable(config[CONF_FP_I2C_ID])
fp_i2c_var = await cg.get_variable(config[CONF_FP_I2C_ID])
cg.add(fp_i2c_var.set_sda_pin(config[CONF_SDA])) cg.add(fp_i2c_var.set_sda_pin(config[CONF_SDA]))
cg.add(fp_i2c_var.set_scl_pin(config[CONF_SCL])) cg.add(fp_i2c_var.set_scl_pin(config[CONF_SCL]))
cg.add(fp_i2c_var.set_scan(True)) cg.add(fp_i2c_var.set_scan(True))
cg.add(fp_hal.set_i2c_parent(fp_i2c_var)) cg.add(fp_hal.set_i2c_parent(fp_i2c_var))
cg.add(fp_hal.set_i2c_address(config[CONF_ADDRESS])) cg.add(fp_hal.set_i2c_address(config[CONF_ADDRESS]))
def to_code(config):
async def to_code(config):
# Dirty little hack to make the ESPHome component loader include # Dirty little hack to make the ESPHome component loader include
# the code for the "gpio" platform for the "output" domain. # the code for the "gpio" platform for the "output" domain.
# Loading specific platform components is not possible using # Loading specific platform components is not possible using
# the AUTO_LOAD feature unfortunately. # the AUTO_LOAD feature unfortunately.
CORE.config["output"].append({ CONF_PLATFORM: "gpio" }) CORE.config["output"].append({ CONF_PLATFORM: "gpio" })
yield make_light_hal(config)
yield make_front_panel_hal(config)
await make_light_hal(config)
await make_front_panel_hal(config)

+ 17
- 1
example.yaml View File

@ -39,6 +39,22 @@ api:
# flicker. # flicker.
reboot_timeout: 0s reboot_timeout: 0s
api:
password: !secret api_password
# If you want to use light presets (see below) from Home Assistant,
# then you can expose the required functionality as a service here.
# This is an example of how you could expose the activation of a preset.
services:
- service: activate_preset
variables:
my_group: string
my_preset: string
then:
- preset.activate:
group: !lambda 'return my_group;'
preset: !lambda 'return my_preset;'
ota: ota:
password: "Password-For-Flashing-This-Device-Over-The-Air" password: "Password-For-Flashing-This-Device-Over-The-Air"
@ -78,7 +94,7 @@ esphome:
light: light:
- platform: xiaomi_bslamp2 - platform: xiaomi_bslamp2
id: ${id_light} id: ${id_light}
name: ${friendly_name} RGBW Light
name: ${friendly_name} RGBWW Light
default_transition_length: ${transition_length} default_transition_length: ${transition_length}
# When the brightness is changed, then update the level indicator # When the brightness is changed, then update the level indicator
# on the front panel accordingly. In night light mode, turn off # on the front panel accordingly. In night light mode, turn off


Loading…
Cancel
Save