From 9f331f96547045f1e9b3087c2470409d7782c939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E6=B1=82=E5=9C=A3=E5=89=91?= Date: Tue, 18 Nov 2025 15:18:56 +0800 Subject: [PATCH] fix: refine meter serial number mapping and data topic handling in JackeryHome sensor - Updated meter serial number mapping logic to explicitly handle "grid_import" and "grid_export" sensor IDs. - Simplified data topic assignment by removing unnecessary conditional checks. --- AGENTS.md | 30 +++++++++++++++++++++++++ custom_components/JackeryHome/sensor.py | 6 +++-- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 AGENTS.md diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..3ea4c51 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,30 @@ +# Repository Guidelines + +## Project Structure & Module Organization +- Energy simulator lives at `main.py`; MQTT examples under `data_transmission_example.py`. +- Home Assistant integration is in `custom_components/JackeryHome/` with `__init__.py`, `sensor.py`, `config_flow.py`, translations, and docs. +- Branding assets sit in `brands/`; release helpers and docs (e.g., `prepare_release.sh`, `README.md`, `energy_flow_card_config.yaml`) are at the repo root. +- Tests currently consist of targeted scripts such as `test_mqtt.py`; add new suites beside related modules. + +## Build, Test, and Development Commands +- `uv sync` — install the Python toolchain defined in `pyproject.toml` (uses uv for fast, reproducible envs). +- `uv run main.py` — run the MQTT simulator against the broker configured inside the script. +- `python test_mqtt.py` — quick publishing/subscription sanity check for MQTT topics. +- `./prepare_release.sh` — bump integration metadata and prep a tagged release; review the script before running. + +## Coding Style & Naming Conventions +- Python source follows 4-space indentation, snake_case identifiers, and descriptive constants (e.g., `MQTT_BROKER`). +- Keep Home Assistant entity IDs lowercase with underscores (`sensor.solar_power`). +- Maintain docstrings or top-of-file comments for modules that expose user-facing behavior; prefer concise inline comments for non-obvious logic. +- JSON/YAML assets should stay UTF-8, two-space indented, with trailing commas avoided. + +## Testing Guidelines +- Favor lightweight integration tests that exercise MQTT flows end-to-end (publish via simulator, assert consumption by HA sensors). +- Mirror Home Assistant’s naming pattern: `test_.py` with `async` helpers where applicable. +- Run tests locally before opening a PR; when adding new sensors, include topic fixtures and expected payload assertions. + +## Commit & Pull Request Guidelines +- Commits typically use an imperative summary (e.g., "Add inverter sensor mapping") followed by focused changes. +- Reference relevant issues in the body (`Fixes #42`) and keep commits scoped so they are reviewable. +- Pull requests should describe motivation, outline testing performed (`uv run main.py`, HA log screenshots), and mention any config migrations. +- Include UI screenshots/GIFs when altering Lovelace card guidance or other user-facing docs. diff --git a/custom_components/JackeryHome/sensor.py b/custom_components/JackeryHome/sensor.py index 46b9831..f1d9a0d 100644 --- a/custom_components/JackeryHome/sensor.py +++ b/custom_components/JackeryHome/sensor.py @@ -207,8 +207,10 @@ class JackeryHomeSensor(SensorEntity): self._data_task = None self._device_sn = "" # 设备序列号(从 LWT 消息中获取) # 获取 meter_sn,对于功率传感器,使用对应的 _power 键 - if sensor_id in ["grid_import", "grid_export"]: + if sensor_id == "grid_import": self._meter_sn = METER_SN_MAP.get("grid_import_power", 0) + elif sensor_id == "grid_export": + self._meter_sn = METER_SN_MAP.get("grid_export_power", 0) elif sensor_id in ["battery_charge", "battery_discharge"]: self._meter_sn = METER_SN_MAP.get("battery_charge_power", 0) else: @@ -441,7 +443,7 @@ class JackeryHomeSensor(SensorEntity): request_data = self._construct_data_get_request() # 发送数据获取请求 - topic = f"{self._data_get_topic}" if self._device_sn else self._data_get_topic + topic = self._data_get_topic await ha_mqtt.async_publish( self.hass, topic,