diff --git a/homeassistant/components/palazzetti/__init__.py b/homeassistant/components/palazzetti/__init__.py index 9a1eafe36379..b562c3db8660 100644 --- a/homeassistant/components/palazzetti/__init__.py +++ b/homeassistant/components/palazzetti/__init__.py @@ -8,6 +8,7 @@ from .coordinator import PalazzettiConfigEntry, PalazzettiDataUpdateCoordinator PLATFORMS: list[Platform] = [ Platform.BUTTON, Platform.CLIMATE, + Platform.LIGHT, Platform.NUMBER, Platform.SENSOR, ] diff --git a/homeassistant/components/palazzetti/light.py b/homeassistant/components/palazzetti/light.py new file mode 100644 index 000000000000..768a4174fe47 --- /dev/null +++ b/homeassistant/components/palazzetti/light.py @@ -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() diff --git a/homeassistant/components/palazzetti/strings.json b/homeassistant/components/palazzetti/strings.json index 9e1ded4114d7..0cb37bd28f90 100644 --- a/homeassistant/components/palazzetti/strings.json +++ b/homeassistant/components/palazzetti/strings.json @@ -48,6 +48,11 @@ } } }, + "light": { + "light": { + "name": "[%key:component::light::title%]" + } + }, "number": { "combustion_power": { "name": "Combustion power" diff --git a/tests/components/palazzetti/conftest.py b/tests/components/palazzetti/conftest.py index 859a0f8e3500..a0c43d94a340 100644 --- a/tests/components/palazzetti/conftest.py +++ b/tests/components/palazzetti/conftest.py @@ -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 diff --git a/tests/components/palazzetti/snapshots/test_light.ambr b/tests/components/palazzetti/snapshots/test_light.ambr new file mode 100644 index 000000000000..fbc4f75fb15b --- /dev/null +++ b/tests/components/palazzetti/snapshots/test_light.ambr @@ -0,0 +1,60 @@ +# serializer version: 1 +# name: test_all_entities[light.stove_light-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : list([ + , + ]), + }), + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'light', + 'entity_category': None, + 'entity_id': 'light.stove_light', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + '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({ + : None, + : 'Stove Light', + : list([ + , + ]), + : , + }), + 'context': , + 'entity_id': 'light.stove_light', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/palazzetti/test_light.py b/tests/components/palazzetti/test_light.py new file mode 100644 index 000000000000..6dca8d9621ca --- /dev/null +++ b/tests/components/palazzetti/test_light.py @@ -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