1 """Support for Tractive switches."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
7 from typing
import Any, Literal, cast
9 from aiotractive.exceptions
import TractiveError
16 from .
import Trackables, TractiveClient, TractiveConfigEntry
21 TRACKER_SWITCH_STATUS_UPDATED,
23 from .entity
import TractiveEntity
25 _LOGGER = logging.getLogger(__name__)
28 @dataclass(frozen=True, kw_only=True)
30 """Class describing Tractive switch entities."""
32 method: Literal[
"async_set_buzzer",
"async_set_led",
"async_set_live_tracking"]
35 SWITCH_TYPES: tuple[TractiveSwitchEntityDescription, ...] = (
38 translation_key=
"tracker_buzzer",
39 method=
"async_set_buzzer",
40 entity_category=EntityCategory.CONFIG,
44 translation_key=
"tracker_led",
45 method=
"async_set_led",
46 entity_category=EntityCategory.CONFIG,
49 key=ATTR_LIVE_TRACKING,
50 translation_key=
"live_tracking",
51 method=
"async_set_live_tracking",
52 entity_category=EntityCategory.CONFIG,
59 entry: TractiveConfigEntry,
60 async_add_entities: AddEntitiesCallback,
62 """Set up Tractive switches."""
63 client = entry.runtime_data.client
64 trackables = entry.runtime_data.trackables
68 for description
in SWITCH_TYPES
69 for item
in trackables
76 """Tractive switch."""
78 entity_description: TractiveSwitchEntityDescription
82 client: TractiveClient,
84 description: TractiveSwitchEntityDescription,
86 """Initialize switch entity."""
91 f
"{TRACKER_SWITCH_STATUS_UPDATED}-{item.tracker_details['_id']}",
94 self.
_attr_unique_id_attr_unique_id = f
"{item.trackable['_id']}_{description.key}"
96 self.
_method_method = getattr(self, description.method)
101 """Handle status update."""
113 """Turn on a switch."""
115 result = await self.
_method_method(
True)
116 except TractiveError
as error:
120 if result[
"pending"]:
125 """Turn off a switch."""
127 result = await self.
_method_method(
False)
128 except TractiveError
as error:
132 if result[
"pending"]:
137 """Set the buzzer on/off."""
138 return cast(dict[str, Any], await self.
_tracker_tracker.set_buzzer_active(active))
141 """Set the LED on/off."""
142 return cast(dict[str, Any], await self.
_tracker_tracker.set_led_active(active))
145 """Set the live tracking on/off."""
147 dict[str, Any], await self.
_tracker_tracker.set_live_tracking_active(active)
dict[str, Any] async_set_buzzer(self, bool active)
None __init__(self, TractiveClient client, Trackables item, TractiveSwitchEntityDescription description)
None async_turn_off(self, **Any kwargs)
dict[str, Any] async_set_led(self, bool active)
dict[str, Any] async_set_live_tracking(self, bool active)
None handle_status_update(self, dict[str, Any] event)
None async_turn_on(self, **Any kwargs)
None async_write_ha_state(self)
None async_setup_entry(HomeAssistant hass, TractiveConfigEntry entry, AddEntitiesCallback async_add_entities)