|
|
@@ -195,17 +195,34 @@ class RoomCalCoordinator(DataUpdateCoordinator[CoordinatorData]):
|
|
|
await self._guest_mode_switch.async_turn_off()
|
|
|
|
|
|
async def _async_run_check_in_actions(self, res: ReservationData) -> None:
|
|
|
- """Run WiFi key update and guest mode activation. No-op when mode_sync is off."""
|
|
|
+ """Run WiFi updates and guest mode activation. No-op when mode_sync is off."""
|
|
|
if not self._is_mode_sync_on():
|
|
|
return
|
|
|
- if isinstance(res.wifi_ssid, str) and isinstance(res.wifi_key, str):
|
|
|
- from .wifi import async_set_wifi_key
|
|
|
+ if self._is_use_unifi_on() and isinstance(res.wifi_ssid, str):
|
|
|
+ from .wifi import async_set_ssid_enabled, async_set_wifi_key
|
|
|
try:
|
|
|
- await async_set_wifi_key(self.hass, res.wifi_ssid, res.wifi_key)
|
|
|
+ await async_set_ssid_enabled(self.hass, res.wifi_ssid, True)
|
|
|
except Exception as err:
|
|
|
- _LOGGER.warning("Failed to set WiFi key: %s", err)
|
|
|
+ _LOGGER.warning("Failed to enable SSID '%s': %s", res.wifi_ssid, err)
|
|
|
+ if isinstance(res.wifi_key, str):
|
|
|
+ try:
|
|
|
+ await async_set_wifi_key(self.hass, res.wifi_ssid, res.wifi_key)
|
|
|
+ except Exception as err:
|
|
|
+ _LOGGER.warning("Failed to set WiFi key: %s", err)
|
|
|
await self._async_set_guest_mode(True)
|
|
|
|
|
|
+ async def _async_run_check_out_actions(self, res: ReservationData) -> None:
|
|
|
+ """Run WiFi SSID disable and guest mode deactivation. No-op when mode_sync is off."""
|
|
|
+ if not self._is_mode_sync_on():
|
|
|
+ return
|
|
|
+ if self._is_use_unifi_on() and isinstance(res.wifi_ssid, str):
|
|
|
+ from .wifi import async_set_ssid_enabled
|
|
|
+ try:
|
|
|
+ await async_set_ssid_enabled(self.hass, res.wifi_ssid, False)
|
|
|
+ except Exception as err:
|
|
|
+ _LOGGER.warning("Failed to disable SSID '%s': %s", res.wifi_ssid, err)
|
|
|
+ await self._async_set_guest_mode(False)
|
|
|
+
|
|
|
async def async_reconcile_from_store(self, *, _startup: bool = False) -> None:
|
|
|
"""Load store state, apply elapsed transitions, set room state, reschedule mode sync.
|
|
|
|
|
|
@@ -243,6 +260,7 @@ class RoomCalCoordinator(DataUpdateCoordinator[CoordinatorData]):
|
|
|
next=real_next,
|
|
|
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_post_setup_reconcile(self) -> None:
|
|
|
@@ -260,6 +278,13 @@ class RoomCalCoordinator(DataUpdateCoordinator[CoordinatorData]):
|
|
|
return entity.is_on
|
|
|
return False
|
|
|
|
|
|
+ def _is_use_unifi_on(self) -> bool:
|
|
|
+ from .switch import UseUnifiSwitch # avoid circular at module level
|
|
|
+ for entity in self.hass.data.get(DOMAIN, {}).get(self.entry.entry_id, {}).get("switches", []):
|
|
|
+ if isinstance(entity, UseUnifiSwitch):
|
|
|
+ return entity.is_on
|
|
|
+ return True # safe default: don't silently skip WiFi if switch isn't found yet
|
|
|
+
|
|
|
@callback
|
|
|
def _async_handle_check_in(self, _now: datetime) -> None:
|
|
|
self.hass.async_create_task(self._async_do_check_in())
|
|
|
@@ -273,6 +298,7 @@ class RoomCalCoordinator(DataUpdateCoordinator[CoordinatorData]):
|
|
|
self.hass.async_create_task(self._async_do_check_out())
|
|
|
|
|
|
async def _async_do_check_out(self) -> None:
|
|
|
+ departing = self.store.get(STORE_CURRENT) or self._empty_reservation()
|
|
|
await self.store.async_set(STORE_CURRENT, None)
|
|
|
self._current_last_refresh = dt_util.utcnow()
|
|
|
self.async_set_updated_data(CoordinatorData(
|
|
|
@@ -280,8 +306,9 @@ class RoomCalCoordinator(DataUpdateCoordinator[CoordinatorData]):
|
|
|
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,
|
|
|
))
|
|
|
- await self._async_set_guest_mode(False)
|
|
|
+ await self._async_run_check_out_actions(departing)
|
|
|
|
|
|
async def async_clear_current(self) -> None:
|
|
|
"""Clear the Current slot in the store and notify listeners."""
|
|
|
@@ -306,6 +333,7 @@ class RoomCalCoordinator(DataUpdateCoordinator[CoordinatorData]):
|
|
|
next=new_next,
|
|
|
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,
|
|
|
))
|
|
|
self._reschedule_mode_sync(next_data, new_next)
|
|
|
# If mode_sync is on and the newly imported record's check-in has already passed,
|
|
|
@@ -326,8 +354,8 @@ class RoomCalCoordinator(DataUpdateCoordinator[CoordinatorData]):
|
|
|
next=displayed_next,
|
|
|
current_last_refresh=self._current_last_refresh,
|
|
|
next_last_refresh=self._next_last_refresh,
|
|
|
-
|
|
|
override_active=True,
|
|
|
+ api_status=self.data.api_status if self.data else None,
|
|
|
))
|
|
|
await self._async_run_check_in_actions(override.current)
|
|
|
|
|
|
@@ -370,8 +398,8 @@ class RoomCalCoordinator(DataUpdateCoordinator[CoordinatorData]):
|
|
|
next=displayed_next,
|
|
|
current_last_refresh=self._current_last_refresh,
|
|
|
next_last_refresh=self._next_last_refresh,
|
|
|
-
|
|
|
override_active=True,
|
|
|
+ api_status=self.data.api_status if self.data else None,
|
|
|
))
|
|
|
return
|
|
|
|
|
|
@@ -393,8 +421,8 @@ class RoomCalCoordinator(DataUpdateCoordinator[CoordinatorData]):
|
|
|
next=displayed_next,
|
|
|
current_last_refresh=self._current_last_refresh,
|
|
|
next_last_refresh=self._next_last_refresh,
|
|
|
-
|
|
|
override_active=True,
|
|
|
+ api_status=self.data.api_status if self.data else None,
|
|
|
))
|
|
|
else:
|
|
|
self.async_set_updated_data(CoordinatorData(
|
|
|
@@ -402,6 +430,7 @@ class RoomCalCoordinator(DataUpdateCoordinator[CoordinatorData]):
|
|
|
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,
|
|
|
))
|
|
|
|
|
|
def cancel_mode_sync(self) -> None:
|