auto-commit

This commit is contained in:
2026-04-27 15:43:28 +02:00
parent e2e9549b9d
commit 0f9dc29b2d

View File

@@ -1,113 +1,246 @@
# ESPHome Firmware for
# Sonoff Switchman M5 2-Gang US
# Copyright (c) 2024 Mario Di Vece
# License: [MIT](https://opensource.org/license/mit/)
# Decription:
# Aims to provide a feature-rich, production-ready firmware for this elegant device
# - Provides Diagnostic data plus, status LED indicator when not connected to Home Assistant API
# - Relays may be configured individually on the UI to work in decoupled mode.
# LEDs are physically connected to the relays, and they can't be individually controlled :(
# - Exposes gestures via events:
# esphome.on_gesture { button: (A|B), gesture: (click|double_click|button_hold) }
# - Off-state (Background Brightness) of the LEDs is configurable via the UI
#
# For the below example to work, you need to keep the following entries in your secrets.yaml file:
# - wifi_ssid: "<secret>"
# - wifi_password: "<secret>"
# - ota_password: "<secret>"
# - api_key: "<32-byte-base-64-secret>"
#
# Example file (lab-office-wasw-003.yaml)
#
# substitutions:
# device_site: "lab"
# device_location_code: "office"
# device_location_name: "Office"
# device_type_code: "wasw"
# device_type_name: "Wall Switch"
# device_number: "003"
# device_ip: "192.168.1.31"
#
# packages:
# base_package:
# url: https://github.com/mariodivece/esphometemplates/
# ref: main
# file: sonoff-m5-2g-us.yaml
# refresh: 0d
#
substitutions: substitutions:
# User-required substitutions # User-required substitutions
device_site: "home" device_location_name: "Sypialnia"
device_location_code: "bedroom01" device_type_code: "m5-2c-86"
device_location_name: "Bedroom 1" device_type_name: "Przełącznik"
device_type_code: "wasw" device_number: "001"
device_type_name: "Wall Switch" device_ip: "10.48.39.20"
device_number: "044"
device_ip: "0.0.0.0"
# Project Substitutions (not intended for user substitution) # Project Substitutions
device_model: "Switchman M5 (2-Gang)" device_model: "Switchman M5-2C-86"
device_make: "Sonoff" device_make: "Sonoff"
package_version: "2024.2.13" package_version: "2024.2.13"
# Relay Configurations # Generated device name/code
device_name: "${device_number} - ${device_type_name} - ${device_location_name}"
device_code: "${device_number}-${device_type_code}"
# WiFi / API secrets
api_key: !secret api_key
wifi_ssid: !secret wifi_ssid
wifi_password: !secret wifi_password
ota_password: !secret ota_password
# Relay GPIO
relay_a_gpio: GPIO23 relay_a_gpio: GPIO23
relay_b_gpio: GPIO19 relay_b_gpio: GPIO19
# Button Configurations # Button GPIO
button_a_gpio: GPIO04 button_a_gpio: GPIO04
button_b_gpio: GPIO15 button_b_gpio: GPIO15
# Indicator LED # Indicator LED GPIO
led_indicator_gpio: GPIO22 led_indicator_gpio: GPIO22
# Allow importing this package # Diagnostics
dashboard_import: log_level: INFO
package_import_url: ${package_url} timezone: "Europe/Warsaw"
import_full_config: false
# Import packages # Button timing configurations
packages: filter_delay_on: 50ms
standard_package: filter_delay_off: 50ms
url: https://github.com/mariodivece/esphometemplates/ timing_click_1: ON for at most 400ms
ref: main timing_click_2: OFF for at least 600ms
refresh: 0d timing_double_click_1: ON for at most 500ms
files: timing_double_click_2: OFF for at most 400ms
- standard/project.yaml timing_double_click_3: ON for at most 500ms
- standard/diagnostics.yaml timing_double_click_4: OFF for at least 250ms
- standard/wifi.yaml timing_hold: ON for at least 1s
- standard/internaltemp.yaml timing_hold_repeat: 100ms
sonoffm5_package: timing_pulse: 250ms
url: https://github.com/mariodivece/esphometemplates/
ref: main # ── Board ──────────────────────────────────────────────────────────────────────
refresh: 0d
files: esp32:
- sonoff-m5/board.yaml board: esp32dev
- sonoff-m5/timings.yaml framework:
- sonoff-m5/status-led.yaml type: arduino
- sonoff-m5/backlight.yaml version: recommended
- sonoff-m5/indicator-led.yaml
- sonoff-m5/mode-button-a.yaml # ── ESPHome / Project ──────────────────────────────────────────────────────────
- sonoff-m5/mode-button-b.yaml
- sonoff-m5/relay-a.yaml globals:
- sonoff-m5/relay-b.yaml - id: cpu_speed
- sonoff-m5/button-a.yaml type: int
- sonoff-m5/button-b.yaml restore_value: no
initial_value: "0"
esphome:
name: "${device_code}"
friendly_name: "${device_name}"
comment: "${device_model} by ${device_make}"
name_add_mac_suffix: false
min_version: "2023.2.0"
project:
name: "${device_make}.${device_model}"
version: "${package_version}"
on_boot:
- priority: 900.0
then:
- lambda: |-
id(cpu_speed) = ESP.getCpuFreqMHz();
# ── Connectivity ───────────────────────────────────────────────────────────────
api:
reboot_timeout: 0s
encryption:
key: "${api_key}"
ota:
- platform: esphome
password: "${ota_password}"
safe_mode:
wifi:
fast_connect: false
power_save_mode: light
ssid: "${wifi_ssid}"
password: "${wifi_password}"
use_address: "${device_ip}"
ap:
ssid: "${device_code}-setup"
password: "${wifi_password}"
ap_timeout: 5min
captive_portal:
# ── Logging / Time ─────────────────────────────────────────────────────────────
logger:
level: "${log_level}"
baud_rate: 0
time:
- platform: sntp
id: time_service
timezone: ${timezone}
update_interval: 15min
servers:
- 0.pool.ntp.org
- 1.pool.ntp.org
- 2.pool.ntp.org
# ── Outputs ────────────────────────────────────────────────────────────────────
output:
- platform: ledc
id: pwm_output
pin: GPIO18
frequency: 1000 Hz
# ── Lights ─────────────────────────────────────────────────────────────────────
light:
- platform: status_led
name: "LED"
id: led_status
pin:
number: GPIO05
inverted: true
ignore_strapping_warning: true
internal: true
restore_mode: RESTORE_DEFAULT_ON
- platform: monochromatic
output: pwm_output
name: "Background Brightness"
restore_mode: RESTORE_DEFAULT_OFF
icon: 'mdi:led-outline'
entity_category: 'config'
# ── Switches ───────────────────────────────────────────────────────────────────
switch: switch:
- platform: gpio
# Modify behavior of imported Relay B name: "Relay A"
- id: !extend relay_b pin: ${relay_a_gpio}
id: relay_a
restore_mode: RESTORE_DEFAULT_OFF
- platform: gpio
name: "Relay B"
pin: ${relay_b_gpio}
id: relay_b
restore_mode: RESTORE_DEFAULT_OFF
on_turn_on: on_turn_on:
- switch.turn_on: led_indicator - switch.turn_on: led_indicator
on_turn_off: on_turn_off:
- switch.turn_off: led_indicator - switch.turn_off: led_indicator
- platform: gpio
id: led_indicator
internal: true
pin: ${led_indicator_gpio}
# ── Selects ────────────────────────────────────────────────────────────────────
select:
- platform: template
name: "Mode - Button A"
id: mode_a
icon: 'mdi:link-box-outline'
entity_category: 'config'
options:
- "Latching" # 0
- "Momentary" # 1
- "Pulse" # 2
- "Decoupled" # 3
initial_option: "Latching"
restore_value: true
optimistic: true
set_action:
- switch.turn_off: relay_a
- platform: template
name: "Mode - Button B"
id: mode_b
icon: 'mdi:link-box-outline'
entity_category: 'config'
options:
- "Latching" # 0
- "Momentary" # 1
- "Pulse" # 2
- "Decoupled" # 3
initial_option: "Latching"
restore_value: true
optimistic: true
set_action:
- switch.turn_off: relay_b
# ── Binary Sensors ─────────────────────────────────────────────────────────────
binary_sensor: binary_sensor:
- platform: status
icon: 'mdi:home-assistant'
name: "API Status"
id: sensor_status
disabled_by_default: true
# Button A - platform: template
- id: !extend button_a name: "API connected"
id: sensor_api_connected
internal: true
entity_category: 'diagnostic'
device_class: 'connectivity'
lambda: return global_api_server->is_connected();
on_press:
- light.turn_off: led_status
on_release:
- light.turn_on: led_status
- platform: gpio
name: "Button A"
id: button_a
pin:
number: ${button_a_gpio}
mode: INPUT_PULLUP
inverted: true
ignore_strapping_warning: false
filters:
- delayed_on: ${filter_delay_on}
- delayed_off: ${filter_delay_off}
on_press: on_press:
# Latching # Latching
- if: - if:
@@ -136,9 +269,54 @@ binary_sensor:
- lambda: 'return id(mode_a).active_index() == 1;' - lambda: 'return id(mode_a).active_index() == 1;'
then: then:
- switch.turn_off: relay_a - switch.turn_off: relay_a
on_multi_click:
- timing:
- ${timing_click_1}
- ${timing_click_2}
then:
- homeassistant.event:
event: esphome.on_gesture
data:
button: A
gesture: single_click
- timing:
- ${timing_double_click_1}
- ${timing_double_click_2}
- ${timing_double_click_3}
- ${timing_double_click_4}
then:
- homeassistant.event:
event: esphome.on_gesture
data:
button: A
gesture: double_click
- timing:
- ${timing_hold}
then:
- while:
condition:
binary_sensor.is_on: button_a
then:
- light.toggle: led_status
- homeassistant.event:
event: esphome.on_gesture
data:
button: A
gesture: button_hold
- delay: ${timing_hold_repeat}
- light.turn_off: led_status
# Button B - platform: gpio
- id: !extend button_b name: "Button B"
id: button_b
pin:
number: ${button_b_gpio}
mode: INPUT_PULLUP
inverted: true
ignore_strapping_warning: true
filters:
- delayed_on: ${filter_delay_on}
- delayed_off: ${filter_delay_off}
on_press: on_press:
# Latching # Latching
- if: - if:
@@ -166,7 +344,6 @@ binary_sensor:
- lambda: 'return id(mode_b).active_index() == 3;' - lambda: 'return id(mode_b).active_index() == 3;'
then: then:
- switch.turn_on: led_indicator - switch.turn_on: led_indicator
on_release: on_release:
# Momentary # Momentary
- if: - if:
@@ -180,3 +357,138 @@ binary_sensor:
- lambda: 'return id(mode_b).active_index() == 3;' - lambda: 'return id(mode_b).active_index() == 3;'
then: then:
- switch.turn_off: led_indicator - switch.turn_off: led_indicator
on_multi_click:
- timing:
- ${timing_click_1}
- ${timing_click_2}
then:
- homeassistant.event:
event: esphome.on_gesture
data:
button: B
gesture: single_click
- timing:
- ${timing_double_click_1}
- ${timing_double_click_2}
- ${timing_double_click_3}
- ${timing_double_click_4}
then:
- homeassistant.event:
event: esphome.on_gesture
data:
button: B
gesture: double_click
- timing:
- ${timing_hold}
then:
- while:
condition:
binary_sensor.is_on: button_b
then:
- light.toggle: led_status
- homeassistant.event:
event: esphome.on_gesture
data:
button: B
gesture: button_hold
- delay: ${timing_hold_repeat}
- light.turn_off: led_status
# ── Sensors ────────────────────────────────────────────────────────────────────
sensor:
- platform: wifi_signal
name: "RSSI"
id: sensor_rssi
icon: 'mdi:signal'
update_interval: 60s
entity_category: "diagnostic"
- platform: internal_temperature
name: "Internal Temperature"
disabled_by_default: true
icon: mdi:heat-wave
- platform: template
name: "CPU Frequency"
icon: "mdi:speedometer"
accuracy_decimals: 0
unit_of_measurement: Mhz
disabled_by_default: true
lambda: |-
return (id(cpu_speed));
entity_category: diagnostic
- platform: template
id: esp_memory
icon: mdi:memory
name: Free Memory
lambda: return heap_caps_get_free_size(MALLOC_CAP_INTERNAL) / 1024;
unit_of_measurement: "kB"
state_class: measurement
entity_category: "diagnostic"
disabled_by_default: true
- platform: uptime
name: "Uptime"
id: sensor_uptime
update_interval: 60s
entity_category: "diagnostic"
internal: true
on_raw_value:
then:
- text_sensor.template.publish:
id: uptime_human
state: !lambda |-
int seconds = round(id(sensor_uptime).raw_state);
int days = seconds / (24 * 3600);
seconds = seconds % (24 * 3600);
int hours = seconds / 3600;
seconds = seconds % 3600;
int minutes = seconds / 60;
seconds = seconds % 60;
return (
(days ? String(days) + "d " : "") +
(hours ? String(hours) + "h " : "") +
(minutes ? String(minutes) + "m " : "") +
(String(seconds) + "s")
).c_str();
# ── Text Sensors ───────────────────────────────────────────────────────────────
text_sensor:
- platform: wifi_info
ip_address:
id: ip_address
name: "IP Address"
icon: "mdi:wan"
- platform: template
name: "Uptime"
id: uptime_human
icon: "mdi:timer-check-outline"
update_interval: 60s
entity_category: "diagnostic"
disabled_by_default: true
- platform: template
name: "Deployment Version"
lambda: return {"${package_version}"};
disabled_by_default: true
icon: "mdi:tag"
entity_category: diagnostic
# ── Buttons ────────────────────────────────────────────────────────────────────
button:
- platform: restart
name: "Reboot Device"
id: button_restart
icon: mdi:power-cycle
entity_category: "diagnostic"
- platform: factory_reset
disabled_by_default: false
name: "Load Factory Settings"
id: factory_reset_all
icon: mdi:factory