mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 12:21:04 -04:00
Fix certain device variants failing during setup of lunatone (#182396)
This commit is contained in:
@@ -11,6 +11,7 @@ from lunatone_rest_api_client import (
|
||||
Info,
|
||||
Sensors,
|
||||
)
|
||||
from lunatone_rest_api_client.models import DeviceCapabilities
|
||||
|
||||
from homeassistant.const import CONF_URL, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
@@ -142,14 +143,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: LunatoneConfigEntry) ->
|
||||
coordinator_devices = LunatoneDevicesDataUpdateCoordinator(hass, entry, devices_api)
|
||||
await coordinator_devices.async_config_entry_first_refresh()
|
||||
|
||||
sensors_api = Sensors(auth_api)
|
||||
coordinator_sensors = LunatoneSensorsDataUpdateCoordinator(hass, entry, sensors_api)
|
||||
await coordinator_sensors.async_config_entry_first_refresh()
|
||||
|
||||
dali_scan_api = DALIScan(auth_api)
|
||||
coordinator_scan = LunatoneScanDataUpdateCoordinator(hass, entry, dali_scan_api)
|
||||
await coordinator_scan.async_config_entry_first_refresh()
|
||||
|
||||
coordinator_sensors = None
|
||||
if DeviceCapabilities.SENSORS in info_api.data.device_capabilities:
|
||||
sensors_api = Sensors(auth_api)
|
||||
coordinator_sensors = LunatoneSensorsDataUpdateCoordinator(
|
||||
hass, entry, sensors_api
|
||||
)
|
||||
await coordinator_sensors.async_config_entry_first_refresh()
|
||||
|
||||
dali_line_broadcasts = [
|
||||
DALIBroadcast(auth_api, int(line)) for line in coordinator_info.data.lines
|
||||
]
|
||||
@@ -157,8 +162,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: LunatoneConfigEntry) ->
|
||||
entry.runtime_data = LunatoneData(
|
||||
coordinator_info,
|
||||
coordinator_devices,
|
||||
coordinator_sensors,
|
||||
coordinator_scan,
|
||||
coordinator_sensors,
|
||||
dali_line_broadcasts,
|
||||
)
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""Coordinator for handling data fetching and updates."""
|
||||
|
||||
from collections import defaultdict
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import override
|
||||
@@ -38,9 +38,9 @@ class LunatoneData:
|
||||
|
||||
coordinator_info: LunatoneInfoDataUpdateCoordinator
|
||||
coordinator_devices: LunatoneDevicesDataUpdateCoordinator
|
||||
coordinator_sensors: LunatoneSensorsDataUpdateCoordinator
|
||||
coordinator_scan: LunatoneScanDataUpdateCoordinator
|
||||
dali_line_broadcasts: list[DALIBroadcast]
|
||||
coordinator_sensors: LunatoneSensorsDataUpdateCoordinator | None = None
|
||||
dali_line_broadcasts: list[DALIBroadcast] = field(default_factory=list)
|
||||
|
||||
|
||||
type LunatoneConfigEntry = ConfigEntry[LunatoneData]
|
||||
|
||||
@@ -95,16 +95,18 @@ async def async_setup_entry(
|
||||
assert config_entry.unique_id is not None
|
||||
|
||||
entities: list[SensorEntity] = [
|
||||
LunatoneSensor(
|
||||
coordinator_sensors, description, sensor_id, config_entry.unique_id
|
||||
)
|
||||
for sensor_id, sensor_data in coordinator_sensors.data.items()
|
||||
if (description := SENSOR_TYPES.get(sensor_data.data.type))
|
||||
]
|
||||
entities.extend(
|
||||
LunatoneDALILineStatusSensor(coordinator_info, line_id, config_entry.unique_id)
|
||||
for line_id in coordinator_info.data.lines
|
||||
)
|
||||
]
|
||||
|
||||
if coordinator_sensors is not None:
|
||||
entities.extend(
|
||||
LunatoneSensor(
|
||||
coordinator_sensors, description, sensor_id, config_entry.unique_id
|
||||
)
|
||||
for sensor_id, sensor_data in coordinator_sensors.data.items()
|
||||
if (description := SENSOR_TYPES.get(sensor_data.data.type))
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
from unittest.mock import AsyncMock, PropertyMock
|
||||
|
||||
import aiohttp
|
||||
from lunatone_rest_api_client.models.info import Tier
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.lunatone.const import DOMAIN, MANUFACTURER
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
@@ -61,6 +63,44 @@ async def test_load_unload_config_entry(
|
||||
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("tier", "emergency_light", "sensors_supported"),
|
||||
[
|
||||
(Tier.BASIC, False, True),
|
||||
(Tier.BASIC, True, False),
|
||||
(Tier.PLUS, False, True),
|
||||
(Tier.PLUS, True, True),
|
||||
],
|
||||
ids=["basic", "emergency", "plus", "plus+emergency"],
|
||||
)
|
||||
async def test_load_config_entry_sensor_capability(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
mock_lunatone_devices: AsyncMock,
|
||||
mock_lunatone_sensors: AsyncMock,
|
||||
mock_lunatone_scan: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
tier: Tier,
|
||||
emergency_light: bool,
|
||||
sensors_supported: bool,
|
||||
) -> None:
|
||||
"""Test the sensor coordinator follows the device capabilities."""
|
||||
mock_lunatone_info.data.tier = tier
|
||||
mock_lunatone_info.data.emergency_light = emergency_light
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert (
|
||||
mock_config_entry.runtime_data.coordinator_sensors is not None
|
||||
) == sensors_supported
|
||||
if sensors_supported:
|
||||
mock_lunatone_sensors.async_refresh.assert_called()
|
||||
mock_lunatone_sensors.async_update.assert_called()
|
||||
else:
|
||||
mock_lunatone_sensors.async_refresh.assert_not_called()
|
||||
mock_lunatone_sensors.async_update.assert_not_called()
|
||||
|
||||
|
||||
async def test_config_entry_not_ready_info_api_fail(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
@@ -146,8 +186,8 @@ async def test_config_entry_not_ready_scan_api_fail(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
mock_lunatone_devices: AsyncMock,
|
||||
mock_lunatone_sensors: AsyncMock,
|
||||
mock_lunatone_scan: AsyncMock,
|
||||
mock_lunatone_sensors: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test config entry not ready due to sensors API failure."""
|
||||
@@ -157,7 +197,6 @@ async def test_config_entry_not_ready_scan_api_fail(
|
||||
|
||||
mock_lunatone_info.async_update.assert_called_once()
|
||||
mock_lunatone_devices.async_update.assert_called_once()
|
||||
mock_lunatone_sensors.async_update.assert_called_once()
|
||||
mock_lunatone_scan.async_update.assert_called_once()
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
@@ -168,8 +207,8 @@ async def test_config_entry_not_ready_scan_api_fail(
|
||||
|
||||
mock_lunatone_info.async_update.assert_called()
|
||||
mock_lunatone_devices.async_update.assert_called()
|
||||
mock_lunatone_sensors.async_update.assert_called()
|
||||
mock_lunatone_scan.async_update.assert_called()
|
||||
mock_lunatone_sensors.async_update.assert_called()
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
|
||||
@@ -225,6 +264,7 @@ async def test_config_entry_not_ready_no_sensors_data(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
mock_lunatone_devices: AsyncMock,
|
||||
mock_lunatone_scan: AsyncMock,
|
||||
mock_lunatone_sensors: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
@@ -235,6 +275,7 @@ async def test_config_entry_not_ready_no_sensors_data(
|
||||
|
||||
mock_lunatone_info.async_update.assert_called_once()
|
||||
mock_lunatone_devices.async_update.assert_called_once()
|
||||
mock_lunatone_scan.async_update.assert_called_once()
|
||||
mock_lunatone_sensors.async_update.assert_called_once()
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
@@ -243,7 +284,6 @@ async def test_config_entry_not_ready_no_dali_scan_data(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
mock_lunatone_devices: AsyncMock,
|
||||
mock_lunatone_sensors: AsyncMock,
|
||||
mock_lunatone_scan: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
@@ -254,7 +294,6 @@ async def test_config_entry_not_ready_no_dali_scan_data(
|
||||
|
||||
mock_lunatone_info.async_update.assert_called_once()
|
||||
mock_lunatone_devices.async_update.assert_called_once()
|
||||
mock_lunatone_sensors.async_update.assert_called_once()
|
||||
mock_lunatone_scan.async_update.assert_called_once()
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
@@ -60,18 +60,18 @@ async def test_sensor_value_update(
|
||||
mock_lunatone_sensors.async_update.side_effect = fake_update
|
||||
|
||||
entities = hass.states.async_all(Platform.SENSOR)
|
||||
assert entities[0].state == "unknown"
|
||||
assert entities[1].state == "unknown"
|
||||
assert entities[2].state == "unknown"
|
||||
assert entities[3].state == "unknown"
|
||||
assert entities[4].state == "unknown"
|
||||
|
||||
freezer.tick(timedelta(seconds=40))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entities = hass.states.async_all(Platform.SENSOR)
|
||||
assert entities[0].state == "22"
|
||||
assert entities[1].state == "55"
|
||||
assert entities[2].state == "20"
|
||||
assert entities[2].state == "22"
|
||||
assert entities[3].state == "55"
|
||||
assert entities[4].state == "20"
|
||||
|
||||
|
||||
async def test_dali_line_status_value_update(
|
||||
|
||||
Reference in New Issue
Block a user