fix cutoff a grid peak shaving register
Some checks failed
CI and deploy / migration-check (push) Failing after 13s
CI and deploy / deploy (push) Has been skipped

This commit is contained in:
Dusan Vojacek
2026-04-29 13:36:38 +02:00
parent 2c884e2135
commit dede8d604d
12 changed files with 79 additions and 125 deletions

View File

@@ -36,7 +36,8 @@ BATT_VOLTAGE_V = 51.2
# Reg 143 ve SELL: min(|grid_setpoint_w|, …) nesmí klesnout pod tuto podlahu (W) — kvůli chování firmware, ne mapování režimu.
REG143_SELL_CAP_MIN_W = 200
# Reg 178 pevné hodnoty (bit45); bez read-modify-write (kolize s Loxone / transaction ID)
# Reg 178 bitové pole: používáme bity 45 (peak shaving switch) a bity 01 (MI export cutoff).
# Ostatní bity zachovat → read-modify-write.
REG178_SELL = 0b00100000 # 32, grid peak shaving disable
REG178_PASSIVE = 0b00110000 # 48, grid peak shaving enable (PASSIVE i CHARGE)
# TOU reg 166+ ve PASSIVE při prioritě baterie: signál střídači „využij celý dostupný rozsah“,
@@ -44,14 +45,11 @@ REG178_PASSIVE = 0b00110000 # 48, grid peak shaving enable (PASSIVE i CHARGE)
DEYE_TOU_SOC_PASSIVE_BATTERY_PRIORITY_PCT = 100
# Verify: jen bity 45 (horní byte layout v dokumentaci); ostatní bity mohou mít firmware / Loxone
REG178_VERIFY_MASK = 0x0030
# Reg 179 Control board special 1: bits 01 ovládají MI export cutoff (AC coupling / GEN).
REG179_MI_EXPORT_MASK = 0x0003
REG179_MI_EXPORT_DISABLE = 0b10
REG179_MI_EXPORT_ENABLE = 0b11
def _deye_reg179_verify_match(expected_i: int, actual_i: int) -> bool:
return (int(expected_i) & REG179_MI_EXPORT_MASK) == (int(actual_i) & REG179_MI_EXPORT_MASK)
# Reg 178 bits 01: MI export cutoff (AC coupling / GEN).
REG178_MI_EXPORT_MASK = 0x0003
REG178_MI_EXPORT_DISABLE = 0b10
REG178_MI_EXPORT_ENABLE = 0b11
REG178_VERIFY_MASK_COMBINED = REG178_VERIFY_MASK | REG178_MI_EXPORT_MASK
# Po 3 neúspěšných verify pokusech → SELF_SUSTAIN jen u těchto registrech (bezpečnost / export).
# 6264 řeší toleranční bundle (nemění režim). 178 a TOU power W jsou „soft“ — jen log + Discord.
@@ -63,7 +61,9 @@ DEYE_LV_BATTERY_MAX_CHARGE_DISCHARGE_A = 350
def _deye_reg178_verify_match(expected_i: int, actual_i: int) -> bool:
return (int(expected_i) & REG178_VERIFY_MASK) == (int(actual_i) & REG178_VERIFY_MASK)
return (int(expected_i) & REG178_VERIFY_MASK_COMBINED) == (
int(actual_i) & REG178_VERIFY_MASK_COMBINED
)
def deye_reg_triggers_self_sustain_after_verify_exhaust(reg: int) -> bool:
@@ -121,8 +121,7 @@ DEYE_REGISTER_NAMES: dict[int, str] = {
142: "limit_control (0=selling first, 1=zero export to load, 2=zero export to CT)",
143: "export_limit_w (max export do sítě)",
145: "solar_sell (0=disabled, 1=enabled)",
178: "grid_peak_shaving_switch (SELL=32 bit4-5=10, PASSIVE/CHARGE=48 bit4-5=11)",
179: "control_board_special_1 (bits0-1: MI export cutoff disable=2 enable=3)",
178: "control_board_special_1 (bits0-1: MI export cutoff disable=2 enable=3; bits4-5 peak shaving 32/48)",
148: "time_point_1_time",
149: "time_point_2_time",
154: "time_point_1_power_w",
@@ -338,16 +337,6 @@ def _drop_registers_matching_last_verified(
if int(reg) == 178 and _deye_reg178_verify_match(int(val), int(lv)):
skipped.append(int(reg))
continue
# reg179: porovnáváme jen bits01 maskou 0x0003 (masked RMW zachovává ostatní bity).
if int(reg) == 179 and _deye_reg179_verify_match(int(val), int(lv)):
# GEN cutoff (BA81): chceme na zařízení dostat "clean" hodnotu 2/3.
# Pokud minulý verified stav obsahuje jiné bity (např. 0xFFFE/0xFFFF),
# maska sice sedí, ale firmware/UI nemusí cutoff aplikovat správně.
# Proto reg179 skipneme jen tehdy, když je poslední verified hodnota už
# skutečně 2 nebo 3 (tj. clean value), ne jen maskově ekvivalentní.
if int(lv) in (REG179_MI_EXPORT_DISABLE, REG179_MI_EXPORT_ENABLE):
skipped.append(int(reg))
continue
if int(lv) == int(val):
skipped.append(int(reg))
continue
@@ -370,7 +359,7 @@ class ControlSetpoints:
deye_physical_mode: str | None = None
#: True = zákaz exportu (BLOCK_EXPORT) pro daný slot: např. při efektivní vykupní ceně < 0.
export_ban: bool = False
#: True = odpojit GEN port (MI export cutoff) v tomto slotu dle plánu (reg 179 bits0-1).
#: True = odpojit GEN port (MI export cutoff) v tomto slotu dle plánu (reg 178 bits0-1, 0-based).
#: None/False = neodpojovat.
deye_gen_cutoff_enabled: bool = False
#: Efektivní vykupní cena slotu (Kč/kWh z plánu); pro TOU řízení priorit baterie vs. přetok
@@ -833,8 +822,6 @@ async def verify_modbus_commands(
first_178,
second_178,
)
if reg == 179:
matches = _deye_reg179_verify_match(expected_i, actual_i)
if not matches and reg in DEYE_TOU_POWER_REGS and inv_cfg is not None:
matches = _deye_tou_power_verify_match(expected_i, actual_i, inv_cfg)
@@ -861,7 +848,7 @@ async def verify_modbus_commands(
(
" (reg178 mask 0x%04X)" % REG178_VERIFY_MASK
if reg == 178
else (" (reg179 mask 0x%04X)" % REG179_MI_EXPORT_MASK if reg == 179 else "")
else ""
),
)
row_ac = await db.fetchrow(
@@ -1643,42 +1630,42 @@ async def write_inverter_setpoints(
(142, "limit_control", selling_mode),
(143, "", export_limit),
(145, "solar_sell", solar_sell),
(178, "grid_peak_shaving_switch", reg178_val),
]
)
if inv.deye_gen_microinverter_cutoff_enabled:
# Deye UI semantics: "MI export cutoff ENABLE" means export to grid is blocked (GEN effectively cut off).
# Therefore: want_cutoff=True -> ENABLE (3), want_cutoff=False -> DISABLE (2).
want_cutoff = bool(setpoints_now.deye_gen_cutoff_enabled) and deye_mode != "SELL"
target_bits = (
REG179_MI_EXPORT_ENABLE if want_cutoff else REG179_MI_EXPORT_DISABLE
# Reg 178: bitové pole. Nastavujeme bits45 (peak shaving) vždy; bits01 (MI export cutoff) jen pokud feature.
# Ostatní bity musí zůstat zachované → read-modify-write.
try:
mb178 = await get_modbus_client(inv.host, inv.port)
r178 = await mb178.read_holding_registers(178, 1, unit_id)
if not r178 or len(r178) < 1:
raise OSError(f"reg178 read returned {len(r178) if r178 is not None else None} values")
current_178 = int(r178[0])
peak_bits = int(reg178_val) & int(REG178_VERIFY_MASK)
if inv.deye_gen_microinverter_cutoff_enabled:
want_cutoff = bool(setpoints_now.deye_gen_cutoff_enabled) and deye_mode != "SELL"
mi_bits = (
REG178_MI_EXPORT_ENABLE if want_cutoff else REG178_MI_EXPORT_DISABLE
)
else:
mi_bits = int(current_178) & int(REG178_MI_EXPORT_MASK)
new_178 = (
(int(current_178) & ~int(REG178_VERIFY_MASK_COMBINED))
| int(peak_bits)
| int(mi_bits)
)
try:
mb179 = await get_modbus_client(inv.host, inv.port)
r179 = await mb179.read_holding_registers(179, 1, unit_id)
if r179 and len(r179) >= 1:
current_179 = int(r179[0])
# Deye firmware/UI u některých instalací neinterpretuje jen bits01 maskou,
# ale očekává přímo hodnotu 2/3. Proto zapisujeme "clean" 2/3 (bez RMW),
# aby se cutoff skutečně projevil i v UI.
registers.append((179, "control_board_special_1", int(target_bits)))
logger.info(
"[control] %s: reg179 MI cutoff %s (old=%s new=%s mask=0x%04X)",
inv.code,
"ON" if want_cutoff else "OFF",
current_179,
int(target_bits),
REG179_MI_EXPORT_MASK,
)
else:
logger.warning(
"[control] %s: reg179 read returned %s values, skip cutoff write",
inv.code,
len(r179) if r179 is not None else None,
)
except Exception as e:
logger.warning("[control] %s: reg179 cutoff RMW failed: %s", inv.code, e)
registers.append((178, "control_board_special_1", int(new_178)))
logger.info(
"[control] %s: reg178 (control_board_special_1) old=%s new=%s peak_bits=0x%04X mi_bits=%s",
inv.code,
current_178,
new_178,
int(peak_bits),
int(mi_bits),
)
except Exception as e:
logger.warning("[control] %s: reg178 RMW failed (skip reg178 write): %s", inv.code, e)
logger.info(
"[control] %s: deye_mode=%s charge=%sA discharge=%sA "
@@ -1803,13 +1790,11 @@ async def read_deye_registers_live(site_id: int, db: asyncpg.Connection) -> dict
b108 = await mb.read_holding_registers(108, 2)
b141 = await mb.read_holding_registers(141, 5)
r178 = await mb.read_holding_registers(178, 1)
r179 = await mb.read_holding_registers(179, 1)
r191 = await mb.read_holding_registers(191, 1)
r108, r109 = b108[0], b108[1]
r141, r142, r143 = b141[0], b141[1], b141[2]
r145 = b141[4]
r178 = r178[0]
r179 = r179[0]
r191 = r191[0]
except Exception:
logger.exception("read_deye_registers_live site=%s failed", site_id)
@@ -1823,9 +1808,9 @@ async def read_deye_registers_live(site_id: int, db: asyncpg.Connection) -> dict
"reg143_export_limit_w": int(r143),
"reg145_solar_sell": int(r145),
"reg178_peak_shaving_switch": int(r178),
"reg179_control_board_special_1": int(r179),
"reg179_mi_export_cutoff_bits": int(r179) & int(REG179_MI_EXPORT_MASK),
"reg179_mi_export_cutoff_is_on": (int(r179) & int(REG179_MI_EXPORT_MASK)) == int(REG179_MI_EXPORT_ENABLE),
"reg178_control_board_special_1": int(r178),
"reg178_mi_export_cutoff_bits": int(r178) & int(REG178_MI_EXPORT_MASK),
"reg178_mi_export_cutoff_is_on": (int(r178) & int(REG178_MI_EXPORT_MASK)) == int(REG178_MI_EXPORT_ENABLE),
"reg191_peak_shaving_w": int(r191),
"read_at": read_at.isoformat(),
}