mirror of
https://github.com/home-assistant/core.git
synced 2026-12-06 05:38:06 -04:00
Fix Control4 lights not being created when the system has no dimmers (#188310)
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
11ddd6d8a8
commit
e28f192c77
@@ -1,6 +1,7 @@
|
||||
"""Provides data updates from the Control4 controller for platforms."""
|
||||
|
||||
from collections import defaultdict
|
||||
from json import JSONDecodeError
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
@@ -23,7 +24,14 @@ async def _update_variables_for_config_entry(
|
||||
) -> dict[int, dict[str, Any]]:
|
||||
"""Retrieve data from the Control4 director."""
|
||||
director = entry.runtime_data.director
|
||||
data = await director.get_all_item_variable_value(variable_names)
|
||||
try:
|
||||
data = await director.get_all_item_variable_value(variable_names)
|
||||
except JSONDecodeError:
|
||||
# A subclass of ValueError: an unreadable response is still a failure.
|
||||
raise
|
||||
except ValueError:
|
||||
# pyControl4 raises ValueError when no item has any of the variables.
|
||||
return {}
|
||||
result_dict: defaultdict[int, dict[str, Any]] = defaultdict(dict)
|
||||
for item in data:
|
||||
result_dict[item["id"]][item["varName"]] = item["value"]
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
"""Test Control4 Light."""
|
||||
|
||||
from json import JSONDecodeError
|
||||
from typing import Any
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
|
||||
from homeassistant.components.control4.const import DOMAIN
|
||||
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
||||
from homeassistant.const import STATE_ON, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
LIGHT_ITEMS = [
|
||||
{
|
||||
"id": 345,
|
||||
"name": "Kitchen Light",
|
||||
"type": 7,
|
||||
"parentId": 678,
|
||||
"categories": ["lights"],
|
||||
},
|
||||
{
|
||||
"id": 678,
|
||||
"name": "Kitchen Light",
|
||||
"type": 6,
|
||||
"manufacturer": "Control4",
|
||||
"model": "C4-SW120277",
|
||||
"categories": ["lights"],
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def platforms() -> list[Platform]:
|
||||
"""Platforms which should be loaded during the test."""
|
||||
return [Platform.LIGHT]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_light_items(mock_c4_director: MagicMock) -> None:
|
||||
"""Add a light to the items reported by the director."""
|
||||
mock_c4_director.get_all_item_info.return_value = [
|
||||
*mock_c4_director.get_all_item_info.return_value,
|
||||
*LIGHT_ITEMS,
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_light_variables(
|
||||
mock_c4_director: MagicMock, light_variables: list[dict[str, Any]]
|
||||
) -> None:
|
||||
"""Mock a director where only some light variables exist."""
|
||||
reported_names = {variable["varName"] for variable in light_variables}
|
||||
|
||||
def get_all_item_variable_value(variable_names: set[str]) -> list[dict[str, Any]]:
|
||||
if not variable_names & reported_names:
|
||||
# pyControl4 raises ValueError when no item has any of the variables.
|
||||
raise ValueError("Empty response received from Director!")
|
||||
return light_variables
|
||||
|
||||
mock_c4_director.get_all_item_variable_value.side_effect = (
|
||||
get_all_item_variable_value
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_c4_account", "mock_light_items", "mock_light_variables")
|
||||
@pytest.mark.parametrize(
|
||||
"light_variables",
|
||||
[
|
||||
pytest.param(
|
||||
[{"id": 345, "varName": "LIGHT_STATE", "value": 1}],
|
||||
id="no_dimmers",
|
||||
),
|
||||
pytest.param(
|
||||
[{"id": 345, "varName": "LIGHT_LEVEL", "value": 100}],
|
||||
id="only_dimmers",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_light_created_when_other_light_type_missing(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test lights are created when the system has only one type of light."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
entity_id = entity_registry.async_get_entity_id(LIGHT_DOMAIN, DOMAIN, "345")
|
||||
assert entity_id is not None
|
||||
assert hass.states.get(entity_id).state == STATE_ON
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_c4_account", "mock_light_items")
|
||||
async def test_light_not_created_when_response_is_invalid(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_c4_director: MagicMock,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test an unreadable response is not treated as a system without lights."""
|
||||
mock_c4_director.get_all_item_variable_value.side_effect = JSONDecodeError(
|
||||
"Expecting value", "", 0
|
||||
)
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert entity_registry.async_get_entity_id(LIGHT_DOMAIN, DOMAIN, "345") is None
|
||||
# The failure is reported instead of being read as an empty response.
|
||||
assert "Expecting value" in caplog.text
|
||||
Reference in New Issue
Block a user