Skip to content

Commit 3af8616

Browse files
bieniufrenck
authored andcommitted
Mark Tractive switches as unavailable when tacker is in the enegy saving zone (home-assistant#151817)
1 parent 64ec460 commit 3af8616

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

homeassistant/components/tractive/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ def _send_switch_update(self, event: dict[str, Any]) -> None:
291291
for switch, key in SWITCH_KEY_MAP.items():
292292
if switch_data := event.get(key):
293293
payload[switch] = switch_data["active"]
294+
payload[ATTR_POWER_SAVING] = event.get("tracker_state_reason") == "POWER_SAVING"
294295
self._dispatch_tracker_event(
295296
TRACKER_SWITCH_STATUS_UPDATED, event["tracker_id"], payload
296297
)

homeassistant/components/tractive/switch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
ATTR_BUZZER,
1919
ATTR_LED,
2020
ATTR_LIVE_TRACKING,
21+
ATTR_POWER_SAVING,
2122
TRACKER_SWITCH_STATUS_UPDATED,
2223
)
2324
from .entity import TractiveEntity
@@ -104,7 +105,7 @@ def handle_status_update(self, event: dict[str, Any]) -> None:
104105

105106
# We received an event, so the service is online and the switch entities should
106107
# be available.
107-
self._attr_available = True
108+
self._attr_available = not event[ATTR_POWER_SAVING]
108109
self._attr_is_on = event[self.entity_description.key]
109110

110111
self.async_write_ha_state()

tests/components/tractive/test_switch.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
SERVICE_TURN_ON,
1313
STATE_OFF,
1414
STATE_ON,
15+
STATE_UNAVAILABLE,
1516
Platform,
1617
)
1718
from homeassistant.core import HomeAssistant
@@ -226,3 +227,42 @@ async def test_switch_off_with_exception(
226227
state = hass.states.get(entity_id)
227228
assert state
228229
assert state.state == STATE_ON
230+
231+
232+
async def test_switch_unavailable(
233+
hass: HomeAssistant,
234+
mock_tractive_client: AsyncMock,
235+
mock_config_entry: MockConfigEntry,
236+
) -> None:
237+
"""Test the switch is navailable when the tracker is in the energy saving zone."""
238+
entity_id = "switch.test_pet_tracker_buzzer"
239+
240+
await init_integration(hass, mock_config_entry)
241+
242+
mock_tractive_client.send_switch_event(mock_config_entry)
243+
await hass.async_block_till_done()
244+
245+
state = hass.states.get(entity_id)
246+
assert state
247+
assert state.state == STATE_ON
248+
249+
event = {
250+
"tracker_id": "device_id_123",
251+
"buzzer_control": {"active": True},
252+
"led_control": {"active": False},
253+
"live_tracking": {"active": True},
254+
"tracker_state_reason": "POWER_SAVING",
255+
}
256+
mock_tractive_client.send_switch_event(mock_config_entry, event)
257+
await hass.async_block_till_done()
258+
259+
state = hass.states.get(entity_id)
260+
assert state
261+
assert state.state == STATE_UNAVAILABLE
262+
263+
mock_tractive_client.send_switch_event(mock_config_entry)
264+
await hass.async_block_till_done()
265+
266+
state = hass.states.get(entity_id)
267+
assert state
268+
assert state.state == STATE_ON

0 commit comments

Comments
 (0)