forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add event platform to Nice G.O. (home-assistant#124253)
* Add event platform to Nice G.O. * Add icon for barrier obstructed event * Better assertions * More test improvements
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |