Skip to content

Commit

Permalink
Support H-Bridge relay switch
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Dec 18, 2024
1 parent e263368 commit c709f61
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 11 deletions.
49 changes: 38 additions & 11 deletions upk2esphome/parts/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,47 @@ def generate(yr: YamlResult, config: ConfigData, opts: Opts):
for i in range(0, 10):
rl_pin = config.get(f"rl{i}_pin", None)
rl_inv = config.get(f"rl{i}_lv", None) == 0
rl_on_pin = config.get(f"rl_on{i}_pin", None)
rl_on_inv = config.get(f"rl_on{i}_lv", None) == 0
rl_off_pin = config.get(f"rl_off{i}_pin", None)
rl_off_inv = config.get(f"rl_off{i}_lv", None) == 0
led_pin = config.get(f"led{i}_pin", None)
led_inv = config.get(f"led{i}_lv", None) == 0
bt_pin = config.get(f"bt{i}_pin", None)
bt_inv = config.get(f"bt{i}_lv", None) == 0
onoff_pin = config.get(f"onoff{i}", None)
if rl_pin is None:
continue

yr.log(f" - relay {i}: pin P{rl_pin}")
yr.found = True
switch = {
"platform": "gpio",
"id": f"switch_{i}",
"name": f"Relay {i}",
"pin": f"P{rl_pin}",
}
invert(switch, rl_inv)
if rl_pin is not None:
yr.log(f" - relay {i}: pin P{rl_pin}")
yr.found = True
switch = {
"platform": "gpio",
"id": f"switch_{i}",
"name": f"Relay {i}",
"pin": f"P{rl_pin}",
}
invert(switch, rl_inv)
elif rl_on_pin is not None and rl_off_pin is not None:
yr.log(
f" - relay {i} (bistable/H-Bridge): "
f"pin ON P{rl_on_pin}, "
f"pin OFF P{rl_off_pin}"
)
yr.found = True
pulse_length = config.get(f"rl{i}_drvtime", 100)
switch = {
"platform": "hbridge",
"id": f"switch_{i}",
"name": f"Relay {i}",
"on_pin": f"P{rl_on_pin}",
"off_pin": f"P{rl_off_pin}",
"pulse_length": f"{pulse_length}ms",
"wait_time": f"{pulse_length // 2}ms",
}
invert(switch, rl_on_inv, "on_pin")
invert(switch, rl_off_inv, "off_pin")
else:
continue

if led_pin is not None and netled_reuse:
yr.warn(
Expand Down Expand Up @@ -119,6 +143,9 @@ def generate(yr: YamlResult, config: ConfigData, opts: Opts):
switches.append(switch["id"])
yr.switch(switch)

if not switches:
yr.warn("The switch/plug type is unknown (no relay pin found)")

bt_pin = config.get(f"total_bt_pin", None)
bt_inv = config.get(f"total_bt_lv", None) == 0
if bt_pin is not None:
Expand Down
36 changes: 36 additions & 0 deletions upk2esphome/tests/breaker_hbridge.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"bt1_lv": 0,
"bt1_pin": 17,
"bt1_type": 0,
"ch1_stat": 2,
"ch_cddpid1": 9,
"ch_dpid1": 1,
"ch_num": 1,
"chip_type": 3,
"crc": 80,
"ele_fun_en": 1,
"jv": "1.0.1",
"led1_lv": 0,
"led1_pin": 9,
"lose_vol": 76,
"module": "CBU",
"net_trig": 2,
"netled1_lv": 0,
"netled1_pin": 15,
"netled_reuse": 0,
"netn_led": 0,
"nety_led": 1,
"online_time": 10,
"over_cur": 63000,
"over_vol": 265,
"reset_t": 5,
"rl1_drvtime": 60,
"rl1_type": 1,
"rl_off1_lv": 1,
"rl_off1_pin": 26,
"rl_on1_lv": 1,
"rl_on1_pin": 24,
"temp_fun_en": 0,
"vol_def": 0,
"wide_vol": 0
}
64 changes: 64 additions & 0 deletions upk2esphome/tests/expected_output/breaker_hbridge.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
output:
- platform: libretiny_pwm
id: output_led_1
pin:
number: P9
inverted: true

light:
- platform: monochromatic
id: light_switch_1
output: output_led_1

binary_sensor:
- platform: gpio
id: binary_switch_1
pin:
number: P17
inverted: true
mode: INPUT_PULLUP
on_press:
then:
- switch.toggle: switch_1

switch:
- platform: hbridge
id: switch_1
name: Relay 1
on_pin: P24
off_pin: P26
pulse_length: 60ms
wait_time: 30ms
on_turn_on:
- light.turn_on: light_switch_1
on_turn_off:
- light.turn_off: light_switch_1

status_led:
pin:
number: P15
inverted: true

uart:
id: uart_bus
tx_pin: TX1
rx_pin: RX1
baud_rate: 4800
stop_bits: 1

sensor:
- platform: bl0942
uart_id: uart_bus
current:
name: BL0942 Current
voltage:
name: BL0942 Voltage
power:
name: BL0942 Power
filters:
multiply: -1
energy:
name: BL0942 Energy
frequency:
name: BL0942 Frequency
accuracy_decimals: 2

0 comments on commit c709f61

Please sign in to comment.