mmakaay / esphome-xiaomi_bslamp2

ESPHome integration for the Xiaomi Mijia Bedside Lamp v2.
Other
213 stars 51 forks source link

Question: Using power button for HA calls #68

Closed Xitro01 closed 2 years ago

Xitro01 commented 2 years ago

So this time not a bug, also not really a feature request. Just a question if something is possible.

Would it be possible to light the power LED when an entity in HA has a certain state? Also would it be possible to long press the power button when it has this state to change an entity in HA?

I have a cool idea with the lamp, but not sure if it is possible to pull off.

Thanks!

mmakaay commented 2 years ago

The LEDs of the front panel can be controlled individually, so it is possible to give the power button light its own functionality. For example, on my bedroom lamp, I installed a microwave presence sensor, and I power up the power button LED for 3 seconds when the sensor is triggered. This allows me to see the button in the dark.

Long press and a HA call are possible too. That's something I use too. Long press triggers a power off script, so I can turn off everything in the house when going to sleep.

Xitro01 commented 2 years ago

That sounds quite like what I would like to achieve. After a certain time (9PM) I would like to have the power button lid if my "House mode" is "Default". If I long press the button "House mode" should go to "Night mode" (which turns everything off in my home) and that should turn the power LED off.

Do you have some configuration examples for me, to achieve this? Would be great, thanks in advance.

mmakaay commented 2 years ago

Sure thing, I'll share some stuff when I'm behind my pc. I will put my config in a config package on the GitHub repo, so anybody can use it for inspiration.

mmakaay commented 2 years ago

I have just gone through the upgrade procedure for the lamp configuration, so the config is now compatible with the latest releases of ESPHome and the bslamp2 external component. I have not yet created a configuration package yet. However, here's the configuration of my bedroom lamp for some inspiration. Note that I've got some local configuration packages for setting up WiFi for example, but I think you'll find what you need in here.

substitutions:
  name: bedside-lamp-bedroom
  friendly_name: Bedside Lamp Bedroom
  default_transition_length: 500ms

  # For packages/base.yaml
  ip: !secret net_bedside_lamp_bedroom_ip

external_components:
  - source:
      type: git
      url: https://github.com/mmakaay/esphome-xiaomi_bslamp2
      ref: dev
    refresh: 60s

packages:
  my_base: !include packages/base.yaml
  my_fixed_ip: !include packages/fixed_ip.yaml
  bslamp2:
    url: https://github.com/mmakaay/esphome-xiaomi_bslamp2
    ref: dev
    files:
      - packages/core.yaml
    refresh: 0s

logger:
  level: DEBUG

api:
  reboot_timeout: 0s

globals:
  - id: current_brightness
    type: float
    initial_value: "0.0"
  - id: show_power_button
    type: bool
    initial_value: "false"
  - id: light_is_off
    type: bool
    initial_value: "true"

script:
  - id: update_front_panel
    mode: restart
    then:
      - front_panel.turn_off_leds: ALL
      - if:
          condition:
            or:
              - text_sensor.state:
                  id: my_light_mode
                  state: rgb
              - text_sensor.state:
                  id: my_light_mode
                  state: white
          then:
            - globals.set:
                id: light_is_off
                value: !lambda return false;
            - front_panel.turn_on_leds: [ POWER, COLOR ]
            - front_panel.set_level: !lambda return id(current_brightness);
      - if:
          condition:
            - text_sensor.state:
                id: my_light_mode
                state: night
          then:
            - globals.set:
                id: light_is_off
                value: !lambda return false;
      - if:
          condition:
            and:
              - text_sensor.state:
                  id: my_light_mode
                  state: "off"
              - lambda: return !id(light_is_off);
          then:
            - globals.set:
                id: light_is_off
                value: !lambda return true;
            # A fun little animation when the lamp is turned off.
            - front_panel.turn_on_leds: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, COLOR ]
            - delay: 70ms
            - front_panel.turn_off_leds: COLOR
            - delay: 70ms
            - front_panel.turn_off_leds: [ 1, 10 ]
            - delay: 70ms
            - front_panel.turn_off_leds: [ 2, 9 ]
            - delay: 70ms
            - front_panel.turn_off_leds: [ 3, 8 ]
            - delay: 80ms
            - front_panel.turn_off_leds: [ 4, 7 ]
            - delay: 80ms
            - front_panel.turn_off_leds: 6
            - delay: 90ms
            - front_panel.turn_off_leds: 5
            - delay: 90ms
            - front_panel.turn_on_leds: 5
            - delay: 100ms
            - front_panel.turn_off_leds: 5
            - delay: 100ms
            - front_panel.turn_on_leds: 5
            - delay: 100ms
            - front_panel.turn_off_leds: 5    
      - if:
          condition:
            lambda: return id(show_power_button);
          then:
            - front_panel.turn_on_leds: POWER
      - front_panel.update_leds:

# This component controls the LED lights of the lamp.
light:
  - platform: xiaomi_bslamp2
    id: my_light
    name: ${light_name}
    default_transition_length: ${default_transition_length}
    on_brightness:
      then:
        - globals.set:
            id: current_brightness
            value: !lambda return x;
        - script.execute: update_front_panel

    # You can use any effects that you like. These are just examples.
    effects:
      - random:
          name: "Slow Random"
          transition_length: 30s
          update_interval: 30s
      - random:
          name: "Fast Random"
          transition_length: 3s
          update_interval: 3s

    # You can define one or more groups of presets. These presets can
    # be activated using various "preset.activate" action options.
    # The presets can for example be used to mimic the behavior of the
    # original firmware (tapping the color button = go to next preset,
    # holding the color button = switch between RGB and white light mode).
    # These bindings have been setup below, using the binary_sensor for
    # the color button.
    presets:
      rgb:
        red:         { red: 100%, green: 0%,   blue: 0%   }
        green:       { red: 0%,   green: 100%, blue: 0%   }
        blue:        { red: 0%,   green: 0%,   blue: 100% }
        yellow:      { red: 100%, green: 100%, blue: 0%   }
        purple:      { red: 100%, green: 0%,   blue: 100% }
        randomize:   { effect: Fast Random                }
      white:
        cold:        { color_temperature: 153 mireds      }
        chilly:      { color_temperature: 275 mireds      }
        luke:        { color_temperature: 400 mireds      }
        warm:        { color_temperature: 588 mireds      }

# This text sensor propagates the currently active light mode.
# The possible light modes are: "off", "rgb", "white" and "night".
# By setting the name, the text_sensor will show up as an entity
# for the lamp in Home Assistant.
text_sensor:
  - platform: xiaomi_bslamp2
    name: ${light_mode_text_sensor_name}
    id: my_light_mode

# Binary sensors can be created for handling front panel touch / release
# events. To specify what part of the front panel to look at, the "for"
# parameter can be set to: "POWER_BUTTON", "COLOR_BUTTON" or "SLIDER".
binary_sensor:
  # When tapping the power button, toggle the light.
  # When holding the power button, power down the house.
  - platform: xiaomi_bslamp2
    id: my_power_button
    for: POWER_BUTTON
    on_multi_click:
    - timing:
        - ON for at most 1.0s
      then:
        - light.toggle: my_light
    - timing:
        - ON for at least 2.0s
      then:
        - light.turn_on:
            id: my_light
            brightness: 1%
            red: 100%
            green: 0%
            blue: 0%
        - homeassistant.service:
            service: script.power_down_house

  # When tapping the color button, acivate the next preset.
  # When holding the color button, activate the next preset group.
  - platform: xiaomi_bslamp2
    id: my_color_button
    for: COLOR_BUTTON
    on_multi_click:
      - timing:
          - ON for at most 0.6s
        then:
          - preset.activate:
              next: preset
      - timing:
          - ON for at least 0.6s
        then:
          - preset.activate:
              next: group

  - platform: gpio
    id: microwave_sensor
    name: ${friendly_name} Presence Sensor
    pin: GPIO2
    filters:
      - delayed_off: 3s
    on_state:
      then:
        - globals.set:
            id: show_power_button
            value: !lambda return x;
        - script.execute: update_front_panel

# This sensor component publishes touch events for the front panel slider.
# The published value represents the level at which the slider was touched.
# By default, values range from 0.01 to 1.00 (in 20 steps). This range can
# be modified using the "range_from" and "range_to" parameters.
sensor:
  # When the slider is touched, update the brightness.
  # Brightness 0.01 initiates the light night mode, which has already
  # been handled above (by holding the power button). Therefore, brightness
  # starts from 0.02 here, to not trigger night mode using the slider.
  - platform: xiaomi_bslamp2
    id: my_slider_level
    range_from: 0.02
    on_value:
      then:
        - light.turn_on:
            id: my_light
            brightness: !lambda return x;
mmakaay commented 2 years ago

Can this issue be closed @Xitro01 ? Or do you need more help with this?

Xitro01 commented 2 years ago

@mmakaay Sure, you may close the issue. Haven't been able to get this to work yet, but don't have a lot of time to fiddle around. I also found a completely other route to achieve what I would like.

mmakaay commented 2 years ago

Okay, feel free to come back to this issue if and when you get back to this idea.