mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Mark Tractive switches as unavailable when tacker is in the enegy saving zone (#151817)
This commit is contained in:
@@ -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
|
||||
)
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user