Skip to content

Commit

Permalink
Add event platform to Nice G.O. (home-assistant#124253)
Browse files Browse the repository at this point in the history
* Add event platform to Nice G.O.

* Add icon for barrier obstructed event

* Better assertions

* More test improvements
  • Loading branch information
IceBotYT authored Aug 19, 2024
1 parent 566c00e commit fc767ee
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
7 changes: 6 additions & 1 deletion homeassistant/components/nice_go/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
51 changes: 51 additions & 0 deletions homeassistant/components/nice_go/event.py
Original file line number Diff line number Diff line change
@@ -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()
5 changes: 5 additions & 0 deletions homeassistant/components/nice_go/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"vacation_mode": {
"default": "mdi:beach"
}
},
"event": {
"barrier_obstructed": {
"default": "mdi:garage-alert"
}
}
}
}
12 changes: 12 additions & 0 deletions homeassistant/components/nice_go/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
31 changes: 31 additions & 0 deletions tests/components/nice_go/test_event.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit fc767ee

Please sign in to comment.