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
|
|
|
|
|
2018-11-08 15:37:11 +00:00
|
|
|
import logging
|
2022-01-08 10:47:16 +00:00
|
|
|
from typing import Any
|
2018-11-08 15:37:11 +00:00
|
|
|
|
2022-01-08 10:47:16 +00:00
|
|
|
from pylaunches.objects.launch import Launch
|
2018-11-08 15:37:11 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2022-01-08 10:47:16 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
|
|
|
from homeassistant.const import CONF_NAME
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2019-12-04 10:11:21 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-03 18:11:50 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2022-01-08 10:47:16 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
2018-11-08 15:37:11 +00:00
|
|
|
|
2020-11-05 14:42:12 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_AGENCY,
|
|
|
|
ATTR_AGENCY_COUNTRY_CODE,
|
|
|
|
ATTR_LAUNCH_TIME,
|
|
|
|
ATTR_STREAM,
|
|
|
|
ATTRIBUTION,
|
|
|
|
DEFAULT_NAME,
|
2022-01-08 10:47:16 +00:00
|
|
|
DOMAIN,
|
2020-11-05 14:42:12 +00:00
|
|
|
)
|
2018-11-08 15:37:11 +00:00
|
|
|
|
2020-11-05 14:42:12 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-11-08 15:37:11 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}
|
|
|
|
)
|
2018-11-08 15:37:11 +00:00
|
|
|
|
|
|
|
|
2022-01-03 18:11:50 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2022-01-08 10:47:16 +00: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."""
|
2020-11-05 14:42:12 +00:00
|
|
|
|
2022-01-08 10:47:16 +00:00
|
|
|
name = entry.data.get(CONF_NAME, DEFAULT_NAME)
|
2018-11-08 15:37:11 +00:00
|
|
|
|
2022-01-08 10:47:16 +00:00
|
|
|
coordinator = hass.data[DOMAIN]
|
2018-11-08 15:37:11 +00:00
|
|
|
|
2022-01-08 10:47:16 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
NextLaunchSensor(coordinator, entry.entry_id, name),
|
|
|
|
]
|
|
|
|
)
|
2018-11-08 15:37:11 +00:00
|
|
|
|
2021-08-10 11:11:12 +00:00
|
|
|
|
2022-01-08 10:47:16 +00:00
|
|
|
class NextLaunchSensor(CoordinatorEntity, SensorEntity):
|
|
|
|
"""Representation of the next launch sensor."""
|
|
|
|
|
|
|
|
_attr_attribution = ATTRIBUTION
|
|
|
|
_attr_icon = "mdi:rocket-launch"
|
|
|
|
_next_launch: Launch | None = None
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, coordinator: DataUpdateCoordinator, entry_id: str, name: str
|
|
|
|
) -> None:
|
|
|
|
"""Initialize a Launch Library entity."""
|
|
|
|
super().__init__(coordinator)
|
2021-08-10 11:11:12 +00:00
|
|
|
self._attr_name = name
|
2022-01-08 10:47:16 +00:00
|
|
|
self._attr_unique_id = f"{entry_id}_next_launch"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self) -> str | None:
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
if self._next_launch is None:
|
|
|
|
return None
|
|
|
|
return self._next_launch.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if the sensor is available."""
|
|
|
|
return super().available and self._next_launch is not None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
|
|
|
"""Return the attributes of the sensor."""
|
|
|
|
if self._next_launch is None:
|
|
|
|
return None
|
|
|
|
return {
|
|
|
|
ATTR_LAUNCH_TIME: self._next_launch.net,
|
|
|
|
ATTR_AGENCY: self._next_launch.launch_service_provider.name,
|
|
|
|
ATTR_AGENCY_COUNTRY_CODE: self._next_launch.pad.location.country_code,
|
|
|
|
ATTR_STREAM: self._next_launch.webcast_live,
|
|
|
|
}
|
|
|
|
|
|
|
|
@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 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()
|