Skip to content

Instantly share code, notes, and snippets.

@wivaku
Last active August 17, 2023 09:41
Show Gist options
  • Save wivaku/1934d89d2efe71ab83b4acb40901f5b1 to your computer and use it in GitHub Desktop.
Save wivaku/1934d89d2efe71ab83b4acb40901f5b1 to your computer and use it in GitHub Desktop.
# ESPHome smart doorbell Home Assistant integration
#
# https://gist.github.com/wivaku/1934d89d2efe71ab83b4acb40901f5b1
#
# derived from:
# https://github.com/zuidwijk/esphome-doorbell/blob/main/smartdoorbell.yaml
#
# changed:
# - use substitution variables to set name of device and expose version of script
# - fallback to regular chime if there is no HA connection
# - HA service for "dingdong" (copied from Zuidwijk YAML variation)
# - HA service for "short ring"
# - button to execute short ring (behaves the same as the physical button)
# - button to perform reset (instead of switch)
# - removal of globals, instead restore from flash (or: explicity set status after boot)
# - disable certain entitities by default (e.g. WiFi signal, uptime)
# - hide timestamp in ESPHome firmware version
# - use ESPHome recent syntax for board details (in this case: esp8266)
#
# note: buttons require HA version 2021.12 or higher
substitutions:
device_name: doorbell
yaml_version: "2.1.1"
esphome:
name: "${device_name}"
comment: "https://www.zuidwijk.com/esphome-based-doorbell/"
project:
name: zuidwijk.doorbell
version: "${yaml_version}"
on_boot:
then:
- text_sensor.template.publish:
id: yaml_version
state: "${yaml_version}"
# optional: always turn on chime after reboot
# - switch.turn_on: chime_active
esp8266:
board: d1_mini
restore_from_flash: true
# dashboard_import:
# package_import_url: github://zuidwijk/doorbell/smartdoorbell.yaml
wifi:
# networks: []
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "${device_name}"
# when using a new device_name (assumption: the original name was "smartdoorbell"):
# 1) enable use_address below
# 2) upload this new config with the new name
# 3) for subsequent updates comment out the use_address line
# ---
# use_address: smartdoorbell # see comment above
# ---
captive_portal:
logger:
ota:
improv_serial:
time:
- platform: homeassistant
id: homeassistant_time
# enable Home Assistant API
api:
# add HA services
# available as e.g. esphome.doorbell_dingdong
services:
- service: dingdong
then:
- script.execute: dingdong_script
- service: ring
then:
- script.execute: short_ring_script
script:
# called by the service
- id: dingdong_script
then:
if:
condition:
- switch.is_on: chime_active
then:
- switch.turn_on: relay
- delay: 200ms
- switch.turn_off: relay
- delay: 600ms
- switch.turn_on: relay
- delay: 200ms
- switch.turn_off: relay
- delay: 600ms
- switch.turn_on: relay
- delay: 200ms
- switch.turn_off: relay
# called by the service or the button
- id: short_ring_script
then:
- script.execute: press_button_start
- delay: 0.5s
- script.execute: press_button_end
# the actual actions when a physical / short_ring_script button is pressed
- id: press_button_start
then:
- lambda: |-
id(button_pressed).publish_state(true);
- if:
condition:
- switch.is_on: chime_active
then:
- switch.turn_on: relay
- id: press_button_end
then:
- lambda: |-
id(button_pressed).publish_state(false);
- switch.turn_off: relay
sensor:
- platform: uptime
name: "${device_name} uptime"
disabled_by_default: true
- platform: wifi_signal
name: "${device_name} WiFi signal"
update_interval: 60s
disabled_by_default: true
text_sensor:
- platform: version
name: "${device_name} ESPHome version"
hide_timestamp: true
- platform: wifi_info
ip_address:
name: "${device_name} IP"
ssid:
name: "${device_name} SSID"
bssid:
name: "${device_name} BSSID"
- platform: template
lambda: return {}; # will be filled using on_boot
name: "${device_name} firmware version"
id: yaml_version
icon: mdi:file-code
entity_category: "diagnostic"
disabled_by_default: true
button:
- platform: restart
name: "${device_name} restart"
- platform: template
name: "${device_name} button" # simulates pressing button
# icon: mdi:bell-ring
disabled_by_default: true
on_press:
then:
- script.execute: short_ring_script
switch:
- platform: gpio
pin:
number: D1 #v1.3: GPIO5 = D1
inverted: false
name: "${device_name} relay"
id: relay
internal: true
icon: mdi:alarm-bell
- platform: template
name: "${device_name} chime active"
id: chime_active
restore_mode: DISABLED
optimistic: true
binary_sensor:
- platform: status
name: "${device_name} status"
- platform: template # a template sensor exposed to HA controlled by both GPIO button and ESPHome button
name: "${device_name}"
id: button_pressed
- platform: gpio
pin:
number: D5 #v1.3: GPIO14 = D5
mode: INPUT_PULLUP
inverted: true
# name: "${device_name}"
id: physical_button
internal: true
filters:
# small filter, to debounce the button press
- delayed_on: 25ms
- delayed_off: 25ms
on_press:
then:
- lambda: |-
id(button_pressed).publish_state(true);
- if: # connected to HA (API) & chime is active
condition:
and:
- api.connected:
- switch.is_on: chime_active
then:
- switch.turn_on: relay
- if: # not connected to HA (API)
condition:
not:
- api.connected:
then:
- switch.turn_on: relay
on_release:
# on release, always turn of the chime
- switch.turn_off: relay
- lambda: |-
id(button_pressed).publish_state(false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment