Преглед изворни кода

Add Clear Current button to Room State device

ZiB пре 4 месеци
родитељ
комит
765d8cba2f

+ 22 - 0
custom_components/room_cal_ha/button.py

@@ -25,6 +25,7 @@ async def async_setup_entry(
   async_add_entities([
   async_add_entities([
     ForceRefreshButton(coordinator, entry, device_name),
     ForceRefreshButton(coordinator, entry, device_name),
     PromoteFromNextButton(coordinator, entry, device_name),
     PromoteFromNextButton(coordinator, entry, device_name),
+    ClearCurrentButton(coordinator, entry, device_name),
     TestNextGuestButton(coordinator, entry, device_name),
     TestNextGuestButton(coordinator, entry, device_name),
   ])
   ])
 
 
@@ -71,6 +72,27 @@ class PromoteFromNextButton(ButtonEntity):
     await self._coordinator.async_promote_next_to_current()
     await self._coordinator.async_promote_next_to_current()
 
 
 
 
+class ClearCurrentButton(ButtonEntity):
+  _attr_has_entity_name = True
+  _attr_name = "Clear Current"
+
+  def __init__(
+    self,
+    coordinator: RoomCalCoordinator,
+    entry: ConfigEntry,
+    device_name: str,
+  ) -> None:
+    self._coordinator = coordinator
+    self._attr_unique_id = f"{entry.entry_id}_clear_current"
+    self._attr_device_info = DeviceInfo(
+      identifiers={(DOMAIN, f"{entry.entry_id}_room_state")},
+      name=device_name,
+    )
+
+  async def async_press(self) -> None:
+    await self._coordinator.async_clear_current()
+
+
 class TestNextGuestButton(CoordinatorEntity[RoomCalCoordinator], ButtonEntity):
 class TestNextGuestButton(CoordinatorEntity[RoomCalCoordinator], ButtonEntity):
   _attr_has_entity_name = True
   _attr_has_entity_name = True
   _attr_name = "Test Next Guest"
   _attr_name = "Test Next Guest"

+ 12 - 0
custom_components/room_cal_ha/coordinator.py

@@ -283,6 +283,18 @@ class RoomCalCoordinator(DataUpdateCoordinator[CoordinatorData]):
     ))
     ))
     await self._async_set_guest_mode(False)
     await self._async_set_guest_mode(False)
 
 
+  async def async_clear_current(self) -> None:
+    """Clear the Current slot in the store and notify listeners."""
+    await self.store.async_set(STORE_CURRENT, None)
+    self._current_last_refresh = dt_util.utcnow()
+    self.async_set_updated_data(CoordinatorData(
+      current=self._empty_reservation(),
+      next=self.store.get(STORE_NEXT) or self._empty_reservation(),
+      current_last_refresh=self._current_last_refresh,
+      next_last_refresh=self._next_last_refresh,
+      api_status=self.data.api_status if self.data else None,
+    ))
+
   async def async_promote_next_to_current(self) -> None:
   async def async_promote_next_to_current(self) -> None:
     """Copy the Next slot into Current and notify listeners. Next is left unchanged."""
     """Copy the Next slot into Current and notify listeners. Next is left unchanged."""
     next_data = self.store.get(STORE_NEXT) or self._empty_reservation()
     next_data = self.store.get(STORE_NEXT) or self._empty_reservation()

+ 15 - 0
tests/test_buttons.py

@@ -9,6 +9,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
 import pytest
 import pytest
 
 
 from custom_components.room_cal_ha.button import (
 from custom_components.room_cal_ha.button import (
+  ClearCurrentButton,
   ForceRefreshButton,
   ForceRefreshButton,
   PromoteFromNextButton,
   PromoteFromNextButton,
   TestNextGuestButton,
   TestNextGuestButton,
@@ -22,6 +23,7 @@ def _button(cls):
   btn._coordinator = MagicMock()
   btn._coordinator = MagicMock()
   btn._coordinator.async_refresh = AsyncMock()
   btn._coordinator.async_refresh = AsyncMock()
   btn._coordinator.async_promote_next_to_current = AsyncMock()
   btn._coordinator.async_promote_next_to_current = AsyncMock()
+  btn._coordinator.async_clear_current = AsyncMock()
   return btn
   return btn
 
 
 
 
@@ -49,6 +51,19 @@ async def test_promote_does_not_call_refresh():
   btn._coordinator.async_refresh.assert_not_called()
   btn._coordinator.async_refresh.assert_not_called()
 
 
 
 
+async def test_clear_current_calls_coordinator_clear():
+  btn = _button(ClearCurrentButton)
+  await btn.async_press()
+  btn._coordinator.async_clear_current.assert_awaited_once()
+
+
+async def test_clear_current_does_not_call_refresh_or_promote():
+  btn = _button(ClearCurrentButton)
+  await btn.async_press()
+  btn._coordinator.async_refresh.assert_not_called()
+  btn._coordinator.async_promote_next_to_current.assert_not_called()
+
+
 # ---------------------------------------------------------------------------
 # ---------------------------------------------------------------------------
 # TestNextGuestButton
 # TestNextGuestButton
 # ---------------------------------------------------------------------------
 # ---------------------------------------------------------------------------