diff --git a/homeassistant/components/nice_go/__init__.py b/homeassistant/components/nice_go/__init__.py index bf8dbfcaecdc0e..ab3dc06e3c1f09 100644 --- a/homeassistant/components/nice_go/__init__.py +++ b/homeassistant/components/nice_go/__init__.py @@ -11,7 +11,12 @@ from .coordinator import NiceGOUpdateCoordinator _LOGGER = logging.getLogger(__name__) -PLATFORMS: list[Platform] = [Platform.COVER, Platform.LIGHT, Platform.SWITCH] +PLATFORMS: list[Platform] = [ + Platform.COVER, + Platform.EVENT, + Platform.LIGHT, + Platform.SWITCH, +] type NiceGOConfigEntry = ConfigEntry[NiceGOUpdateCoordinator] diff --git a/homeassistant/components/nice_go/event.py b/homeassistant/components/nice_go/event.py new file mode 100644 index 00000000000000..caf984f824f89b --- /dev/null +++ b/homeassistant/components/nice_go/event.py @@ -0,0 +1,51 @@ +"""Nice G.O. event platform.""" + +import logging +from typing import Any + +from homeassistant.components.event import EventEntity +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import NiceGOConfigEntry +from .entity import NiceGOEntity + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: NiceGOConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up Nice G.O. event.""" + + coordinator = config_entry.runtime_data + + async_add_entities( + NiceGOEventEntity(coordinator, device_id, device_data.name, "event") + for device_id, device_data in coordinator.data.items() + ) + + +EVENT_BARRIER_OBSTRUCTED = "barrier_obstructed" + + +class NiceGOEventEntity(NiceGOEntity, EventEntity): + """Event for Nice G.O. devices.""" + + _attr_translation_key = "barrier_obstructed" + _attr_event_types = [EVENT_BARRIER_OBSTRUCTED] + + async def async_added_to_hass(self) -> None: + """Listen for events.""" + await super().async_added_to_hass() + self.coordinator.api.event(self.on_barrier_obstructed) + + async def on_barrier_obstructed(self, data: dict[str, Any]) -> None: + """Handle barrier obstructed event.""" + _LOGGER.debug("Barrier obstructed event: %s", data) + if data["deviceId"] == self.data.id: + _LOGGER.debug("Barrier obstructed event for %s, triggering", self.data.name) + self._trigger_event(EVENT_BARRIER_OBSTRUCTED) + self.schedule_update_ha_state() diff --git a/homeassistant/components/nice_go/icons.json b/homeassistant/components/nice_go/icons.json index fdce7c43e242c1..61e36c9eb3a2ef 100644 --- a/homeassistant/components/nice_go/icons.json +++ b/homeassistant/components/nice_go/icons.json @@ -4,6 +4,11 @@ "vacation_mode": { "default": "mdi:beach" } + }, + "event": { + "barrier_obstructed": { + "default": "mdi:garage-alert" + } } } } diff --git a/homeassistant/components/nice_go/strings.json b/homeassistant/components/nice_go/strings.json index 7f2ea1512ddc32..30a2bbf58b60a2 100644 --- a/homeassistant/components/nice_go/strings.json +++ b/homeassistant/components/nice_go/strings.json @@ -27,6 +27,18 @@ "vacation_mode": { "name": "Vacation mode" } + }, + "event": { + "barrier_obstructed": { + "name": "Barrier obstructed", + "state_attributes": { + "event_type": { + "state": { + "barrier_obstructed": "Barrier obstructed" + } + } + } + } } }, "issues": { diff --git a/tests/components/nice_go/test_event.py b/tests/components/nice_go/test_event.py new file mode 100644 index 00000000000000..0038b2882ad4cc --- /dev/null +++ b/tests/components/nice_go/test_event.py @@ -0,0 +1,31 @@ +"""Nice G.O. event tests.""" + +from unittest.mock import AsyncMock, MagicMock + +import pytest + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant + +from . import setup_integration + +from tests.common import MockConfigEntry + + +@pytest.mark.freeze_time("2024-08-19") +async def test_barrier_obstructed( + hass: HomeAssistant, + mock_nice_go: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test barrier obstructed.""" + mock_nice_go.event = MagicMock() + await setup_integration(hass, mock_config_entry, [Platform.EVENT]) + + await mock_nice_go.event.call_args_list[2][0][0]({"deviceId": "1"}) + await hass.async_block_till_done() + + event_state = hass.states.get("event.test_garage_1_barrier_obstructed") + + assert event_state.state == "2024-08-19T00:00:00.000+00:00" + assert event_state.attributes["event_type"] == "barrier_obstructed"