Skip to content

Instantly share code, notes, and snippets.

@kdorff
Last active October 18, 2022 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdorff/046ddc1ad3c481aeb2b18e511e84b289 to your computer and use it in GitHub Desktop.
Save kdorff/046ddc1ad3c481aeb2b18e511e84b289 to your computer and use it in GitHub Desktop.
##
## My latest version targets the ESP32. See the code at
## https://gist.github.com/kdorff/396a11b5bb29142f09c1f52242c546e5
##
## ESPHome configuration for led-and-key1
## * LED0 is lit if the back door autolock is enabled
## * LED1 is lit if the front door autolock is enabled
## * ..
## * BUTTON0 toggles the back door autolock enable
## * BUTTON1 toggles the front door autolock enable
## * ...
## * BUTTON6 will decrease the intensity
## * BUTTON7 will decrease the intensity
##
## The global ledkey1_display controls what is being displayed.
## if (ledkey1_display == "") the current time and temperature
## will be displayed, instead (obtained from HA).
##
## Wiring of the TM1638
## 3.3v and GND from 3.3V and GND from the microcontroller (may not be ideal?)
## DIO on D7, GPIO13
## CLK on D6, GPIO12
## STB on D5, GPIO14
##
esphome:
name: led-and-key-1
esp8266:
board: esp01_1m
# Enable logging
logger:
level: DEBUG
# Enable Home Assistant API
api:
encryption:
key: !secret ledkey1_api_encryption
ota:
password: !secret ota_password
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
fast_connect: true
domain: .mylocal
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "led-and-key-1 Fallback Hotspot"
password: !secret fallback_wifi_password
captive_portal:
##
## Use HA as a source of time.
##
time:
- platform: homeassistant
id: esptime
globals:
##
## The value to be displayed
##
- id: ledkey1_display
type: std::string
restore_value: no
initial_value: ""
##
## The value currently on the display
##
- id: ledkey1_display_current
type: std::string
restore_value: no
initial_value: ""
##
## The intensity of the display (0-7).
##
- id: intensity
type: int
restore_value: yes
initial_value: "1"
##
## Display the value in the global ledkey1_display
## or display the time and temperature if
## (ledkey1_display == "").
##
display:
platform: tm1638
id: ledkey1_display_actual
stb_pin: 14
clk_pin: 12
dio_pin: 13
intensity: 1
update_interval: 0.5s
lambda: |-
it.set_intensity(id(intensity));
if (strlen(id(ledkey1_display).c_str()) == 0) {
//
// Nothing being displayed. Show the time and temp.
//
id(ledkey1_display_current).clear();
it.print(" ");
it.printf("%s %.0fF", id(esptime).now().strftime("%I.%M").c_str(), id(back_yard_temperature).state);
}
else {
if (strcmp(id(ledkey1_display).c_str(), id(ledkey1_display_current).c_str()) != 0) {
//
// New text to display. Show it and remember it
// so we don't have to show it again in the next iteration
// of the lambda.
//
id(ledkey1_display_current).clear();
id(ledkey1_display_current).append(id(ledkey1_display));
// Clear the display and then print.
it.print(" ");
it.print(id(ledkey1_display).c_str());
}
}
sensor:
##
## HA sensors we need to do our work.
##
- platform: homeassistant
id: back_yard_temperature
entity_id: sensor.back_yard_sensor_temperature
binary_sensor:
##
## More HA sensors we need to do our work.
##
- platform: homeassistant
id: back_door_autolock
entity_id: input_boolean.keymaster_back_door_autolock
publish_initial_state: true
on_state:
##
## Observe initial state and state changes within HA.
##
- if:
condition:
- binary_sensor.is_on: back_door_autolock
then:
- logger.log: "LED0 on"
- output.turn_on: ledkey1_led_0
else:
- logger.log: "LED0 off"
- output.turn_off: ledkey1_led_0
- platform: homeassistant
id: front_door_autolock
entity_id: input_boolean.keymaster_front_door_autolock
publish_initial_state: true
on_state:
##
## Observe initial state and state changes within HA.
##
- if:
condition:
- binary_sensor.is_on: front_door_autolock
then:
- logger.log: "LED1 on"
- output.turn_on: ledkey1_led_1
else:
- logger.log: "LED1 off"
- output.turn_off: ledkey1_led_1
##
## The eight buttons on the TM1638.
##
- platform: tm1638
name: "ledkey1 button 0"
id: ledkey1_button_0
key: 0
on_click:
##
## Button 0 is pressed. Toggle back door autolock.
##
- if:
condition:
- binary_sensor.is_on: back_door_autolock
then:
- logger.log: "Back door autolock disabled"
- homeassistant.service:
##
## I thought changing ESPHome's binary_sensor.back_door_autolock
## should have changed the associated input_boolean.keymaster_back_door_autolock
## but it didn't seem to. Calling the HA service to change the value worked.
##
service: "input_boolean.turn_off"
data:
entity_id: "input_boolean.keymaster_back_door_autolock"
- lambda: |-
id(ledkey1_display) = "Back dr";
- delay: 1s
- lambda: |-
id(ledkey1_display) = "Autolock";
- delay: 1s
- lambda: |-
id(ledkey1_display) = "Disabled";
- delay: 1s
- lambda: |-
id(ledkey1_display).clear();
else:
- logger.log: "Back door autolock enabled"
- homeassistant.service:
service: "input_boolean.turn_on"
data:
entity_id: "input_boolean.keymaster_back_door_autolock"
- lambda: |-
id(ledkey1_display) = "Back dr";
- delay: 1s
- lambda: |-
id(ledkey1_display) = "Autolock";
- delay: 1s
- lambda: |-
id(ledkey1_display) = "Enabled";
- delay: 1s
- lambda: |-
id(ledkey1_display).clear();
- platform: tm1638
name: "ledkey1 button 1"
id: ledkey1_button_1
key: 1
on_click:
##
## Button 1 is pressed. Toggle front door autolock.
##
- if:
condition:
- binary_sensor.is_on: front_door_autolock
then:
- logger.log: "Front door autolock disabled"
- homeassistant.service:
service: "input_boolean.turn_off"
data:
entity_id: "input_boolean.keymaster_front_door_autolock"
- lambda: |-
id(ledkey1_display) = "Front dr";
- delay: 1s
- lambda: |-
id(ledkey1_display) = "Autolock";
- delay: 1s
- lambda: |-
id(ledkey1_display) = "Disabled";
- delay: 1s
- lambda: |-
id(ledkey1_display).clear();
else:
- logger.log: "Front door autolock enabled"
- homeassistant.service:
service: "input_boolean.turn_on"
data:
entity_id: "input_boolean.keymaster_front_door_autolock"
- lambda: |-
id(ledkey1_display) = "Front dr";
- delay: 1s
- lambda: |-
id(ledkey1_display) = "Autolock";
- delay: 1s
- lambda: |-
id(ledkey1_display) = "Enabled";
- delay: 1s
- lambda: |-
id(ledkey1_display).clear();
- platform: tm1638
name: "ledkey1 button 2"
id: ledkey1_button_2
key: 2
- platform: tm1638
name: "ledkey1 button 3"
id: ledkey1_button_3
key: 3
- platform: tm1638
name: "ledkey1 button 4"
id: ledkey1_button_4
key: 4
- platform: tm1638
name: "ledkey1 button 5"
id: ledkey1_button_5
key: 5
- platform: tm1638
name: "ledkey1 button 6"
id: ledkey1_button_6
key: 6
on_click:
##
## Button 6: Decrease display intensity
##
- lambda: |-
if (id(intensity) > 0) {
id(intensity)--;
}
id(ledkey1_display) = "Inten ";
id(ledkey1_display).append(std::to_string(id(intensity)));
- delay: 1s
- lambda: |-
id(ledkey1_display).clear();
- platform: tm1638
name: "ledkey1 button 7"
id: ledkey1_button_7
key: 7
on_click:
##
## Button 7: Increase display intensity
##
- lambda: |-
if (id(intensity) < 7) {
id(intensity)++;
}
id(ledkey1_display) = "Inten ";
id(ledkey1_display).append(std::to_string(id(intensity)));
- delay: 1s
- lambda: |-
id(ledkey1_display).clear();
##
## The LEDs. We control these elsewhere, as necessary.
##
output:
- platform: tm1638
# name: "ledkey1 led 0"
id: ledkey1_led_0
led: 0
- platform: tm1638
# name: "ledkey1 led 1"
id: ledkey1_led_1
led: 1
- platform: tm1638
# name: "ledkey1 led 2"
id: ledkey1_led_2
led: 2
- platform: tm1638
# name: "ledkey1 led 3"
id: ledkey1_led_3
led: 3
- platform: tm1638
# name: "ledkey1 led 4"
id: ledkey1_led_4
led: 4
- platform: tm1638
# name: "ledkey1 led 5"
id: ledkey1_led_5
led: 5
- platform: tm1638
# name: "ledkey1 led 6"
id: ledkey1_led_6
led: 6
- platform: tm1638
# name: "ledkey1 led 7"
id: ledkey1_led_7
led: 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment