Skip to content

Commit

Permalink
Mark FGLAir entities unavailable if they are reporting to be offline (h…
Browse files Browse the repository at this point in the history
  • Loading branch information
crevetor authored Jan 10, 2025
1 parent 246a9f9 commit 9388879
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
10 changes: 8 additions & 2 deletions homeassistant/components/fujitsu_fglair/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,16 @@ async def _async_update_data(self) -> dict[str, FujitsuHVAC]:
raise ConfigEntryAuthFailed("Credentials expired for Ayla IoT API") from e

if not listening_entities:
devices = [dev for dev in devices if isinstance(dev, FujitsuHVAC)]
devices = [
dev
for dev in devices
if isinstance(dev, FujitsuHVAC) and dev.is_online()
]
else:
devices = [
dev for dev in devices if dev.device_serial_number in listening_entities
dev
for dev in devices
if dev.device_serial_number in listening_entities and dev.is_online()
]

try:
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/components/fujitsu_fglair/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def __init__(self, coordinator: FGLairCoordinator, device: FujitsuHVAC) -> None:
sw_version=device.property_values["mcu_firmware_version"],
)

@property
def available(self) -> bool:
"""Return if entity is available."""
return super().available and self.coordinator_context in self.coordinator.data

@property
def device(self) -> FujitsuHVAC:
"""Return the device object from the coordinator data."""
Expand Down
21 changes: 21 additions & 0 deletions tests/components/fujitsu_fglair/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from freezegun.api import FrozenDateTimeFactory
import pytest

from homeassistant.components.climate import HVACMode
from homeassistant.components.fujitsu_fglair.const import (
API_REFRESH,
API_TIMEOUT,
Expand Down Expand Up @@ -124,6 +125,26 @@ async def test_device_auth_failure(
assert hass.states.get(entity_id(mock_devices[1])).state == STATE_UNAVAILABLE


async def test_device_offline(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_ayla_api: AsyncMock,
mock_config_entry: MockConfigEntry,
mock_devices: list[AsyncMock],
) -> None:
"""Test entities become unavailable if device if offline."""
await setup_integration(hass, mock_config_entry)

mock_ayla_api.async_get_devices.return_value[0].is_online.return_value = False

freezer.tick(API_REFRESH)
async_fire_time_changed(hass)
await hass.async_block_till_done()

assert hass.states.get(entity_id(mock_devices[0])).state == STATE_UNAVAILABLE
assert hass.states.get(entity_id(mock_devices[1])).state == HVACMode.COOL


async def test_token_expired(
hass: HomeAssistant,
mock_ayla_api: AsyncMock,
Expand Down

0 comments on commit 9388879

Please sign in to comment.