更新返回数据结构

This commit is contained in:
不求圣剑
2026-01-23 10:38:04 +08:00
parent 7c3eae1f3b
commit a749efe544

View File

@@ -210,14 +210,15 @@ SENSORS = {
class JackeryDataCoordinator: class JackeryDataCoordinator:
"""协调器管理MQTT订阅和数据获取供所有传感器实体共享使用.""" """协调器管理MQTT订阅和数据获取供所有传感器实体共享使用."""
def __init__(self, hass: HomeAssistant, topic_prefix: str, token: str) -> None: def __init__(self, hass: HomeAssistant, topic_prefix: str, token: str, mqtt_host: str, device_sn: str) -> None:
"""初始化协调器.""" """初始化协调器."""
self.hass = hass self.hass = hass
self._topic_prefix = topic_prefix self._topic_prefix = topic_prefix
self._token = token self._token = token
self._topic_root = "hb" self._mqtt_host = mqtt_host
self._device_sn = device_sn
self._topic_root = topic_prefix
self._device_sn = "T02601220110001" # 设备序列号
self._sensors = {} # {sensor_id: entity} self._sensors = {} # {sensor_id: entity}
self._data_task = None self._data_task = None
self._subscribed = False self._subscribed = False
@@ -279,8 +280,8 @@ class JackeryDataCoordinator:
if isinstance(payload, bytes): if isinstance(payload, bytes):
payload = payload.decode("utf-8") payload = payload.decode("utf-8")
# Extract device SN from topic: hb/device/{sn}/status # Extract device SN from topic: {prefix}/device/{sn}/status
match = re.search(r"hb/device/([^/]+)/status", topic) match = re.search(rf"{self._topic_root}/device/([^/]+)/status", topic)
if match: if match:
sn = match.group(1) sn = match.group(1)
if not self._device_sn: if not self._device_sn:
@@ -292,6 +293,9 @@ class JackeryDataCoordinator:
# Parse Payload # Parse Payload
try: try:
data = json.loads(payload) data = json.loads(payload)
# 如果数据在 body 字段中,则提取 body
if "body" in data and isinstance(data["body"], dict):
data = data["body"]
except json.JSONDecodeError: except json.JSONDecodeError:
_LOGGER.warning(f"Invalid JSON payload on {topic}") _LOGGER.warning(f"Invalid JSON payload on {topic}")
return return
@@ -311,7 +315,7 @@ class JackeryDataCoordinator:
async def _periodic_data_request(self) -> None: async def _periodic_data_request(self) -> None:
"""定期发送 'type: 25' 指令请求全量数据.""" """定期发送 'type: 25' 指令请求全量数据."""
_LOGGER.info("Starting periodic data polling...") _LOGGER.info(f"Starting periodic data polling for {self._device_sn} via {self._mqtt_host}...")
await asyncio.sleep(2) await asyncio.sleep(2)
while True: while True:
@@ -361,8 +365,10 @@ async def async_setup_entry(
config = config_entry.data config = config_entry.data
topic_prefix = config.get("topic_prefix", "hb") topic_prefix = config.get("topic_prefix", "hb")
token = config.get("token") token = config.get("token")
mqtt_host = config.get("mqtt_host")
device_sn = config.get("device_sn")
coordinator = JackeryDataCoordinator(hass, topic_prefix, token) coordinator = JackeryDataCoordinator(hass, topic_prefix, token, mqtt_host, device_sn)
hass.data[DOMAIN][config_entry.entry_id]["coordinator"] = coordinator hass.data[DOMAIN][config_entry.entry_id]["coordinator"] = coordinator
entities = [] entities = []
@@ -434,15 +440,14 @@ class JackerySensor(SensorEntity):
elif self._sensor_id == "battery_soc": elif self._sensor_id == "battery_soc":
self._attr_native_value = value self._attr_native_value = value
elif self._sensor_id.startswith("solar_power_pv") and isinstance(value, dict): elif self._sensor_id.startswith("solar_power_pv") and isinstance(value, dict):
# Handle dictionary for PV if it occurs, trying to find common value keys # Handle dictionary for PV if it occurs
# Assumption based on "PV1发电总功率" -> it might contain power if "pvPw" in value:
if "w" in value: self._attr_native_value = value["pvPw"]
elif "w" in value:
self._attr_native_value = value["w"] self._attr_native_value = value["w"]
elif "power" in value: elif "power" in value:
self._attr_native_value = value["power"] self._attr_native_value = value["power"]
else: else:
# Fallback: display raw dict as string or extract first numeric value?
# Using str(value) for safety if structure is unknown
self._attr_native_value = str(value) self._attr_native_value = str(value)
else: else:
self._attr_native_value = value self._attr_native_value = value