Fix Acaia battery sensor going unavailable on first-session disconnect (#169420)

Co-authored-by: Josef Zweck <josef@zweck.dev>
This commit is contained in:
Andrew Ng
2026-04-29 18:12:44 +08:00
committed by GitHub
parent f4637db26d
commit 32743fcf8d
2 changed files with 29 additions and 1 deletions
+1 -1
View File
@@ -143,4 +143,4 @@ class AcaiaRestoreSensor(AcaiaEntity, RestoreSensor):
@property
def available(self) -> bool:
"""Return True if entity is available."""
return super().available or self._restored_data is not None
return super().available or self.native_value is not None
+28
View File
@@ -1,7 +1,9 @@
"""Test sensors for acaia integration."""
from datetime import timedelta
from unittest.mock import MagicMock, patch
from freezegun.api import FrozenDateTimeFactory
from syrupy.assertion import SnapshotAssertion
from homeassistant.const import PERCENTAGE, Platform
@@ -12,6 +14,7 @@ from . import setup_integration
from tests.common import (
MockConfigEntry,
async_fire_time_changed,
mock_restore_cache_with_extra_data,
snapshot_platform,
)
@@ -61,3 +64,28 @@ async def test_restore_state(
state = hass.states.get(entity_id)
assert state
assert state.state == "65"
async def test_battery_available_within_session_after_disconnect(
hass: HomeAssistant,
mock_scale: MagicMock,
freezer: FrozenDateTimeFactory,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test battery stays available on disconnect when no restore data exists."""
entity_id = "sensor.lunar_ddeeff_battery"
await setup_integration(hass, mock_config_entry)
state = hass.states.get(entity_id)
assert state
assert state.state == "42"
mock_scale.connected = False
freezer.tick(timedelta(minutes=10))
async_fire_time_changed(hass)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state
assert state.state == "42"