2022-01-08 10:47:16 +00:00
|
|
|
"""Support for Launch Library sensors."""
|
2021-03-18 12:07:04 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-01-18 19:58:36 +00:00
|
|
|
from collections.abc import Callable
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from datetime import datetime
|
2022-01-08 10:47:16 +00:00
|
|
|
from typing import Any
|
2018-11-08 15:37:11 +00:00
|
|
|
|
2022-01-26 14:37:15 +00:00
|
|
|
from pylaunches.objects.event import Event
|
2022-01-08 10:47:16 +00:00
|
|
|
from pylaunches.objects.launch import Launch
|
2018-11-08 15:37:11 +00:00
|
|
|
|
2022-01-12 13:44:29 +00:00
|
|
|
from homeassistant.components.sensor import (
|
2022-01-18 19:58:36 +00:00
|
|
|
SensorDeviceClass,
|
2022-01-12 13:44:29 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2022-03-31 18:23:52 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-01-19 08:52:22 +00:00
|
|
|
from homeassistant.const import CONF_NAME, PERCENTAGE
|
2022-01-08 10:47:16 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-01-03 18:11:50 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-01-08 10:47:16 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
2022-01-18 19:58:36 +00:00
|
|
|
from homeassistant.util.dt import parse_datetime
|
2018-11-08 15:37:11 +00:00
|
|
|
|
2022-01-26 11:31:03 +00:00
|
|
|
from . import LaunchLibraryData
|
2022-01-19 08:52:22 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
DEFAULT_NEXT_LAUNCH_NAME = "Next launch"
|
2018-11-08 15:37:11 +00:00
|
|
|
|
|
|
|
|
2022-01-18 19:58:36 +00:00
|
|
|
@dataclass
|
2022-01-26 11:31:03 +00:00
|
|
|
class LaunchLibrarySensorEntityDescriptionMixin:
|
2022-01-18 19:58:36 +00:00
|
|
|
"""Mixin for required keys."""
|
|
|
|
|
2022-01-26 14:37:15 +00:00
|
|
|
value_fn: Callable[[Launch | Event], datetime | int | str | None]
|
|
|
|
attributes_fn: Callable[[Launch | Event], dict[str, Any] | None]
|
2022-01-18 19:58:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
2022-01-26 11:31:03 +00:00
|
|
|
class LaunchLibrarySensorEntityDescription(
|
|
|
|
SensorEntityDescription, LaunchLibrarySensorEntityDescriptionMixin
|
2022-01-18 19:58:36 +00:00
|
|
|
):
|
|
|
|
"""Describes a Next Launch sensor entity."""
|
|
|
|
|
|
|
|
|
2022-01-26 11:31:03 +00:00
|
|
|
SENSOR_DESCRIPTIONS: tuple[LaunchLibrarySensorEntityDescription, ...] = (
|
|
|
|
LaunchLibrarySensorEntityDescription(
|
2022-01-19 08:52:22 +00:00
|
|
|
key="next_launch",
|
2022-01-18 19:58:36 +00:00
|
|
|
icon="mdi:rocket-launch",
|
2022-01-19 08:52:22 +00:00
|
|
|
name="Next launch",
|
2022-01-19 09:40:53 +00:00
|
|
|
value_fn=lambda nl: nl.name,
|
|
|
|
attributes_fn=lambda nl: {
|
|
|
|
"provider": nl.launch_service_provider.name,
|
|
|
|
"pad": nl.pad.name,
|
|
|
|
"facility": nl.pad.location.name,
|
|
|
|
"provider_country_code": nl.pad.location.country_code,
|
2022-01-18 19:58:36 +00:00
|
|
|
},
|
|
|
|
),
|
2022-01-26 11:31:03 +00:00
|
|
|
LaunchLibrarySensorEntityDescription(
|
2022-01-19 08:52:22 +00:00
|
|
|
key="launch_time",
|
2022-01-18 19:58:36 +00:00
|
|
|
icon="mdi:clock-outline",
|
|
|
|
name="Launch time",
|
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
2022-01-19 09:40:53 +00:00
|
|
|
value_fn=lambda nl: parse_datetime(nl.net),
|
|
|
|
attributes_fn=lambda nl: {
|
|
|
|
"window_start": nl.window_start,
|
|
|
|
"window_end": nl.window_end,
|
|
|
|
"stream_live": nl.webcast_live,
|
2022-01-18 19:58:36 +00:00
|
|
|
},
|
|
|
|
),
|
2022-01-26 11:31:03 +00:00
|
|
|
LaunchLibrarySensorEntityDescription(
|
2022-01-19 08:52:22 +00:00
|
|
|
key="launch_probability",
|
2022-01-18 19:58:36 +00:00
|
|
|
icon="mdi:dice-multiple",
|
2022-01-27 18:02:10 +00:00
|
|
|
name="Launch probability",
|
2022-01-18 19:58:36 +00:00
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
2022-01-19 09:40:53 +00:00
|
|
|
value_fn=lambda nl: None if nl.probability == -1 else nl.probability,
|
|
|
|
attributes_fn=lambda nl: None,
|
2022-01-18 19:58:36 +00:00
|
|
|
),
|
2022-01-26 11:31:03 +00:00
|
|
|
LaunchLibrarySensorEntityDescription(
|
2022-01-19 08:52:22 +00:00
|
|
|
key="launch_status",
|
2022-01-18 21:39:37 +00:00
|
|
|
icon="mdi:rocket-launch",
|
|
|
|
name="Launch status",
|
2022-01-19 09:40:53 +00:00
|
|
|
value_fn=lambda nl: nl.status.name,
|
|
|
|
attributes_fn=lambda nl: {"reason": nl.holdreason} if nl.inhold else None,
|
2022-01-18 21:39:37 +00:00
|
|
|
),
|
2022-01-26 11:31:03 +00:00
|
|
|
LaunchLibrarySensorEntityDescription(
|
2022-01-19 08:52:22 +00:00
|
|
|
key="launch_mission",
|
2022-01-19 08:28:07 +00:00
|
|
|
icon="mdi:orbit",
|
|
|
|
name="Launch mission",
|
2022-01-19 09:40:53 +00:00
|
|
|
value_fn=lambda nl: nl.mission.name,
|
|
|
|
attributes_fn=lambda nl: {
|
|
|
|
"mission_type": nl.mission.type,
|
|
|
|
"target_orbit": nl.mission.orbit.name,
|
|
|
|
"description": nl.mission.description,
|
2022-01-19 08:28:07 +00:00
|
|
|
},
|
|
|
|
),
|
2022-01-26 11:31:03 +00:00
|
|
|
LaunchLibrarySensorEntityDescription(
|
|
|
|
key="starship_launch",
|
|
|
|
icon="mdi:rocket",
|
|
|
|
name="Next Starship launch",
|
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
|
|
|
value_fn=lambda sl: parse_datetime(sl.net),
|
|
|
|
attributes_fn=lambda sl: {
|
|
|
|
"title": sl.mission.name,
|
|
|
|
"status": sl.status.name,
|
|
|
|
"target_orbit": sl.mission.orbit.name,
|
|
|
|
"description": sl.mission.description,
|
|
|
|
},
|
|
|
|
),
|
2022-01-26 14:37:15 +00:00
|
|
|
LaunchLibrarySensorEntityDescription(
|
|
|
|
key="starship_event",
|
|
|
|
icon="mdi:calendar",
|
|
|
|
name="Next Starship event",
|
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
|
|
|
value_fn=lambda se: parse_datetime(se.date),
|
|
|
|
attributes_fn=lambda se: {
|
|
|
|
"title": se.name,
|
|
|
|
"location": se.location,
|
|
|
|
"stream": se.video_url,
|
|
|
|
"description": se.description,
|
|
|
|
},
|
|
|
|
),
|
2022-01-12 13:44:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-08 10:47:16 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the sensor platform."""
|
2022-01-19 08:52:22 +00:00
|
|
|
name = entry.data.get(CONF_NAME, DEFAULT_NEXT_LAUNCH_NAME)
|
2022-01-26 11:31:03 +00:00
|
|
|
coordinator: DataUpdateCoordinator[LaunchLibraryData] = hass.data[DOMAIN]
|
2018-11-08 15:37:11 +00:00
|
|
|
|
2022-01-08 10:47:16 +00:00
|
|
|
async_add_entities(
|
2022-01-26 11:31:03 +00:00
|
|
|
LaunchLibrarySensor(
|
2022-01-18 19:58:36 +00:00
|
|
|
coordinator=coordinator,
|
|
|
|
entry_id=entry.entry_id,
|
|
|
|
description=description,
|
2022-01-19 08:52:22 +00:00
|
|
|
name=name if description.key == "next_launch" else None,
|
2022-01-18 19:58:36 +00:00
|
|
|
)
|
|
|
|
for description in SENSOR_DESCRIPTIONS
|
2022-01-08 10:47:16 +00:00
|
|
|
)
|
2018-11-08 15:37:11 +00:00
|
|
|
|
2021-08-10 11:11:12 +00:00
|
|
|
|
2022-03-21 13:14:46 +00:00
|
|
|
class LaunchLibrarySensor(
|
|
|
|
CoordinatorEntity[DataUpdateCoordinator[LaunchLibraryData]], SensorEntity
|
|
|
|
):
|
2022-01-18 19:58:36 +00:00
|
|
|
"""Representation of the next launch sensors."""
|
2022-01-08 10:47:16 +00:00
|
|
|
|
2022-01-19 08:52:22 +00:00
|
|
|
_attr_attribution = "Data provided by Launch Library."
|
2022-01-26 14:37:15 +00:00
|
|
|
_next_event: Launch | Event | None = None
|
2022-01-26 11:31:03 +00:00
|
|
|
entity_description: LaunchLibrarySensorEntityDescription
|
2022-01-08 10:47:16 +00:00
|
|
|
|
|
|
|
def __init__(
|
2022-01-12 13:44:29 +00:00
|
|
|
self,
|
2022-01-26 11:31:03 +00:00
|
|
|
coordinator: DataUpdateCoordinator[LaunchLibraryData],
|
2022-01-12 13:44:29 +00:00
|
|
|
entry_id: str,
|
2022-01-26 11:31:03 +00:00
|
|
|
description: LaunchLibrarySensorEntityDescription,
|
2022-01-18 19:58:36 +00:00
|
|
|
name: str | None = None,
|
2022-01-08 10:47:16 +00:00
|
|
|
) -> None:
|
2022-01-18 19:58:36 +00:00
|
|
|
"""Initialize a Launch Library sensor."""
|
2022-01-08 10:47:16 +00:00
|
|
|
super().__init__(coordinator)
|
2022-01-18 19:58:36 +00:00
|
|
|
if name:
|
|
|
|
self._attr_name = name
|
2022-01-12 13:44:29 +00:00
|
|
|
self._attr_unique_id = f"{entry_id}_{description.key}"
|
|
|
|
self.entity_description = description
|
|
|
|
|
|
|
|
@property
|
2022-01-18 19:58:36 +00:00
|
|
|
def native_value(self) -> datetime | str | int | None:
|
2022-01-08 10:47:16 +00:00
|
|
|
"""Return the state of the sensor."""
|
2022-01-26 14:37:15 +00:00
|
|
|
if self._next_event is None:
|
2022-01-08 10:47:16 +00:00
|
|
|
return None
|
2022-01-26 14:37:15 +00:00
|
|
|
return self.entity_description.value_fn(self._next_event)
|
2022-01-08 10:47:16 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
|
|
|
"""Return the attributes of the sensor."""
|
2022-01-26 14:37:15 +00:00
|
|
|
if self._next_event is None:
|
2022-01-08 10:47:16 +00:00
|
|
|
return None
|
2022-01-26 14:37:15 +00:00
|
|
|
return self.entity_description.attributes_fn(self._next_event)
|
2022-01-18 19:58:36 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if the sensor is available."""
|
2022-01-26 14:37:15 +00:00
|
|
|
return super().available and self._next_event is not None
|
2022-01-08 10:47:16 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle updated data from the coordinator."""
|
2022-01-26 11:31:03 +00:00
|
|
|
if self.entity_description.key == "starship_launch":
|
2022-01-26 14:37:15 +00:00
|
|
|
events = self.coordinator.data["starship_events"].upcoming.launches
|
|
|
|
elif self.entity_description.key == "starship_event":
|
|
|
|
events = self.coordinator.data["starship_events"].upcoming.events
|
2022-01-26 11:31:03 +00:00
|
|
|
else:
|
2022-01-26 14:37:15 +00:00
|
|
|
events = self.coordinator.data["upcoming_launches"]
|
2022-01-26 11:31:03 +00:00
|
|
|
|
2022-01-26 14:37:15 +00:00
|
|
|
self._next_event = next((event for event in (events)), None)
|
2022-01-08 10:47:16 +00:00
|
|
|
super()._handle_coordinator_update()
|
2018-11-08 15:37:11 +00:00
|
|
|
|
2022-01-08 10:47:16 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""When entity is added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
self._handle_coordinator_update()
|