2019-04-03 15:40:03 +00:00
|
|
|
"""A sensor platform that give you information about the next space launch."""
|
2021-03-18 12:07:04 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-11-08 15:37:11 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
2020-11-05 14:42:12 +00:00
|
|
|
from pylaunches import PyLaunches, PyLaunchesException
|
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
|
2018-11-08 15:37:11 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2019-12-04 10:11:21 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
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,
|
|
|
|
)
|
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
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(hours=1)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2018-11-08 15:37:11 +00:00
|
|
|
"""Create the launch sensor."""
|
|
|
|
name = config[CONF_NAME]
|
|
|
|
session = async_get_clientsession(hass)
|
2020-11-05 14:42:12 +00:00
|
|
|
launches = PyLaunches(session)
|
|
|
|
|
|
|
|
async_add_entities([LaunchLibrarySensor(launches, name)], True)
|
2018-11-08 15:37:11 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
class LaunchLibrarySensor(SensorEntity):
|
2018-11-09 21:24:26 +00:00
|
|
|
"""Representation of a launch_library Sensor."""
|
2018-11-08 15:37:11 +00:00
|
|
|
|
2021-08-10 11:11:12 +00:00
|
|
|
_attr_icon = "mdi:rocket"
|
|
|
|
|
|
|
|
def __init__(self, api: PyLaunches, name: str) -> None:
|
2018-11-08 15:37:11 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-08-10 11:11:12 +00:00
|
|
|
self.api = api
|
|
|
|
self._attr_name = name
|
2018-11-08 15:37:11 +00:00
|
|
|
|
2020-11-05 14:42:12 +00:00
|
|
|
async def async_update(self) -> None:
|
2018-11-08 15:37:11 +00:00
|
|
|
"""Get the latest data."""
|
|
|
|
try:
|
2021-08-10 11:11:12 +00:00
|
|
|
launches = await self.api.upcoming_launches()
|
2020-11-05 14:42:12 +00:00
|
|
|
except PyLaunchesException as exception:
|
|
|
|
_LOGGER.error("Error getting data, %s", exception)
|
2021-08-10 11:11:12 +00:00
|
|
|
self._attr_available = False
|
2020-11-05 14:42:12 +00:00
|
|
|
else:
|
2021-08-10 13:03:12 +00:00
|
|
|
if next_launch := next((launch for launch in launches), None):
|
2021-08-10 11:11:12 +00:00
|
|
|
self._attr_available = True
|
2021-08-11 19:17:47 +00:00
|
|
|
self._attr_native_value = next_launch.name
|
2021-08-10 13:03:12 +00:00
|
|
|
self._attr_extra_state_attributes = {
|
|
|
|
ATTR_LAUNCH_TIME: next_launch.net,
|
|
|
|
ATTR_AGENCY: next_launch.launch_service_provider.name,
|
|
|
|
ATTR_AGENCY_COUNTRY_CODE: next_launch.pad.location.country_code,
|
|
|
|
ATTR_STREAM: next_launch.webcast_live,
|
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
|
|
|
}
|