From c596db47bf3438dbbe15a1b15d535fa3bf3124af Mon Sep 17 00:00:00 2001 From: eigger Date: Tue, 1 Sep 2026 15:55:50 +0900 Subject: [PATCH] feat(parking): persist request counter and 0x41/0x40 payload phases Co-authored-by: Cursor --- config.yaml | 18 ++++++++++----- scripts/validate.py | 54 ++++++++++++++++++++++++++++++++++++++++++--- templates.yaml | 15 ++++++++----- 3 files changed, 73 insertions(+), 14 deletions(-) diff --git a/config.yaml b/config.yaml index 05d5e28..1ff9d30 100644 --- a/config.yaml +++ b/config.yaml @@ -102,7 +102,8 @@ devices: # decode: { offset: 18, length: 0, type: string } # Parking beacon — instance_mode: shared → MAC 무관, manufacturer 0x035D 광고가 하나의 엔티티 공유 - # payload 14바이트: [0]=04(응답타입), [1..6]=KeyMAC, [7..10]=월·일·시·분, [11]=층(256-raw), [12..13]=구역코드 + # 요청 6바이트: [0]=02 [1]=05 [2]=00 [3]=battery [4]=state [5]=counter + # 응답 14바이트: [0]=04(응답타입), [1..6]=KeyMAC, [7..10]=월·일·시·분, [11]=층(256-raw), [12..13]=구역코드 # 04xx: 주차위치 응답 비콘, 07xx: SmartKey 인증(22B 암호화), 02xx: 출입 이벤트(6B) — prefix "04" 로 응답만 매칭 - id: parking_beacon name: "Parking Beacon" @@ -114,13 +115,18 @@ devices: manufacturer_min_length: 14 # 최소 14바이트 advertise: manufacturer_id: 861 # 0x035D - payload: "0205005541{counter:02X}" # 02 + 050055(기기코드) + 41(주차요청) + counter - counter_mode: reset # reset(기본) | persist + # 02 05 00 | battery 100% | {state} | {counter} — 카운터는 버튼마다 +1, 버스트 중 고정 + payload: "02050064{state:02X}{counter:02X}" + counter_mode: persist # 요청마다 누적. 0xFF 다음은 0 counter_start: 0 - mode: balanced + mode: low_latency # ~100ms → press 200ms 동안 약 1~2패킷 tx_power: high - timeout: 15s - repeat_interval: 1s + timeout: 4s # 페이즈 합(3.2s)보다 약간 큰 상한 + payload_phases: + - state: 65 # 0x41 버튼 누름 + ACK 대기 + duration: 200ms + - state: 64 # 0x40 홀드 + ACK 대기 + duration: 3s stop_on_response: true controls: - key: request_location diff --git a/scripts/validate.py b/scripts/validate.py index c683247..227e6c1 100644 --- a/scripts/validate.py +++ b/scripts/validate.py @@ -54,9 +54,12 @@ KNOWN_OBD_KEYS = { } KNOWN_ADVERTISE_KEYS = { "manufacturer_id", "payload", "counter_mode", "counter_start", "mode", - "tx_power", "timeout", "repeat_interval", "stop_on_response", + "tx_power", "timeout", "repeat_interval", "payload_phases", "stop_on_response", "connectable", "scannable", "include_device_name" } +KNOWN_ADVERTISE_PHASE_KEYS = { + "state", "duration", "payload" +} KNOWN_SENSOR_KEYS = { "key", "name", "platform", "device_class", "unit", "state_class", "icon", "entity_category", "accuracy_decimals", "source_field", "length", @@ -216,8 +219,22 @@ def validate_sensor(sensor: Any, device_source: str, path: str, res: ValidationR validate_publish(sensor["publish"], f"{path}.publish", res) +_PAYLOAD_TOKEN_RE = re.compile(r"\{(counter|state)(?::[^}]+)?\}") +_DURATION_RE = re.compile(r"^\d+(ms|s|m|h)$") + + +def _payload_has_state_token(payload: str) -> bool: + return any(m.group(1) == "state" for m in _PAYLOAD_TOKEN_RE.finditer(payload)) + + def validate_advertise_payload(payload: str) -> Optional[str]: - clean = payload.replace("{counter}", "0").replace("{counter:02X}", "00").replace("{counter:02x}", "00") + def subst(m: re.Match) -> str: + token = m.group(0) + if ":02X" in token or ":02x" in token: + return "00" + return "0" + + clean = _PAYLOAD_TOKEN_RE.sub(subst, payload) if not re.match(r"^[0-9A-Fa-f]*$", clean): return f"payload contains invalid hex characters: '{payload}'" if len(clean) % 2 != 0: @@ -274,9 +291,40 @@ def validate_advertise(adv: Any, device: dict, path: str, res: ValidationResult) for dur_field in ["timeout", "repeat_interval"]: if dur_field in adv: val = adv[dur_field] - if val is not None and (not isinstance(val, str) or not re.match(r"^\d+(ms|s|m|h)$", str(val).strip())): + if val is not None and (not isinstance(val, str) or not _DURATION_RE.match(str(val).strip())): res.error(f"{path}.{dur_field}", f"Invalid duration '{val}'. Example: 500ms, 15s, 1m") + phases = adv.get("payload_phases", []) + if phases: + if not isinstance(phases, list): + res.error(f"{path}.payload_phases", "payload_phases must be a list") + else: + if adv.get("repeat_interval"): + res.warning(f"{path}.repeat_interval", "repeat_interval is ignored when payload_phases is set — counter stays fixed across phases") + for i, phase in enumerate(phases): + ppath = f"{path}.payload_phases[{i}]" + if not isinstance(phase, dict): + res.error(ppath, "payload_phases item must be an object") + continue + check_unknown_keys(phase, KNOWN_ADVERTISE_PHASE_KEYS, ppath, res) + duration = phase.get("duration") + if not isinstance(duration, str) or not _DURATION_RE.match(duration.strip()): + res.error(f"{ppath}.duration", f"Invalid duration '{duration}'. Example: 200ms, 3s") + state = phase.get("state") + if state is not None and (not isinstance(state, int) or state < 0 or state > 255): + res.error(f"{ppath}.state", f"state must be between 0 and 255 (got '{state}')") + template = phase["payload"] if isinstance(phase.get("payload"), str) and phase["payload"].strip() else adv.get("payload", "") + if isinstance(template, str): + if _payload_has_state_token(template) and state is None: + res.error(f"{ppath}.state", "payload_phases item has a {state} token but no state value") + err = validate_advertise_payload(template) + if err: + res.error(ppath, err) + + payload = adv.get("payload") + if isinstance(payload, str) and _payload_has_state_token(payload) and not phases: + res.error(f"{path}.payload", "payload contains {state} but advertise.payload_phases is empty") + for bool_field in ["stop_on_response", "connectable", "scannable", "include_device_name"]: if bool_field in adv and not isinstance(adv[bool_field], bool): res.error(f"{path}.{bool_field}", f"{bool_field} must be a boolean (true/false)") diff --git a/templates.yaml b/templates.yaml index 7e54bb6..554b9e8 100644 --- a/templates.yaml +++ b/templates.yaml @@ -50,13 +50,18 @@ templates: manufacturer_min_length: 14 # 포맷(1B) + 스마트키 MAC(6B) + 시간(4B) + 층(1B) + 구역(2B) = 14바이트 advertise: manufacturer_id: 861 # 0x035D - payload: "0205005541{counter:02X}" # 02 + 050055(기기코드) + 41(주차요청) + counter - counter_mode: reset # reset(기본) | persist + # 02 05 00 | battery 100% | {state} | {counter} — 카운터는 버튼마다 +1, 버스트 중 고정 + payload: "02050064{state:02X}{counter:02X}" + counter_mode: persist # 요청마다 누적. 0xFF 다음은 0 counter_start: 0 - mode: balanced + mode: low_latency # ~100ms → press 200ms 동안 약 1~2패킷 tx_power: high - timeout: 15s - repeat_interval: 1s + timeout: 4s # 페이즈 합(3.2s)보다 약간 큰 상한 + payload_phases: + - state: 65 # 0x41 버튼 누름 + ACK 대기 + duration: 200ms + - state: 64 # 0x40 홀드 + ACK 대기 + duration: 3s stop_on_response: true controls: - key: request_location