Enhance availability checks of Lunatone entities (#186138)

This commit is contained in:
MoonDevLT
2026-10-14 15:00:53 +02:00
committed by GitHub
parent c812dadc8f
commit e6aaffc96b
6 changed files with 158 additions and 18 deletions
@@ -74,6 +74,13 @@ class LunatoneDALIScanStatus(
identifiers={(DOMAIN, device_unique_id)},
)
@property
@override
def available(self) -> bool:
return super().available and (
self._line is None or self._line in self.coordinator.line_scan_status
)
@property
@override
def is_on(self) -> bool:
+8 -6
View File
@@ -1,10 +1,10 @@
"""Platform for Lunatone light integration."""
import contextlib
from typing import Any, override
import aiohttp
from lunatone_rest_api_client import DALIBroadcast
from lunatone_rest_api_client.models import LineStatus
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
@@ -116,13 +116,15 @@ class LunatoneLight(
@override
def available(self) -> bool:
"""Return True if entity is available."""
return super().available and self._device is not None
return super().available and self._device_id in self.coordinator.data.get(
self._line_id, {}
)
@property
@override
def is_on(self) -> bool:
"""Return True if light is on."""
return self._device is not None and self._device.is_on
return self._device.is_on
@property
@override
@@ -187,7 +189,8 @@ class LunatoneLight(
@override
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._device = self.coordinator.data[self._line_id][self._device_id]
with contextlib.suppress(KeyError):
self._device = self.coordinator.data[self._line_id][self._device_id]
self.async_write_ha_state()
@override
@@ -294,8 +297,7 @@ class LunatoneLineBroadcastLight(
super().available
and self._coordinator_info.last_update_success
and line_id is not None
and str(line_id) in info_data.lines
and info_data.lines[str(line_id)].line_status == LineStatus.OK
and info_data.is_line_status_ok(line_id)
)
@property
@@ -216,6 +216,11 @@ class LunatoneDALILineStatusSensor(
)
self._attr_unique_id = f"{line_unique_id}-status"
@property
@override
def available(self) -> bool:
return super().available and self._line_id in self.coordinator.data.lines
@property
@override
def native_value(self) -> str:
@@ -9,7 +9,7 @@ from lunatone_rest_api_client.models import ScanLineState, ScanState
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.lunatone.coordinator import DEFAULT_SCAN_UPDATE_INTERVAL
from homeassistant.const import STATE_OFF, STATE_ON, Platform
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
@@ -92,15 +92,17 @@ async def test_sensor_value_update(
assert coordinator.update_interval == timedelta(seconds=10)
async def test_line_scan_status_off_when_line_data_is_missing(
async def test_line_scan_status_availability_updates_with_line_data(
hass: HomeAssistant,
mock_lunatone_info: AsyncMock,
mock_lunatone_devices: AsyncMock,
mock_lunatone_sensors: AsyncMock,
mock_lunatone_scan: AsyncMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test the Lunatone DALI scan status value update."""
"""Test the availability of line scan status sensors."""
scan_data_with_lines = deepcopy(mock_lunatone_scan.data)
mock_lunatone_scan.data.lines = []
mock_lunatone_scan.data.status = ScanState.ADDRESSING
@@ -108,6 +110,22 @@ async def test_line_scan_status_off_when_line_data_is_missing(
entity_ids = (
("binary_sensor.test_scan_status", STATE_ON),
("binary_sensor.dali_line_0_scan_status", STATE_UNAVAILABLE),
("binary_sensor.dali_line_1_scan_status", STATE_UNAVAILABLE),
)
for entity_id, expected_state in entity_ids:
entity = hass.states.get(entity_id)
assert entity and entity.state == expected_state
mock_lunatone_scan.data = scan_data_with_lines
freezer.tick(DEFAULT_SCAN_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
entity_ids = (
("binary_sensor.test_scan_status", STATE_OFF),
("binary_sensor.dali_line_0_scan_status", STATE_OFF),
("binary_sensor.dali_line_1_scan_status", STATE_OFF),
)
+66 -7
View File
@@ -1,6 +1,6 @@
"""Tests for the Lunatone integration."""
import copy
from copy import deepcopy
from unittest.mock import AsyncMock
import aiohttp
@@ -23,6 +23,7 @@ from homeassistant.const import (
SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
STATE_UNAVAILABLE,
Platform,
)
from homeassistant.core import HomeAssistant
@@ -239,7 +240,7 @@ async def test_turn_on_off_broadcast(
state = hass.states.get(entity_id)
assert state
assert state.state == "on"
assert state.state == STATE_ON
await hass.services.async_call(
LIGHT_DOMAIN,
@@ -263,7 +264,7 @@ async def test_turn_on_off_broadcast(
state = hass.states.get(entity_id)
assert state
assert state.state == "off"
assert state.state == STATE_OFF
async def test_turn_on_off_broadcast_with_connection_error(
@@ -325,19 +326,20 @@ async def test_line_broadcast_available_status(
"""Test if the broadcast light is available."""
line_id = str(mock_lunatone_dali_broadcast.line)
entity_id = f"light.dali_line_{line_id}"
line_status = iter((LineStatus.NOT_REACHABLE, LineStatus.OK))
await setup_integration(hass, mock_config_entry)
async def fake_update():
info_data = copy.deepcopy(mock_lunatone_info.data)
info_data.lines[line_id].line_status = LineStatus.NOT_REACHABLE
info_data = deepcopy(mock_lunatone_info.data)
info_data.lines[line_id].line_status = next(line_status)
mock_lunatone_info.data = info_data
mock_lunatone_info.async_update.side_effect = fake_update
state = hass.states.get(entity_id)
assert state
assert state.state != "unavailable"
assert state.state != STATE_UNAVAILABLE
freezer.tick(DEFAULT_INFO_UPDATE_INTERVAL)
async_fire_time_changed(hass)
@@ -345,7 +347,64 @@ async def test_line_broadcast_available_status(
state = hass.states.get(entity_id)
assert state
assert state.state == "unavailable"
assert state.state == STATE_UNAVAILABLE
freezer.tick(DEFAULT_INFO_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
state = hass.states.get(entity_id)
assert state
assert state.state != STATE_UNAVAILABLE
async def test_line_broadcast_available_status_with_line_missing(
hass: HomeAssistant,
mock_lunatone_info: AsyncMock,
mock_lunatone_devices: AsyncMock,
mock_lunatone_sensors: AsyncMock,
mock_lunatone_scan: AsyncMock,
mock_lunatone_dali_broadcast: AsyncMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test if the broadcast light is available."""
line_id = "0"
entity_id = f"light.dali_line_{line_id}"
await setup_integration(hass, mock_config_entry)
line_data = deepcopy(mock_lunatone_info.data.lines[line_id])
async def fake_update():
info_data = deepcopy(mock_lunatone_info.data)
if line_id in info_data.lines:
del info_data.lines[line_id]
else:
info_data.lines[line_id] = line_data
mock_lunatone_info.data = info_data
mock_lunatone_info.async_update.side_effect = fake_update
state = hass.states.get(entity_id)
assert state
assert state.state != STATE_UNAVAILABLE
freezer.tick(DEFAULT_INFO_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
state = hass.states.get(entity_id)
assert state
assert state.state == STATE_UNAVAILABLE
freezer.tick(DEFAULT_INFO_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
state = hass.states.get(entity_id)
assert state
assert state.state != STATE_UNAVAILABLE
@pytest.mark.parametrize(
+51 -2
View File
@@ -9,7 +9,8 @@ from lunatone_rest_api_client.models import LineStatus
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.const import Platform
from homeassistant.components.lunatone.coordinator import DEFAULT_INFO_UPDATE_INTERVAL
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
@@ -72,7 +73,7 @@ async def test_sensor_value_update(
entity = hass.states.get(entity_id)
assert entity
assert entity.state == "unknown"
assert entity.state == STATE_UNKNOWN
freezer.tick(timedelta(seconds=40))
async_fire_time_changed(hass)
@@ -118,3 +119,51 @@ async def test_dali_line_status_value_update(
entity = hass.states.get(entity_id)
assert entity
assert entity.state == "ok"
async def test_dali_line_status_availability(
hass: HomeAssistant,
mock_lunatone_info: AsyncMock,
mock_lunatone_devices: AsyncMock,
mock_lunatone_sensors: AsyncMock,
mock_lunatone_scan: AsyncMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test the Lunatone DALI line status sensor availability."""
line_id = "0"
entity_id = f"sensor.dali_line_{line_id}_status"
await setup_integration(hass, mock_config_entry)
line_data = deepcopy(mock_lunatone_info.data.lines[line_id])
async def fake_update() -> None:
info_data = deepcopy(mock_lunatone_info.data)
if line_id in info_data.lines:
del info_data.lines[line_id]
else:
info_data.lines[line_id] = line_data
mock_lunatone_info.data = info_data
mock_lunatone_info.async_update.side_effect = fake_update
entity = hass.states.get(entity_id)
assert entity
assert entity.state != STATE_UNAVAILABLE
freezer.tick(DEFAULT_INFO_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
entity = hass.states.get(entity_id)
assert entity
assert entity.state == STATE_UNAVAILABLE
freezer.tick(DEFAULT_INFO_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
entity = hass.states.get(entity_id)
assert entity
assert entity.state != STATE_UNAVAILABLE