Move to Launch Library 2 (#42723)
* Move to Launch Library 2 * isort * Apply suggestions from code review * Add const to .coveragerc
This commit is contained in:
parent
cd675f9175
commit
a199d7af59
5 changed files with 52 additions and 36 deletions
|
@ -460,6 +460,7 @@ omit =
|
||||||
homeassistant/components/lametric/*
|
homeassistant/components/lametric/*
|
||||||
homeassistant/components/lannouncer/notify.py
|
homeassistant/components/lannouncer/notify.py
|
||||||
homeassistant/components/lastfm/sensor.py
|
homeassistant/components/lastfm/sensor.py
|
||||||
|
homeassistant/components/launch_library/const.py
|
||||||
homeassistant/components/launch_library/sensor.py
|
homeassistant/components/launch_library/sensor.py
|
||||||
homeassistant/components/lcn/*
|
homeassistant/components/lcn/*
|
||||||
homeassistant/components/lg_netcast/media_player.py
|
homeassistant/components/lg_netcast/media_player.py
|
||||||
|
|
10
homeassistant/components/launch_library/const.py
Normal file
10
homeassistant/components/launch_library/const.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
"""Constants for launch_library."""
|
||||||
|
|
||||||
|
ATTR_AGENCY = "agency"
|
||||||
|
ATTR_AGENCY_COUNTRY_CODE = "agency_country_code"
|
||||||
|
ATTR_LAUNCH_TIME = "launch_time"
|
||||||
|
ATTR_STREAM = "stream"
|
||||||
|
|
||||||
|
ATTRIBUTION = "Data provided by Launch Library."
|
||||||
|
|
||||||
|
DEFAULT_NAME = "Next launch"
|
|
@ -2,6 +2,6 @@
|
||||||
"domain": "launch_library",
|
"domain": "launch_library",
|
||||||
"name": "Launch Library",
|
"name": "Launch Library",
|
||||||
"documentation": "https://www.home-assistant.io/integrations/launch_library",
|
"documentation": "https://www.home-assistant.io/integrations/launch_library",
|
||||||
"requirements": ["pylaunches==0.2.0"],
|
"requirements": ["pylaunches==1.0.0"],
|
||||||
"codeowners": ["@ludeeus"]
|
"codeowners": ["@ludeeus"]
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
"""A sensor platform that give you information about the next space launch."""
|
"""A sensor platform that give you information about the next space launch."""
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from pylaunches.api import Launches
|
from pylaunches import PyLaunches, PyLaunchesException
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
|
@ -11,12 +12,17 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
|
from .const import (
|
||||||
|
ATTR_AGENCY,
|
||||||
|
ATTR_AGENCY_COUNTRY_CODE,
|
||||||
|
ATTR_LAUNCH_TIME,
|
||||||
|
ATTR_STREAM,
|
||||||
|
ATTRIBUTION,
|
||||||
|
DEFAULT_NAME,
|
||||||
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
ATTRIBUTION = "Data provided by Launch Library."
|
|
||||||
|
|
||||||
DEFAULT_NAME = "Next launch"
|
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(hours=1)
|
SCAN_INTERVAL = timedelta(hours=1)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
|
@ -26,59 +32,58 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
|
|
||||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||||
"""Create the launch sensor."""
|
"""Create the launch sensor."""
|
||||||
|
|
||||||
name = config[CONF_NAME]
|
name = config[CONF_NAME]
|
||||||
|
|
||||||
session = async_get_clientsession(hass)
|
session = async_get_clientsession(hass)
|
||||||
launches = Launches(hass.loop, session)
|
launches = PyLaunches(session)
|
||||||
sensor = [LaunchLibrarySensor(launches, name)]
|
|
||||||
async_add_entities(sensor, True)
|
async_add_entities([LaunchLibrarySensor(launches, name)], True)
|
||||||
|
|
||||||
|
|
||||||
class LaunchLibrarySensor(Entity):
|
class LaunchLibrarySensor(Entity):
|
||||||
"""Representation of a launch_library Sensor."""
|
"""Representation of a launch_library Sensor."""
|
||||||
|
|
||||||
def __init__(self, launches, name):
|
def __init__(self, launches: PyLaunches, name: str) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self.launches = launches
|
self.launches = launches
|
||||||
self._attributes = {}
|
self.next_launch = None
|
||||||
self._name = name
|
self._name = name
|
||||||
self._state = None
|
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self) -> None:
|
||||||
"""Get the latest data."""
|
"""Get the latest data."""
|
||||||
await self.launches.get_launches()
|
|
||||||
if self.launches.launches is None:
|
|
||||||
_LOGGER.error("No data received")
|
|
||||||
return
|
|
||||||
try:
|
try:
|
||||||
data = self.launches.launches[0]
|
launches = await self.launches.upcoming_launches()
|
||||||
self._state = data["name"]
|
except PyLaunchesException as exception:
|
||||||
self._attributes["launch_time"] = data["start"]
|
_LOGGER.error("Error getting data, %s", exception)
|
||||||
self._attributes["agency"] = data["agency"]
|
else:
|
||||||
agency_country_code = data["agency_country_code"]
|
if launches:
|
||||||
self._attributes["agency_country_code"] = agency_country_code
|
self.next_launch = launches[0]
|
||||||
self._attributes["stream"] = data["stream"]
|
|
||||||
self._attributes[ATTR_ATTRIBUTION] = ATTRIBUTION
|
|
||||||
except (KeyError, IndexError) as error:
|
|
||||||
_LOGGER.debug("Error getting data, %s", error)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self) -> str:
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self) -> Optional[str]:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self._state
|
if self.next_launch:
|
||||||
|
return self.next_launch.name
|
||||||
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self) -> str:
|
||||||
"""Return the icon of the sensor."""
|
"""Return the icon of the sensor."""
|
||||||
return "mdi:rocket"
|
return "mdi:rocket"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self) -> Optional[dict]:
|
||||||
"""Return attributes for the sensor."""
|
"""Return attributes for the sensor."""
|
||||||
return self._attributes
|
if self.next_launch:
|
||||||
|
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,
|
||||||
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||||
|
}
|
||||||
|
return None
|
||||||
|
|
|
@ -1476,7 +1476,7 @@ pylacrosse==0.4
|
||||||
pylast==3.3.0
|
pylast==3.3.0
|
||||||
|
|
||||||
# homeassistant.components.launch_library
|
# homeassistant.components.launch_library
|
||||||
pylaunches==0.2.0
|
pylaunches==1.0.0
|
||||||
|
|
||||||
# homeassistant.components.lg_netcast
|
# homeassistant.components.lg_netcast
|
||||||
pylgnetcast-homeassistant==0.2.0.dev0
|
pylgnetcast-homeassistant==0.2.0.dev0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue