mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Add event to Snoo (#140827)
This commit is contained in:
parent
52054d69c7
commit
ea259ffa66
@ -19,6 +19,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORMS: list[Platform] = [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.EVENT,
|
||||
Platform.SELECT,
|
||||
Platform.SENSOR,
|
||||
Platform.SWITCH,
|
||||
|
62
homeassistant/components/snoo/event.py
Normal file
62
homeassistant/components/snoo/event.py
Normal file
@ -0,0 +1,62 @@
|
||||
"""Support for Snoo Events."""
|
||||
|
||||
from homeassistant.components.event import EventEntity, EventEntityDescription
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .coordinator import SnooConfigEntry
|
||||
from .entity import SnooDescriptionEntity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: SnooConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Snoo device."""
|
||||
coordinators = entry.runtime_data
|
||||
async_add_entities(
|
||||
SnooEvent(
|
||||
coordinator,
|
||||
EventEntityDescription(
|
||||
key="event",
|
||||
translation_key="event",
|
||||
event_types=[
|
||||
"timer",
|
||||
"cry",
|
||||
"command",
|
||||
"safety_clip",
|
||||
"long_activity_press",
|
||||
"activity",
|
||||
"power",
|
||||
"status_requested",
|
||||
"sticky_white_noise_updated",
|
||||
],
|
||||
),
|
||||
)
|
||||
for coordinator in coordinators.values()
|
||||
)
|
||||
|
||||
|
||||
class SnooEvent(SnooDescriptionEntity, EventEntity):
|
||||
"""A event using Snoo coordinator."""
|
||||
|
||||
@callback
|
||||
def _async_handle_event(self) -> None:
|
||||
"""Handle the demo button event."""
|
||||
self._trigger_event(
|
||||
self.coordinator.data.event.value,
|
||||
)
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Add Event."""
|
||||
await super().async_added_to_hass()
|
||||
if self.coordinator.data:
|
||||
# If we were able to get data on startup - set it
|
||||
# Otherwise, it will update when the coordinator gets data.
|
||||
self._async_handle_event()
|
||||
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
self._async_handle_event()
|
||||
return super()._handle_coordinator_update()
|
@ -41,7 +41,26 @@
|
||||
"name": "Right safety clip"
|
||||
}
|
||||
},
|
||||
|
||||
"event": {
|
||||
"event": {
|
||||
"name": "Snoo event",
|
||||
"state_attributes": {
|
||||
"event_type": {
|
||||
"state": {
|
||||
"timer": "Timer",
|
||||
"cry": "Cry",
|
||||
"command": "Command sent",
|
||||
"safety_clip": "Safety clip changed",
|
||||
"long_activity_press": "Long activity press",
|
||||
"activity": "Activity press",
|
||||
"power": "Power button pressed",
|
||||
"status_requested": "Status requested",
|
||||
"sticky_white_noise_updated": "Sleepytime sounds updated"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"sensor": {
|
||||
"state": {
|
||||
"name": "State",
|
||||
|
45
tests/components/snoo/test_event.py
Normal file
45
tests/components/snoo/test_event.py
Normal file
@ -0,0 +1,45 @@
|
||||
"""Test Snoo Events."""
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from freezegun import freeze_time
|
||||
|
||||
from homeassistant.const import STATE_UNAVAILABLE
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import async_init_integration, find_update_callback
|
||||
from .const import MOCK_SNOO_DATA
|
||||
|
||||
|
||||
@freeze_time("2025-01-01 12:00:00")
|
||||
async def test_events(hass: HomeAssistant, bypass_api: AsyncMock) -> None:
|
||||
"""Test events and check test values are correctly set."""
|
||||
await async_init_integration(hass)
|
||||
assert len(hass.states.async_all("event")) == 1
|
||||
assert hass.states.get("event.test_snoo_snoo_event").state == STATE_UNAVAILABLE
|
||||
find_update_callback(bypass_api, "random_num")(MOCK_SNOO_DATA)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("event")) == 1
|
||||
assert (
|
||||
hass.states.get("event.test_snoo_snoo_event").state
|
||||
== "2025-01-01T12:00:00.000+00:00"
|
||||
)
|
||||
|
||||
|
||||
@freeze_time("2025-01-01 12:00:00")
|
||||
async def test_events_data_on_startup(
|
||||
hass: HomeAssistant, bypass_api: AsyncMock
|
||||
) -> None:
|
||||
"""Test events and check test values are correctly set if data exists on first update."""
|
||||
|
||||
def update_status(_):
|
||||
find_update_callback(bypass_api, "random_num")(MOCK_SNOO_DATA)
|
||||
|
||||
bypass_api.get_status.side_effect = update_status
|
||||
await async_init_integration(hass)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("event")) == 1
|
||||
assert (
|
||||
hass.states.get("event.test_snoo_snoo_event").state
|
||||
== "2025-01-01T12:00:00.000+00:00"
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user