Refactoring

This commit is contained in:
Nikolay Vasilchuk
2022-09-09 21:04:09 +03:00
parent e59addb02b
commit 6e66e9c20f
14 changed files with 59 additions and 51 deletions

View File

@@ -0,0 +1,3 @@
api:
password: $api_password
reboot_timeout: 0s

View File

@@ -0,0 +1,120 @@
esphome:
name: $mdns_name
build_path: $build_path
platform: ESP8266
board: $board
comment: "espdomofon.ru"
esp8266_restore_from_flash: true
project:
name: "espdomofon.ru"
version: "1.6.2"
platformio_options:
build_flags: -DBEARSSL_SSL_BASIC -DMMU_IRAM_SIZE=0xC000 -DMMU_ICACHE_SIZE=0x4000 -DMMU_IRAM_HEAP
on_boot:
priority: -100
then:
- script.execute: state_ready
- light.turn_on:
id: rgb_led
effect: "Domofon"
dashboard_import:
package_import_url: github://Anonym-tsk/smart-domofon/ge1mer/domofon.yaml@master
wifi:
ssid: $wifi_ssid
password: $wifi_password
fast_connect: on
ap:
ssid: $ap_ssid
password: $ap_password
logger:
baud_rate: 0
logs:
light: INFO
ota:
password: $ota_password
globals:
- id: mode_auto_open
type: bool
restore_value: yes
initial_value: 'false'
- id: mode_auto_open_once
type: bool
restore_value: yes
initial_value: 'false'
- id: mode_auto_reject
type: bool
restore_value: yes
initial_value: 'false'
- id: mode_mute
type: bool
restore_value: yes
initial_value: 'false'
- id: mode_mute_once
type: bool
restore_value: yes
initial_value: 'false'
sensor:
- platform: template
name: "${board_name} Heap Size"
lambda: "return ESP.getFreeHeap();"
update_interval: 20s
unit_of_measurement: bytes
accuracy_decimals: 0
entity_category: "diagnostic"
- platform: uptime
internal: true
id: uptime_sensor
update_interval: 60s
on_raw_value:
then:
- text_sensor.template.publish:
id: uptime_human
state: !lambda |-
int seconds = round(id(uptime_sensor).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 ? to_string(days) + "d " : "") +
(hours ? to_string(hours) + "h " : "") +
(minutes ? to_string(minutes) + "m " : "") +
(to_string(seconds) + "s")
).c_str();
text_sensor:
- platform: template
id: uptime_human
name: "${board_name} Uptime"
icon: mdi:clock-start
entity_category: "diagnostic"
substitutions:
# Ge1mer board
board: esp12e
pin_relay_answer: GPIO14 # 330 Ohm, NO relay
pin_relay_phone: GPIO16 # Intercom, NC relay
pin_relay_mute: GPIO12 # 50 Ohm, NO relay
pin_led_red: GPIO5
pin_led_green: GPIO4
pin_led_blue: GPIO15
pin_call_detect: GPIO13
pin_btn_accept: GPIO0
packages:
number: !include number.yaml
script: !include script.yaml
output: !include output.yaml
switch: !include switch.yaml
button: !include button.yaml
light: !include light.yaml
binary_sensor: !include binary_sensor.yaml

View File

@@ -0,0 +1,115 @@
binary_sensor:
# Call detection
- platform: gpio
name: "${board_name} incoming call"
id: incoming_call
device_class: sound
pin:
number: $pin_call_detect
mode: INPUT_PULLUP
inverted: True
filters:
delayed_off: $call_end_detect_delay
on_press:
then:
script.execute: state_call
# Accept HW button
- platform: gpio
name: "${board_name} button"
id: button_hw
pin:
number: $pin_btn_accept
mode: INPUT
inverted: True
filters:
delayed_on: 25ms
on_multi_click:
# Double click - mute mode
- timing:
- ON for at most $short_click_time_to
- OFF for at most $short_click_time_to
- ON for $short_click_time_from to $short_click_time_to
- OFF for at least 200ms
then:
lambda: |-
ESP_LOGD("main", "Double Click");
if (!id(incoming_call).state) {
if (id(mode_mute_once)) {
id(mode_mute) = true;
id(mode_mute_once) = false;
} else {
id(mode_mute) = false;
id(mode_mute_once) = true;
}
}
# Long click - disable auto opening and mute
- timing:
- ON for at least $short_click_time_to
then:
lambda: |-
ESP_LOGD("main", "Single Long Click");
if (id(incoming_call).state) {
id(call_reject).execute();
} else {
id(mode_auto_open) = false;
id(mode_auto_open_once) = false;
id(mode_auto_reject) = false;
id(mode_mute) = false;
id(mode_mute_once) = false;
}
# Short click - open door or enable once auto opening
- timing:
- ON for at most $short_click_time_to
- OFF for at least 500ms
then:
lambda: |-
ESP_LOGD("main", "Single Short Click");
if (id(incoming_call).state) {
id(call_accept).execute();
} else {
if (id(mode_auto_open_once)) {
id(mode_auto_open) = true;
id(mode_auto_open_once) = false;
id(mode_auto_reject) = false;
} else if (id(mode_auto_open)) {
id(mode_auto_open) = false;
id(mode_auto_open_once) = false;
id(mode_auto_reject) = true;
} else {
id(mode_auto_open) = false;
id(mode_auto_open_once) = true;
id(mode_auto_reject) = false;
}
}
# # 8 clicks - forget wifi settings
# - timing:
# - ON for 5ms to 1s
# - OFF for 5ms to 1s
# - ON for 5ms to 1s
# - OFF for 5ms to 1s
# - ON for 5ms to 1s
# - OFF for 5ms to 1s
# - ON for 5ms to 1s
# - OFF for 5ms to 1s
# - ON for 5ms to 1s
# - OFF for 5ms to 1s
# - ON for 5ms to 1s
# - OFF for 5ms to 1s
# - ON for 5ms to 1s
# - OFF for 5ms to 1s
# - ON for 5ms to 1s
# then:
# lambda: |-
# ESP_LOGW("DMF", "Erase settings and restart...");
# id(mode_auto_open) = false;
# id(mode_auto_open_once) = false;
# id(mode_auto_reject) = false;
# id(mode_mute) = false;
# id(mode_mute_once) = false;
# wifi::global_wifi_component->save_wifi_sta("-----", "");
# delay(500);
# ESP.restart();

View File

@@ -0,0 +1,29 @@
button:
- platform: restart
name: "${board_name} Restart"
# Accept call
- platform: template
name: "${board_name} accept call"
icon: "mdi:door-open"
on_press:
then:
lambda: |-
if (id(incoming_call).state) {
id(call_accept).execute();
} else {
ESP_LOGD("main", "No incoming call");
}
# Reject call
- platform: template
name: "${board_name} reject call"
icon: "mdi:door-closed-lock"
on_press:
then:
lambda: |-
if (id(incoming_call).state) {
id(call_reject).execute();
} else {
ESP_LOGD("main", "No incoming call");
}

View File

@@ -0,0 +1 @@
captive_portal:

View File

@@ -0,0 +1,124 @@
# RGB Led (not exported to Home Assistant)
light:
- platform: status_led
internal: true
name: "Status LED"
pin: $pin_led_blue
- platform: rgb
id: rgb_led
name: "${board_name} led"
internal: true
restore_mode: ALWAYS_ON
default_transition_length: 0ms
red: led_red
green: led_green
blue: led_blue
effects:
- lambda:
name: "Domofon"
update_interval: 200ms
lambda: |-
static int tick = 0;
static bool old_incoming = false;
static bool old_mute = false;
static bool old_mute_once = false;
static bool old_auto_open = false;
static bool old_auto_open_once = false;
static bool old_auto_reject = false;
float brigtness = id(led_brightness).state / 10;
bool incoming = id(incoming_call).state;
bool mute = id(mode_mute);
bool mute_once = id(mode_mute_once);
bool auto_open = id(mode_auto_open);
bool auto_open_once = id(mode_auto_open_once);
bool auto_reject = id(mode_auto_reject);
const bool state_updated = incoming != old_incoming
|| mute != old_mute
|| mute_once != old_mute_once
|| auto_open != old_auto_open
|| auto_open_once != old_auto_open_once
|| auto_reject != old_auto_reject;
auto call = id(rgb_led).turn_on();
if (initial_run || state_updated) {
tick = 0;
}
if (tick == 0) {
if (incoming || auto_reject) {
call.set_rgb(1.0, 0.0, 0.0); // red
call.set_brightness(brigtness);
} else if (mute && auto_open_once) {
call.set_rgb(0.0, 0.4, 1.0); // blue
call.set_brightness(brigtness);
} else if (auto_open || auto_open_once) {
call.set_rgb(0.0, 1.0, 0.0); // green
call.set_brightness(brigtness);
} else if (mute || mute_once) {
call.set_rgb(0.0, 0.4, 1.0); // blue
call.set_brightness(brigtness);
} else {
call.set_brightness(0.01);
}
} else if (tick == 1) {
if (incoming) {
call.set_brightness(0.01);
} else if (mute && auto_open_once) {
call.set_rgb(0.0, 1.0, 0.0); // green
call.set_brightness(brigtness);
} else if (mute || mute_once) {
if (auto_open || auto_open_once || auto_reject) {
call.set_rgb(0.0, 0.4, 1.0); // blue
call.set_brightness(brigtness);
} else if (mute_once) {
call.set_brightness(0.01);
}
} else if (auto_open) {
call.set_rgb(0.0, 1.0, 0.0); // green
call.set_brightness(brigtness);
} else if (auto_open_once) {
call.set_brightness(0.01);
} else if (auto_reject) {
call.set_rgb(1.0, 0.0, 0.0); // red
call.set_brightness(brigtness);
} else {
call.set_brightness(0.01);
}
} else if (tick == 2) {
if (incoming) {
call.set_brightness(0.01);
} else if (auto_reject) {
call.set_rgb(1.0, 0.0, 0.0); // red
call.set_brightness(brigtness);
} else if (auto_open) {
call.set_rgb(0.0, 1.0, 0.0); // green
call.set_brightness(brigtness);
} else if (mute) {
call.set_rgb(0.0, 0.4, 1.0); // blue
call.set_brightness(brigtness);
} else {
call.set_brightness(0.01);
}
} else if (tick == 7 && incoming) {
call.set_rgb(1.0, 0.0, 0.0); // red
call.set_brightness(brigtness);
} else if (tick == 8 && incoming) {
call.set_brightness(0.01);
}
call.perform();
if (++tick == 14) {
tick = 0;
}
old_incoming = incoming;
old_mute = mute;
old_mute_once = mute_once;
old_auto_open = auto_open;
old_auto_open_once = auto_open_once;
old_auto_reject = auto_reject;

View File

@@ -0,0 +1,6 @@
mqtt:
broker: $mqtt_host
port: $mqtt_port
discovery: false
username: $mqtt_username
password: $mqtt_password

View File

@@ -0,0 +1,72 @@
number:
- platform: template
name: "${board_name} LED Brightness"
icon: "mdi:led-on"
id: led_brightness
optimistic: true
restore_value: true
entity_category: "config"
initial_value: 7
min_value: 0
max_value: 10
step: 1
# Delay before answer call
- platform: template
name: "${board_name} Delay Before Answer"
icon: "mdi:clock-time-two-outline"
id: relay_before_answer_delay
optimistic: true
restore_value: true
mode: box
unit_of_measurement: "ms"
entity_category: "config"
initial_value: 400
min_value: 100
max_value: 5000
step: 100
# Delay between answer call and open/close door
- platform: template
name: "${board_name} Answer On Time"
icon: "mdi:clock-time-two-outline"
id: relay_answer_on_time
optimistic: true
restore_value: true
mode: box
unit_of_measurement: "ms"
entity_category: "config"
initial_value: 1000
min_value: 100
max_value: 5000
step: 100
# How long the "open door button" will be pressed
- platform: template
name: "${board_name} Open On Time"
icon: "mdi:clock-time-two-outline"
id: relay_open_on_time
optimistic: true
restore_value: true
mode: box
unit_of_measurement: "ms"
entity_category: "config"
initial_value: 300
min_value: 50
max_value: 2000
step: 50
# Delay in "answer" state after opening door
- platform: template
name: "${board_name} Delay After Open"
icon: "mdi:clock-time-two-outline"
id: relay_after_open_delay
optimistic: true
restore_value: true
mode: box
unit_of_measurement: "ms"
entity_category: "config"
initial_value: 500
min_value: 100
max_value: 5000
step: 100

View File

@@ -0,0 +1,43 @@
output:
# Red LED
- platform: esp8266_pwm
id: led_red
pin:
number: $pin_led_red
mode: OUTPUT
# Green LED
- platform: esp8266_pwm
id: led_green
pin:
number: $pin_led_green
mode: OUTPUT
# Blue LED
- platform: esp8266_pwm
id: led_blue
pin:
number: $pin_led_blue
mode: OUTPUT
# Intercom
- platform: gpio
pin:
number: $pin_relay_phone
inverted: $phone_relay_inverted
mode: OUTPUT
id: relay_phone
# Mute sound switch (50 Ohm instead of intercom)
- platform: gpio
pin:
number: $pin_relay_mute
mode: OUTPUT
id: relay_mute
# Relay answer (330 Ohm, internal)
- platform: gpio
id: relay_answer
pin:
number: $pin_relay_answer
mode: OUTPUT

View File

@@ -0,0 +1,79 @@
script:
# Connected intercom or mute resistor
- id: state_ready
then:
- lambda: |-
if (id(mode_mute) || id(mode_mute_once)) {
id(relay_mute).turn_on();
id(relay_phone).turn_off();
} else {
id(relay_phone).turn_on();
id(relay_mute).turn_off();
}
id(relay_answer).turn_off();
# Connected answer resistor
- id: state_answer
then:
- lambda: |-
id(relay_answer).turn_on();
id(relay_phone).turn_off();
id(relay_mute).turn_off();
# Disconnected all
- id: state_open
then:
- lambda: |-
id(relay_answer).turn_off();
id(relay_phone).turn_off();
id(relay_mute).turn_off();
# Accept incoming call
- id: call_accept
then:
- logger.log: "Accept call"
- delay: !lambda "return id(relay_before_answer_delay).state;"
- script.execute: state_answer
- delay: !lambda "return id(relay_answer_on_time).state;"
- script.execute: state_open
- delay: !lambda "return id(relay_open_on_time).state;"
- script.execute: state_answer
- delay: !lambda "return id(relay_after_open_delay).state;"
- script.execute: state_ready
- globals.set:
id: mode_mute_once
value: 'false'
# - homeassistant.event:
# event: esphome.domofon_call_accept
# data:
# entity_uid: $mdns_name
# Reject incoming call
- id: call_reject
then:
- logger.log: "Reject call"
- delay: !lambda "return id(relay_before_answer_delay).state;"
- script.execute: state_answer
- delay: !lambda "return id(relay_answer_on_time).state;"
- script.execute: state_ready
- globals.set:
id: mode_mute_once
value: 'false'
# - homeassistant.event:
# event: esphome.domofon_call_reject
# data:
# entity_uid: $mdns_name
# Call state
- id: state_call
then:
- lambda: |-
ESP_LOGD("main", "Incoming call");
if (id(mode_auto_reject)) {
id(call_reject).execute();
} else if (id(mode_auto_open)) {
id(call_accept).execute();
} else if (id(mode_auto_open_once)) {
id(call_accept).execute();
id(mode_auto_open_once) = false;
}

View File

@@ -0,0 +1,110 @@
switch:
# Automatically open door switch
- platform: template
name: "${board_name} automatically open"
id: auto_open
icon: "mdi:door-open"
lambda: |-
return id(mode_auto_open);
turn_on_action:
- globals.set:
id: mode_auto_open
value: 'true'
turn_off_action:
- globals.set:
id: mode_auto_open
value: 'false'
on_turn_on:
- lambda: |-
id(mode_auto_open_once) = false;
id(mode_auto_reject) = false;
# Automatically open door once switch
- platform: template
name: "${board_name} automatically open once"
id: auto_open_once
icon: "mdi:door-open"
lambda: |-
return id(mode_auto_open_once);
turn_on_action:
- globals.set:
id: mode_auto_open_once
value: 'true'
turn_off_action:
- globals.set:
id: mode_auto_open_once
value: 'false'
on_turn_on:
- lambda: |-
id(mode_auto_open) = false;
id(mode_auto_reject) = false;
# Automatically reject call switch
- platform: template
name: "${board_name} automatically reject"
id: auto_reject
icon: "mdi:door-closed-lock"
lambda: |-
return id(mode_auto_reject);
turn_on_action:
- globals.set:
id: mode_auto_reject
value: 'true'
turn_off_action:
- globals.set:
id: mode_auto_reject
value: 'false'
on_turn_on:
- lambda: |-
id(mode_auto_open) = false;
id(mode_auto_open_once) = false;
# Mute sound switch
- platform: template
name: "${board_name} mute sound"
id: mute
icon: "mdi:volume-off"
lambda: |-
return id(mode_mute);
turn_on_action:
- globals.set:
id: mode_mute
value: 'true'
turn_off_action:
- globals.set:
id: mode_mute
value: 'false'
on_turn_on:
- lambda: |-
id(mode_mute_once) = false;
id(relay_mute).turn_on();
id(relay_phone).turn_off();
on_turn_off:
- lambda: |-
id(relay_phone).turn_on();
id(relay_mute).turn_off();
# Mute sound once switch
- platform: template
name: "${board_name} mute sound once"
id: mute_once
icon: "mdi:volume-off"
lambda: |-
return id(mode_mute_once);
turn_on_action:
- globals.set:
id: mode_mute_once
value: 'true'
turn_off_action:
- globals.set:
id: mode_mute_once
value: 'false'
on_turn_on:
- lambda: |-
id(mode_mute) = false;
id(relay_mute).turn_on();
id(relay_phone).turn_off();
on_turn_off:
- lambda: |-
id(relay_phone).turn_on();
id(relay_mute).turn_off();

View File

@@ -0,0 +1,2 @@
web_server:
port: 80