1 """Support for Launch Library sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from datetime
import datetime
10 from pylaunches.types
import Event, Launch
15 SensorEntityDescription,
24 DataUpdateCoordinator,
28 from .
import LaunchLibraryData
29 from .const
import DOMAIN
31 DEFAULT_NEXT_LAUNCH_NAME =
"Next launch"
34 @dataclass(frozen=True, kw_only=True)
36 """Describes a Next Launch sensor entity."""
38 value_fn: Callable[[Launch | Event], datetime | int | str |
None]
39 attributes_fn: Callable[[Launch | Event], dict[str, Any] |
None]
42 SENSOR_DESCRIPTIONS: tuple[LaunchLibrarySensorEntityDescription, ...] = (
45 icon=
"mdi:rocket-launch",
46 translation_key=
"next_launch",
47 value_fn=
lambda nl: nl[
"name"],
48 attributes_fn=
lambda nl: {
49 "provider": nl[
"launch_service_provider"][
"name"],
50 "pad": nl[
"pad"][
"name"],
51 "facility": nl[
"pad"][
"location"][
"name"],
52 "provider_country_code": nl[
"pad"][
"location"][
"country_code"],
57 icon=
"mdi:clock-outline",
58 translation_key=
"launch_time",
59 device_class=SensorDeviceClass.TIMESTAMP,
61 attributes_fn=
lambda nl: {
62 "window_start": nl[
"window_start"],
63 "window_end": nl[
"window_end"],
64 "stream_live": nl[
"window_start"],
68 key=
"launch_probability",
69 icon=
"mdi:dice-multiple",
70 translation_key=
"launch_probability",
71 native_unit_of_measurement=PERCENTAGE,
72 value_fn=
lambda nl:
None if nl[
"probability"] == -1
else nl[
"probability"],
73 attributes_fn=
lambda nl:
None,
77 icon=
"mdi:rocket-launch",
78 translation_key=
"launch_status",
79 value_fn=
lambda nl: nl[
"status"][
"name"],
80 attributes_fn=
lambda nl: {
"reason": nl.get(
"holdreason")},
85 translation_key=
"launch_mission",
86 value_fn=
lambda nl: nl[
"mission"][
"name"],
87 attributes_fn=
lambda nl: {
88 "mission_type": nl[
"mission"][
"type"],
89 "target_orbit": nl[
"mission"][
"orbit"][
"name"],
90 "description": nl[
"mission"][
"description"],
94 key=
"starship_launch",
96 translation_key=
"starship_launch",
97 device_class=SensorDeviceClass.TIMESTAMP,
99 attributes_fn=
lambda sl: {
100 "title": sl[
"mission"][
"name"],
101 "status": sl[
"status"][
"name"],
102 "target_orbit": sl[
"mission"][
"orbit"][
"name"],
103 "description": sl[
"mission"][
"description"],
107 key=
"starship_event",
109 translation_key=
"starship_event",
110 device_class=SensorDeviceClass.TIMESTAMP,
112 attributes_fn=
lambda se: {
114 "location": se[
"location"],
115 "stream": se[
"video_url"],
116 "description": se[
"description"],
125 async_add_entities: AddEntitiesCallback,
127 """Set up the sensor platform."""
128 name = entry.data.get(CONF_NAME, DEFAULT_NEXT_LAUNCH_NAME)
129 coordinator: DataUpdateCoordinator[LaunchLibraryData] = hass.data[DOMAIN]
133 coordinator=coordinator,
134 entry_id=entry.entry_id,
135 description=description,
138 for description
in SENSOR_DESCRIPTIONS
143 CoordinatorEntity[DataUpdateCoordinator[LaunchLibraryData]], SensorEntity
145 """Representation of the next launch sensors."""
147 _attr_attribution =
"Data provided by Launch Library."
148 _attr_has_entity_name =
True
149 _next_event: Launch | Event |
None =
None
150 entity_description: LaunchLibrarySensorEntityDescription
154 coordinator: DataUpdateCoordinator[LaunchLibraryData],
156 description: LaunchLibrarySensorEntityDescription,
159 """Initialize a Launch Library sensor."""
164 identifiers={(DOMAIN, entry_id)},
165 entry_type=DeviceEntryType.SERVICE,
171 """Return the state of the sensor."""
178 """Return the attributes of the sensor."""
185 """Return if the sensor is available."""
186 return super().available
and self.
_next_event_next_event
is not None
190 """Handle updated data from the coordinator."""
192 events = self.coordinator.data[
"starship_events"][
"upcoming"][
"launches"]
194 events = self.coordinator.data[
"starship_events"][
"upcoming"][
"events"]
196 events = self.coordinator.data[
"upcoming_launches"]
198 self.
_next_event_next_event = next((event
for event
in (events)),
None)
202 """When entity is added to hass."""
None async_added_to_hass(self)
dict[str, Any]|None extra_state_attributes(self)
None _handle_coordinator_update(self)
datetime|str|int|None native_value(self)
None __init__(self, DataUpdateCoordinator[LaunchLibraryData] coordinator, str entry_id, LaunchLibrarySensorEntityDescription description, str name)
datetime|None parse_datetime(str|None value)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)