Add light platform to Palazzetti integration (#194752)

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
dotvav
2026-11-28 14:05:30 +01:00
committed by GitHub
co-authored by epenet
parent 4520eb857d
commit f656d6445f
6 changed files with 287 additions and 0 deletions
@@ -8,6 +8,7 @@ from .coordinator import PalazzettiConfigEntry, PalazzettiDataUpdateCoordinator
PLATFORMS: list[Platform] = [
Platform.BUTTON,
Platform.CLIMATE,
Platform.LIGHT,
Platform.NUMBER,
Platform.SENSOR,
]
@@ -0,0 +1,80 @@
"""Support for Palazzetti lights."""
from typing import Any, override
from pypalazzetti.exceptions import CommunicationError
from homeassistant.components.light import ColorMode, LightEntity
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import DOMAIN
from .coordinator import PalazzettiConfigEntry, PalazzettiDataUpdateCoordinator
from .entity import PalazzettiEntity
PARALLEL_UPDATES = 1
async def async_setup_entry(
hass: HomeAssistant,
config_entry: PalazzettiConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up Palazzetti light platform."""
coordinator = config_entry.runtime_data
if coordinator.client.has_light_control:
async_add_entities([PalazzettiLightEntity(coordinator)])
class PalazzettiLightEntity(PalazzettiEntity, LightEntity):
"""Representation of a Palazzetti light."""
_attr_translation_key = "light"
_attr_color_mode = ColorMode.ONOFF
_attr_supported_color_modes = {ColorMode.ONOFF}
def __init__(
self,
coordinator: PalazzettiDataUpdateCoordinator,
) -> None:
"""Initialize a Palazzetti light."""
super().__init__(coordinator)
self._attr_unique_id = coordinator.config_entry.unique_id
@property
@override
def is_on(self) -> bool:
"""Return true if light is on."""
return self.coordinator.client.is_light_on
@override
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the light."""
try:
if not await self.coordinator.client.set_light(True):
raise HomeAssistantError(
translation_domain=DOMAIN, translation_key="cannot_connect"
)
except CommunicationError as err:
raise HomeAssistantError(
translation_domain=DOMAIN, translation_key="cannot_connect"
) from err
await self.coordinator.async_request_refresh()
@override
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the light."""
try:
if not await self.coordinator.client.set_light(False):
raise HomeAssistantError(
translation_domain=DOMAIN, translation_key="cannot_connect"
)
except CommunicationError as err:
raise HomeAssistantError(
translation_domain=DOMAIN, translation_key="cannot_connect"
) from err
await self.coordinator.async_request_refresh()
@@ -48,6 +48,11 @@
}
}
},
"light": {
"light": {
"name": "[%key:component::light::title%]"
}
},
"number": {
"combustion_power": {
"name": "Combustion power"
+4
View File
@@ -63,6 +63,9 @@ def mock_palazzetti_client() -> Generator[AsyncMock]:
mock_client.has_fan_high = True
mock_client.has_fan_auto = True
mock_client.has_on_off_switch = True
mock_client.has_light_control = True
mock_client.is_light_on = False
mock_client.light_status = 0
mock_client.has_pellet_level = True
mock_client.host = "XXXXXXXXXX"
mock_client.connected = True
@@ -88,6 +91,7 @@ def mock_palazzetti_client() -> Generator[AsyncMock]:
mock_client.connect.return_value = True
mock_client.update_state.return_value = True
mock_client.set_on.return_value = True
mock_client.set_light.return_value = True
mock_client.set_target_temperature.return_value = True
mock_client.set_fan_speed.return_value = True
mock_client.set_fan_silent.return_value = True
@@ -0,0 +1,60 @@
# serializer version: 1
# name: test_all_entities[light.stove_light-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': dict({
<LightEntityCapabilityAttribute.SUPPORTED_COLOR_MODES: 'supported_color_modes'>: list([
<ColorMode.ONOFF: 'onoff'>,
]),
}),
'config_entry_id': <ANY>,
'config_subentry_id': None,
'device_class': None,
'device_id': <DeviceEntry connections=[('mac', '11:22:33:44:55:66')]>,
'disabled_by': None,
'domain': 'light',
'entity_category': None,
'entity_id': 'light.stove_light',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Light',
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Light',
'platform': 'palazzetti',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'light',
'unique_id': '11:22:33:44:55:66',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[light.stove_light-state]
StateSnapshot({
'attributes': ReadOnlyDict({
<LightEntityStateAttribute.COLOR_MODE: 'color_mode'>: None,
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Stove Light',
<LightEntityCapabilityAttribute.SUPPORTED_COLOR_MODES: 'supported_color_modes'>: list([
<ColorMode.ONOFF: 'onoff'>,
]),
<EntityStateAttribute.SUPPORTED_FEATURES: 'supported_features'>: <LightEntityFeature: 0>,
}),
'context': <ANY>,
'entity_id': 'light.stove_light',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
+137
View File
@@ -0,0 +1,137 @@
"""Tests for the Palazzetti light platform."""
from unittest.mock import AsyncMock, patch
from pypalazzetti.exceptions import CommunicationError
import pytest
from pytest_homeassistant.common import MockConfigEntry, snapshot_platform
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN, LightService
from homeassistant.const import Platform, TargetKey
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from . import setup_integration
ENTITY_ID = "light.stove_light"
async def test_all_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
mock_palazzetti_client: AsyncMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test all entities."""
with patch("homeassistant.components.palazzetti.PLATFORMS", [Platform.LIGHT]):
await setup_integration(hass, mock_config_entry)
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_async_turn_on(
hass: HomeAssistant,
mock_palazzetti_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test turning on the light via service call."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
LIGHT_DOMAIN,
LightService.TURN_ON,
{TargetKey.ENTITY_ID: ENTITY_ID},
blocking=True,
)
mock_palazzetti_client.set_light.assert_called_once_with(True)
async def test_async_turn_off(
hass: HomeAssistant,
mock_palazzetti_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test turning off the light via service call."""
mock_palazzetti_client.is_light_on = True
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
LIGHT_DOMAIN,
LightService.TURN_OFF,
{TargetKey.ENTITY_ID: ENTITY_ID},
blocking=True,
)
mock_palazzetti_client.set_light.assert_called_once_with(False)
@pytest.mark.parametrize(
("service", "method_arg"),
[
pytest.param(LightService.TURN_ON, True, id="turn_on"),
pytest.param(LightService.TURN_OFF, False, id="turn_off"),
],
)
async def test_async_turn_on_off_error(
hass: HomeAssistant,
mock_palazzetti_client: AsyncMock,
mock_config_entry: MockConfigEntry,
service: LightService,
method_arg: bool,
) -> None:
"""Test error handling during turn on/off."""
mock_palazzetti_client.set_light.side_effect = CommunicationError()
await setup_integration(hass, mock_config_entry)
error_message = "Could not connect to the device"
with pytest.raises(HomeAssistantError, match=error_message):
await hass.services.async_call(
LIGHT_DOMAIN,
service,
{TargetKey.ENTITY_ID: ENTITY_ID},
blocking=True,
)
mock_palazzetti_client.set_light.assert_called_once_with(method_arg)
@pytest.mark.parametrize(
("service", "method_arg"),
[
pytest.param(LightService.TURN_ON, True, id="turn_on"),
pytest.param(LightService.TURN_OFF, False, id="turn_off"),
],
)
async def test_async_turn_on_off_rejected(
hass: HomeAssistant,
mock_palazzetti_client: AsyncMock,
mock_config_entry: MockConfigEntry,
service: LightService,
method_arg: bool,
) -> None:
"""Test error handling when device rejects turn on/off."""
mock_palazzetti_client.set_light.return_value = False
await setup_integration(hass, mock_config_entry)
error_message = "Could not connect to the device"
with pytest.raises(HomeAssistantError, match=error_message):
await hass.services.async_call(
LIGHT_DOMAIN,
service,
{TargetKey.ENTITY_ID: ENTITY_ID},
blocking=True,
)
mock_palazzetti_client.set_light.assert_called_once_with(method_arg)
async def test_no_light_control(
hass: HomeAssistant,
mock_palazzetti_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test that light entity is not created when light control is unavailable."""
mock_palazzetti_client.has_light_control = False
await setup_integration(hass, mock_config_entry)
assert hass.states.get(ENTITY_ID) is None