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

Pass entry_id to guest script; test variables shape

Adds entry_id to the script.turn_on variables so the script can target
service calls back at the correct room when multiple rooms are configured.
ZiB пре 3 месеци
родитељ
комит
a30579f11a
2 измењених фајлова са 21 додато и 1 уклоњено
  1. 7 1
      custom_components/room_cal_ha/coordinator.py
  2. 14 0
      tests/test_arrived_departed.py

+ 7 - 1
custom_components/room_cal_ha/coordinator.py

@@ -734,7 +734,13 @@ class RoomCalCoordinator(DataUpdateCoordinator[CoordinatorData]):
     try:
       await self.hass.services.async_call(
         "script", "turn_on",
-        {"entity_id": script_id, "variables": {"event_type": event_type}},
+        {
+          "entity_id": script_id,
+          "variables": {
+            "event_type": event_type,
+            "entry_id": self.entry.entry_id,
+          },
+        },
         blocking=False,
       )
     except Exception as err:

+ 14 - 0
tests/test_arrived_departed.py

@@ -282,6 +282,20 @@ async def test_set_departed_dispatches_script(hass: HomeAssistant):
   mock_script.assert_awaited_once_with("departed")
 
 
+async def test_guest_script_includes_entry_id(hass: HomeAssistant):
+  entry = _make_entry(hass, {CONF_SYNC_MODE: SYNC_MODE_MANUAL, CONF_GUEST_SCRIPT: "script.guest_event"})
+  c = RoomCalCoordinator(hass, entry)
+  await c.async_setup()
+
+  with patch("homeassistant.core.ServiceRegistry.async_call", new=AsyncMock()) as mock_call:
+    await c._async_run_guest_script("arrived")
+
+  mock_call.assert_awaited_once()
+  _domain, _service, data = mock_call.call_args.args  # (domain, service, data)
+  assert data["variables"]["event_type"] == "arrived"
+  assert data["variables"]["entry_id"] == entry.entry_id
+
+
 async def test_no_script_dispatched_when_setting_false(hass: HomeAssistant):
   entry = _make_entry(hass, {CONF_SYNC_MODE: SYNC_MODE_MANUAL, CONF_GUEST_SCRIPT: "script.guest_event"})
   c = RoomCalCoordinator(hass, entry)