更新数据格式
This commit is contained in:
@@ -3,11 +3,10 @@ import logging
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
|
from homeassistant.components import mqtt
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
from homeassistant.components import mqtt
|
|
||||||
|
|
||||||
from . import DOMAIN
|
from . import DOMAIN
|
||||||
|
|
||||||
@@ -16,7 +15,7 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
# 配置数据模式
|
# 配置数据模式
|
||||||
DATA_SCHEMA = vol.Schema(
|
DATA_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Required("mqtt_host"): str,
|
|
||||||
vol.Required("device_sn"): str,
|
vol.Required("device_sn"): str,
|
||||||
vol.Required("token"): str,
|
vol.Required("token"): str,
|
||||||
vol.Optional(
|
vol.Optional(
|
||||||
@@ -69,4 +68,3 @@ class JackeryConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult:
|
async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult:
|
||||||
"""Import a config entry from configuration.yaml."""
|
"""Import a config entry from configuration.yaml."""
|
||||||
return await self.async_step_user(import_config)
|
return await self.async_step_user(import_config)
|
||||||
|
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ SENSORS = {
|
|||||||
"state_class": SensorStateClass.MEASUREMENT,
|
"state_class": SensorStateClass.MEASUREMENT,
|
||||||
},
|
},
|
||||||
"grid_export_power": { # System -> Grid/Home (inOngirdPw)
|
"grid_export_power": { # System -> Grid/Home (inOngirdPw)
|
||||||
"json_key": "inOngirdPw",
|
"json_key": "inOngridPw",
|
||||||
"name": "Grid Export Power",
|
"name": "Grid Export Power",
|
||||||
"unit": UnitOfPower.WATT,
|
"unit": UnitOfPower.WATT,
|
||||||
"icon": "mdi:transmission-tower-export",
|
"icon": "mdi:transmission-tower-export",
|
||||||
@@ -308,10 +308,7 @@ class JackeryDataCoordinator:
|
|||||||
def _distribute_data(self, data: dict) -> None:
|
def _distribute_data(self, data: dict) -> None:
|
||||||
"""分发数据给传感器."""
|
"""分发数据给传感器."""
|
||||||
for sensor_id, entity in self._sensors.items():
|
for sensor_id, entity in self._sensors.items():
|
||||||
json_key = SENSORS[sensor_id].get("json_key")
|
entity._update_from_coordinator(data)
|
||||||
if json_key and json_key in data:
|
|
||||||
raw_value = data[json_key]
|
|
||||||
entity._update_from_coordinator(raw_value)
|
|
||||||
|
|
||||||
async def _periodic_data_request(self) -> None:
|
async def _periodic_data_request(self) -> None:
|
||||||
"""定期发送 'type: 25' 指令请求全量数据."""
|
"""定期发送 'type: 25' 指令请求全量数据."""
|
||||||
@@ -428,8 +425,23 @@ class JackerySensor(SensorEntity):
|
|||||||
self._coordinator.unregister_sensor(self._sensor_id)
|
self._coordinator.unregister_sensor(self._sensor_id)
|
||||||
await super().async_will_remove_from_hass()
|
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."""
|
"""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
|
# Process specific conversions
|
||||||
if self._sensor_id == "battery_temperature":
|
if self._sensor_id == "battery_temperature":
|
||||||
# cellTemp is 0.1 C
|
# cellTemp is 0.1 C
|
||||||
|
|||||||
Reference in New Issue
Block a user