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.
This commit is contained in:
不求圣剑
2025-11-18 15:18:56 +08:00
parent 679fc4bd77
commit 9f331f9654
2 changed files with 34 additions and 2 deletions

30
AGENTS.md Normal file
View File

@@ -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 Assistants naming pattern: `test_<feature>.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.

View File

@@ -207,8 +207,10 @@ class JackeryHomeSensor(SensorEntity):
self._data_task = None self._data_task = None
self._device_sn = "" # 设备序列号(从 LWT 消息中获取) self._device_sn = "" # 设备序列号(从 LWT 消息中获取)
# 获取 meter_sn对于功率传感器使用对应的 _power 键 # 获取 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) 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"]: elif sensor_id in ["battery_charge", "battery_discharge"]:
self._meter_sn = METER_SN_MAP.get("battery_charge_power", 0) self._meter_sn = METER_SN_MAP.get("battery_charge_power", 0)
else: else:
@@ -441,7 +443,7 @@ class JackeryHomeSensor(SensorEntity):
request_data = self._construct_data_get_request() 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( await ha_mqtt.async_publish(
self.hass, self.hass,
topic, topic,