From 1e7b7dfce8d595796eff718b1321b150b00363dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E6=B1=82=E5=9C=A3=E5=89=91?= Date: Wed, 4 Feb 2026 10:24:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9C=A8=E7=A6=BB=E7=BA=BF?= =?UTF-8?q?=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom_components/jackery/sensor.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/custom_components/jackery/sensor.py b/custom_components/jackery/sensor.py index 2d42d3c..53abc43 100644 --- a/custom_components/jackery/sensor.py +++ b/custom_components/jackery/sensor.py @@ -264,6 +264,7 @@ class JackeryDataCoordinator: self._sensors = {} # {sensor_id: entity} self._data_task = None self._subscribed = False + self._last_update_time = time.time() self._known_plugs = set() # Set of known plug SNs self.add_entities_callback = None # Callback to add new entities @@ -331,6 +332,7 @@ class JackeryDataCoordinator: def _handle_message(self, msg) -> None: """处理接收到的 MQTT 消息.""" + self._last_update_time = time.time() try: topic = msg.topic payload = msg.payload @@ -685,6 +687,13 @@ class JackeryDataCoordinator: for sensor_id, entity in self._sensors.items(): entity._update_from_coordinator(data) + def _mark_all_offline(self) -> None: + """Mark all entities as unavailable.""" + for entity in self._sensors.values(): + if entity.available: + entity._attr_available = False + entity.async_write_ha_state() + async def _periodic_data_request(self) -> None: """定期发送 'type: 25' 和 'type: 100' 指令.""" _LOGGER.info(f"Starting periodic data polling for {self._device_sn} via {self._mqtt_host}...") @@ -692,6 +701,9 @@ class JackeryDataCoordinator: while True: try: + if time.time() - self._last_update_time > 60: + self._mark_all_offline() + if not self._device_sn: _LOGGER.debug("Waiting for device SN discovery...") await asyncio.sleep(5)