Add event platform to Nice G.O. (#124253)
* Add event platform to Nice G.O. * Add icon for barrier obstructed event * Better assertions * More test improvementspull/124262/head
parent
566c00ef12
commit
fc767ee562
|
@ -11,7 +11,12 @@ from homeassistant.core import HomeAssistant
|
|||
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]
|
||||
|
||||
|
|
|
@ -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()
|
|
@ -4,6 +4,11 @@
|
|||
"vacation_mode": {
|
||||
"default": "mdi:beach"
|
||||
}
|
||||
},
|
||||
"event": {
|
||||
"barrier_obstructed": {
|
||||
"default": "mdi:garage-alert"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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": {
|
||||
|
|
|
@ -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"
|
Loading…
Reference in New Issue