diff --git a/custom_components/crestron/const.py b/custom_components/crestron/const.py index 07dbcf1..f6192de 100644 --- a/custom_components/crestron/const.py +++ b/custom_components/crestron/const.py @@ -14,7 +14,7 @@ "iTunes": "ITUNES", } -POLL_INTERVAL_SECONDS = 15 +POLL_INTERVAL_SECONDS = 30 REBOOT_COOLDOWN_SECONDS = 180 -COMMAND_TIMEOUT_SECONDS = 5.0 +COMMAND_TIMEOUT_SECONDS = 2.0 CONNECT_TIMEOUT_SECONDS = 5.0 diff --git a/custom_components/crestron/hub.py b/custom_components/crestron/hub.py index 778fef7..3a31ee8 100644 --- a/custom_components/crestron/hub.py +++ b/custom_components/crestron/hub.py @@ -235,13 +235,14 @@ async def _async_poll(self) -> dict[str, dict[str, Any]]: power = await self.request(f"{zone} POWER") if power is not None: zone_state["power"] = power - volume_line = await self.request(f"{zone} VOLUME CHECK") - if volume_line: - with contextlib.suppress(ValueError): - zone_state["volume"] = int(volume_line.split()[-1]) - source_line = await self.request(f"{zone} SOURCE") - if source_line: - zone_state["source"] = source_line.split()[-1] + if power and power.upper().endswith("ON"): + volume_line = await self.request(f"{zone} VOLUME CHECK") + if volume_line: + with contextlib.suppress(ValueError): + zone_state["volume"] = int(volume_line.split()[-1]) + source_line = await self.request(f"{zone} SOURCE") + if source_line: + zone_state["source"] = source_line.split()[-1] except ConnectionError as exc: had_error = True _LOGGER.debug("Poll failed for zone %s: %s", zone, exc) diff --git a/tests/test_hub.py b/tests/test_hub.py index a67810e..d4f740b 100644 --- a/tests/test_hub.py +++ b/tests/test_hub.py @@ -217,6 +217,7 @@ async def test_poll_collects_state_for_all_zones( ) -> None: entry = _make_entry(["KITCHEN", "DECK"]) entry.add_to_hass(hass) + # DECK is off, so only POWER is queried for it — no VOLUME/SOURCE lines. open_stub.enqueue( 2000, [ @@ -224,8 +225,6 @@ async def test_poll_collects_state_for_all_zones( "KITCHEN VOLUME CURRENT 40\r\n", "KITCHEN SOURCE CHROMECAST\r\n", "DECK POWER OFF\r\n", - "DECK VOLUME CURRENT 0\r\n", - "DECK SOURCE ITUNES\r\n", ], ) @@ -240,8 +239,11 @@ async def test_poll_collects_state_for_all_zones( "volume": 40, "source": "CHROMECAST", } - assert data["DECK"] == { - "power": "DECK POWER OFF", - "volume": 0, - "source": "ITUNES", - } + assert data["DECK"] == {"power": "DECK POWER OFF"} + # Only 4 lines consumed (3 KITCHEN + 1 DECK POWER); VOLUME/SOURCE skipped for OFF zone. + assert open_stub.writers[0].writes == [ + "KITCHEN POWER\r\n", + "KITCHEN VOLUME CHECK\r\n", + "KITCHEN SOURCE\r\n", + "DECK POWER\r\n", + ]