From d4e28a8bfb98c01813919b94d6a37e1cd9f19655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E6=B1=82=E5=9C=A3=E5=89=91?= Date: Fri, 23 Jan 2026 11:02:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=95=B0=E6=8D=AE=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom_components/jackery/config_flow.py | 10 ++++------ custom_components/jackery/sensor.py | 24 ++++++++++++++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/custom_components/jackery/config_flow.py b/custom_components/jackery/config_flow.py index d50b7b4..ddc6ca3 100644 --- a/custom_components/jackery/config_flow.py +++ b/custom_components/jackery/config_flow.py @@ -3,11 +3,10 @@ import logging from typing import Any import voluptuous as vol - from homeassistant import config_entries +from homeassistant.components import mqtt from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResult -from homeassistant.components import mqtt from . import DOMAIN @@ -16,7 +15,7 @@ _LOGGER = logging.getLogger(__name__) # 配置数据模式 DATA_SCHEMA = vol.Schema( { - vol.Required("mqtt_host"): str, + vol.Required("device_sn"): str, vol.Required("token"): str, vol.Optional( @@ -38,7 +37,7 @@ class JackeryConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Handle the initial step.""" if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") - + errors = {} if user_input is not None: @@ -51,7 +50,7 @@ class JackeryConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): f"device_sn: {user_input.get('device_sn')}, " f"topic_prefix: {user_input.get('topic_prefix', 'hb')}" ) - + return self.async_create_entry( title="Jackery", data=user_input, @@ -69,4 +68,3 @@ class JackeryConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult: """Import a config entry from configuration.yaml.""" return await self.async_step_user(import_config) - diff --git a/custom_components/jackery/sensor.py b/custom_components/jackery/sensor.py index d10799d..9918b30 100644 --- a/custom_components/jackery/sensor.py +++ b/custom_components/jackery/sensor.py @@ -121,7 +121,7 @@ SENSORS = { "state_class": SensorStateClass.MEASUREMENT, }, "grid_export_power": { # System -> Grid/Home (inOngirdPw) - "json_key": "inOngirdPw", + "json_key": "inOngridPw", "name": "Grid Export Power", "unit": UnitOfPower.WATT, "icon": "mdi:transmission-tower-export", @@ -308,10 +308,7 @@ class JackeryDataCoordinator: def _distribute_data(self, data: dict) -> None: """分发数据给传感器.""" for sensor_id, entity in self._sensors.items(): - json_key = SENSORS[sensor_id].get("json_key") - if json_key and json_key in data: - raw_value = data[json_key] - entity._update_from_coordinator(raw_value) + entity._update_from_coordinator(data) async def _periodic_data_request(self) -> None: """定期发送 'type: 25' 指令请求全量数据.""" @@ -428,8 +425,23 @@ class JackerySensor(SensorEntity): self._coordinator.unregister_sensor(self._sensor_id) await super().async_will_remove_from_hass() - def _update_from_coordinator(self, value: Any) -> None: + def _update_from_coordinator(self, data: dict) -> None: """Receive data from coordinator.""" + # Special handling for EPS Output Power (Bidirectional) + if self._sensor_id == "eps_output_power": + out_p = float(data.get("swEpsOutPw", 0)) + in_p = float(data.get("swEpsInPw", 0)) + self._attr_native_value = out_p - in_p + self._attr_available = True + self.async_write_ha_state() + return + + json_key = self._config.get("json_key") + if not json_key or json_key not in data: + return + + value = data[json_key] + # Process specific conversions if self._sensor_id == "battery_temperature": # cellTemp is 0.1 C