From 3af86167648b24f76ffddcf8fc7b3bfc30d95ed4 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Sun, 7 Sep 2025 13:34:31 +0200 Subject: [PATCH] Mark Tractive switches as unavailable when tacker is in the enegy saving zone (#151817) --- homeassistant/components/tractive/__init__.py | 1 + homeassistant/components/tractive/switch.py | 3 +- tests/components/tractive/test_switch.py | 40 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/tractive/__init__.py b/homeassistant/components/tractive/__init__.py index 60bae9bfd2e3..f00e0fec4124 100644 --- a/homeassistant/components/tractive/__init__.py +++ b/homeassistant/components/tractive/__init__.py @@ -291,6 +291,7 @@ class TractiveClient: for switch, key in SWITCH_KEY_MAP.items(): if switch_data := event.get(key): payload[switch] = switch_data["active"] + payload[ATTR_POWER_SAVING] = event.get("tracker_state_reason") == "POWER_SAVING" self._dispatch_tracker_event( TRACKER_SWITCH_STATUS_UPDATED, event["tracker_id"], payload ) diff --git a/homeassistant/components/tractive/switch.py b/homeassistant/components/tractive/switch.py index da2c8e35ff7c..e4db6d69bee1 100644 --- a/homeassistant/components/tractive/switch.py +++ b/homeassistant/components/tractive/switch.py @@ -18,6 +18,7 @@ from .const import ( ATTR_BUZZER, ATTR_LED, ATTR_LIVE_TRACKING, + ATTR_POWER_SAVING, TRACKER_SWITCH_STATUS_UPDATED, ) from .entity import TractiveEntity @@ -104,7 +105,7 @@ class TractiveSwitch(TractiveEntity, SwitchEntity): # We received an event, so the service is online and the switch entities should # be available. - self._attr_available = True + self._attr_available = not event[ATTR_POWER_SAVING] self._attr_is_on = event[self.entity_description.key] self.async_write_ha_state() diff --git a/tests/components/tractive/test_switch.py b/tests/components/tractive/test_switch.py index 92e4676aef11..0b9213bee92b 100644 --- a/tests/components/tractive/test_switch.py +++ b/tests/components/tractive/test_switch.py @@ -12,6 +12,7 @@ from homeassistant.const import ( SERVICE_TURN_ON, STATE_OFF, STATE_ON, + STATE_UNAVAILABLE, Platform, ) from homeassistant.core import HomeAssistant @@ -226,3 +227,42 @@ async def test_switch_off_with_exception( state = hass.states.get(entity_id) assert state assert state.state == STATE_ON + + +async def test_switch_unavailable( + hass: HomeAssistant, + mock_tractive_client: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test the switch is navailable when the tracker is in the energy saving zone.""" + entity_id = "switch.test_pet_tracker_buzzer" + + await init_integration(hass, mock_config_entry) + + mock_tractive_client.send_switch_event(mock_config_entry) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state == STATE_ON + + event = { + "tracker_id": "device_id_123", + "buzzer_control": {"active": True}, + "led_control": {"active": False}, + "live_tracking": {"active": True}, + "tracker_state_reason": "POWER_SAVING", + } + mock_tractive_client.send_switch_event(mock_config_entry, event) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state == STATE_UNAVAILABLE + + mock_tractive_client.send_switch_event(mock_config_entry) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state == STATE_ON