2022-01-08 11:47:16 +01:00
|
|
|
"""Support for Launch Library sensors."""
|
2021-03-18 13:07:04 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-01-18 20:58:36 +01:00
|
|
|
from collections.abc import Callable
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from datetime import datetime
|
2018-11-08 16:37:11 +01:00
|
|
|
import logging
|
2022-01-08 11:47:16 +01:00
|
|
|
from typing import Any
|
2018-11-08 16:37:11 +01:00
|
|
|
|
2022-01-08 11:47:16 +01:00
|
|
|
from pylaunches.objects.launch import Launch
|
2018-11-08 16:37:11 +01:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2022-01-12 14:44:29 +01:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
2022-01-18 20:58:36 +01:00
|
|
|
SensorDeviceClass,
|
2022-01-12 14:44:29 +01:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2022-01-08 11:47:16 +01:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2022-01-18 20:58:36 +01:00
|
|
|
from homeassistant.const import CONF_NAME, PERCENTAGE, STATE_UNKNOWN
|
2022-01-08 11:47:16 +01:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2019-12-04 11:11:21 +01:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-03 19:11:50 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2022-01-08 11:47:16 +01:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
2022-01-18 20:58:36 +01:00
|
|
|
from homeassistant.util.dt import parse_datetime
|
2018-11-08 16:37:11 +01:00
|
|
|
|
2020-11-05 15:42:12 +01:00
|
|
|
from .const import (
|
2022-01-18 20:58:36 +01:00
|
|
|
ATTR_LAUNCH_FACILITY,
|
|
|
|
ATTR_LAUNCH_PAD,
|
|
|
|
ATTR_LAUNCH_PAD_COUNTRY_CODE,
|
|
|
|
ATTR_LAUNCH_PROVIDER,
|
2022-01-18 22:39:37 +01:00
|
|
|
ATTR_REASON,
|
2022-01-18 20:58:36 +01:00
|
|
|
ATTR_STREAM_LIVE,
|
|
|
|
ATTR_WINDOW_END,
|
|
|
|
ATTR_WINDOW_START,
|
2020-11-05 15:42:12 +01:00
|
|
|
ATTRIBUTION,
|
|
|
|
DEFAULT_NAME,
|
2022-01-08 11:47:16 +01:00
|
|
|
DOMAIN,
|
2022-01-18 20:58:36 +01:00
|
|
|
LAUNCH_PROBABILITY,
|
2022-01-18 22:39:37 +01:00
|
|
|
LAUNCH_STATUS,
|
2022-01-18 20:58:36 +01:00
|
|
|
LAUNCH_TIME,
|
|
|
|
NEXT_LAUNCH,
|
2020-11-05 15:42:12 +01:00
|
|
|
)
|
2018-11-08 16:37:11 +01:00
|
|
|
|
2020-11-05 15:42:12 +01:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-11-08 16:37:11 +01:00
|
|
|
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}
|
|
|
|
)
|
2018-11-08 16:37:11 +01:00
|
|
|
|
|
|
|
|
2022-01-18 20:58:36 +01:00
|
|
|
@dataclass
|
|
|
|
class NextLaunchSensorEntityDescriptionMixin:
|
|
|
|
"""Mixin for required keys."""
|
|
|
|
|
|
|
|
value_fn: Callable[[Launch], datetime | int | str | None]
|
|
|
|
attributes_fn: Callable[[Launch], dict[str, Any] | None]
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class NextLaunchSensorEntityDescription(
|
|
|
|
SensorEntityDescription, NextLaunchSensorEntityDescriptionMixin
|
|
|
|
):
|
|
|
|
"""Describes a Next Launch sensor entity."""
|
|
|
|
|
|
|
|
|
|
|
|
SENSOR_DESCRIPTIONS: tuple[NextLaunchSensorEntityDescription, ...] = (
|
|
|
|
NextLaunchSensorEntityDescription(
|
|
|
|
key=NEXT_LAUNCH,
|
|
|
|
icon="mdi:rocket-launch",
|
|
|
|
name=DEFAULT_NAME,
|
|
|
|
value_fn=lambda next_launch: next_launch.name,
|
|
|
|
attributes_fn=lambda next_launch: {
|
|
|
|
ATTR_LAUNCH_PROVIDER: next_launch.launch_service_provider.name,
|
|
|
|
ATTR_LAUNCH_PAD: next_launch.pad.name,
|
|
|
|
ATTR_LAUNCH_FACILITY: next_launch.pad.location.name,
|
|
|
|
ATTR_LAUNCH_PAD_COUNTRY_CODE: next_launch.pad.location.country_code,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
NextLaunchSensorEntityDescription(
|
|
|
|
key=LAUNCH_TIME,
|
|
|
|
icon="mdi:clock-outline",
|
|
|
|
name="Launch time",
|
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
|
|
|
value_fn=lambda next_launch: parse_datetime(next_launch.net),
|
|
|
|
attributes_fn=lambda next_launch: {
|
|
|
|
ATTR_WINDOW_START: next_launch.window_start,
|
|
|
|
ATTR_WINDOW_END: next_launch.window_end,
|
|
|
|
ATTR_STREAM_LIVE: next_launch.webcast_live,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
NextLaunchSensorEntityDescription(
|
|
|
|
key=LAUNCH_PROBABILITY,
|
|
|
|
icon="mdi:dice-multiple",
|
|
|
|
name="Launch Probability",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
value_fn=lambda next_launch: next_launch.probability
|
|
|
|
if next_launch.probability != -1
|
|
|
|
else STATE_UNKNOWN,
|
|
|
|
attributes_fn=lambda next_launch: None,
|
|
|
|
),
|
2022-01-18 22:39:37 +01:00
|
|
|
NextLaunchSensorEntityDescription(
|
|
|
|
key=LAUNCH_STATUS,
|
|
|
|
icon="mdi:rocket-launch",
|
|
|
|
name="Launch status",
|
|
|
|
value_fn=lambda next_launch: next_launch.status.name,
|
|
|
|
attributes_fn=lambda next_launch: {
|
|
|
|
ATTR_REASON: next_launch.holdreason,
|
|
|
|
}
|
|
|
|
if next_launch.inhold
|
|
|
|
else None,
|
|
|
|
),
|
2022-01-12 14:44:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-03 19:11:50 +01:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2022-01-08 11:47:16 +01:00
|
|
|
"""Import Launch Library configuration from yaml."""
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Configuration of the launch_library platform in YAML is deprecated and will be "
|
|
|
|
"removed in Home Assistant 2022.4; Your existing configuration "
|
|
|
|
"has been imported into the UI automatically and can be safely removed "
|
|
|
|
"from your configuration.yaml file"
|
|
|
|
)
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data=config,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the sensor platform."""
|
|
|
|
name = entry.data.get(CONF_NAME, DEFAULT_NAME)
|
|
|
|
coordinator = hass.data[DOMAIN]
|
2018-11-08 16:37:11 +01:00
|
|
|
|
2022-01-08 11:47:16 +01:00
|
|
|
async_add_entities(
|
2022-01-18 20:58:36 +01:00
|
|
|
NextLaunchSensor(
|
|
|
|
coordinator=coordinator,
|
|
|
|
entry_id=entry.entry_id,
|
|
|
|
description=description,
|
|
|
|
name=name if description.key == NEXT_LAUNCH else None,
|
|
|
|
)
|
|
|
|
for description in SENSOR_DESCRIPTIONS
|
2022-01-08 11:47:16 +01:00
|
|
|
)
|
2018-11-08 16:37:11 +01:00
|
|
|
|
2021-08-10 13:11:12 +02:00
|
|
|
|
2022-01-08 11:47:16 +01:00
|
|
|
class NextLaunchSensor(CoordinatorEntity, SensorEntity):
|
2022-01-18 20:58:36 +01:00
|
|
|
"""Representation of the next launch sensors."""
|
2022-01-08 11:47:16 +01:00
|
|
|
|
|
|
|
_attr_attribution = ATTRIBUTION
|
|
|
|
_next_launch: Launch | None = None
|
2022-01-18 20:58:36 +01:00
|
|
|
entity_description: NextLaunchSensorEntityDescription
|
2022-01-08 11:47:16 +01:00
|
|
|
|
|
|
|
def __init__(
|
2022-01-12 14:44:29 +01:00
|
|
|
self,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
entry_id: str,
|
2022-01-18 20:58:36 +01:00
|
|
|
description: NextLaunchSensorEntityDescription,
|
|
|
|
name: str | None = None,
|
2022-01-08 11:47:16 +01:00
|
|
|
) -> None:
|
2022-01-18 20:58:36 +01:00
|
|
|
"""Initialize a Launch Library sensor."""
|
2022-01-08 11:47:16 +01:00
|
|
|
super().__init__(coordinator)
|
2022-01-18 20:58:36 +01:00
|
|
|
if name:
|
|
|
|
self._attr_name = name
|
2022-01-12 14:44:29 +01:00
|
|
|
self._attr_unique_id = f"{entry_id}_{description.key}"
|
|
|
|
self.entity_description = description
|
|
|
|
|
|
|
|
@property
|
2022-01-18 20:58:36 +01:00
|
|
|
def native_value(self) -> datetime | str | int | None:
|
2022-01-08 11:47:16 +01:00
|
|
|
"""Return the state of the sensor."""
|
|
|
|
if self._next_launch is None:
|
|
|
|
return None
|
2022-01-18 20:58:36 +01:00
|
|
|
return self.entity_description.value_fn(self._next_launch)
|
2022-01-08 11:47:16 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
|
|
|
"""Return the attributes of the sensor."""
|
|
|
|
if self._next_launch is None:
|
|
|
|
return None
|
2022-01-18 20:58:36 +01:00
|
|
|
return self.entity_description.attributes_fn(self._next_launch)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if the sensor is available."""
|
|
|
|
return super().available and self._next_launch is not None
|
2022-01-08 11:47:16 +01:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle updated data from the coordinator."""
|
|
|
|
self._next_launch = next((launch for launch in self.coordinator.data), None)
|
|
|
|
super()._handle_coordinator_update()
|
2018-11-08 16:37:11 +01:00
|
|
|
|
2022-01-08 11:47:16 +01: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()
|