From fd2987e551c466eaaa866f1e7eb911931ae9d7e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Thu, 8 Nov 2018 16:37:11 +0100 Subject: [PATCH] Add new launch sensor to keep track of space launches. (#18274) * Add new launch sensor to keep track of space launches. * Added attribution to Launch Library. * Adds data class and throtle, reuse aiohttp session. * Add one extra blank line before the new class.. * Change throttle to simpler SCAN_INTERVAL. * Remove the usage of the LaunchData class. * Bump pylaunches, remove . from log, fix line breaker for agency_country_code, remove CONF_ from ATTRIBUTION. --- .coveragerc | 1 + homeassistant/components/sensor/launch.py | 92 +++++++++++++++++++++++ requirements_all.txt | 3 + 3 files changed, 96 insertions(+) create mode 100644 homeassistant/components/sensor/launch.py diff --git a/.coveragerc b/.coveragerc index 8d037f0c2e7..727e57149ba 100644 --- a/.coveragerc +++ b/.coveragerc @@ -728,6 +728,7 @@ omit = homeassistant/components/sensor/kwb.py homeassistant/components/sensor/lacrosse.py homeassistant/components/sensor/lastfm.py + homeassistant/components/sensor/launch.py homeassistant/components/sensor/linky.py homeassistant/components/sensor/linux_battery.py homeassistant/components/sensor/loopenergy.py diff --git a/homeassistant/components/sensor/launch.py b/homeassistant/components/sensor/launch.py new file mode 100644 index 00000000000..ce883d94cd3 --- /dev/null +++ b/homeassistant/components/sensor/launch.py @@ -0,0 +1,92 @@ +""" +A sensor platform that give you information about the next space launch. + +For more details about this platform, please refer to the documentation at +https://www.home-assistant.io/components/sensor.launch/ +""" +from datetime import timedelta +import logging + +import voluptuous as vol + +import homeassistant.helpers.config_validation as cv +from homeassistant.components.sensor import PLATFORM_SCHEMA +from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME +from homeassistant.helpers.entity import Entity +from homeassistant.helpers.aiohttp_client import async_get_clientsession + +REQUIREMENTS = ['pylaunches==0.1.2'] + +_LOGGER = logging.getLogger(__name__) + +ATTRIBUTION = "Data provided by Launch Library." + +DEFAULT_NAME = 'Launch' + +SCAN_INTERVAL = timedelta(hours=1) + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, + }) + + +async def async_setup_platform( + hass, config, async_add_entities, discovery_info=None): + """Create the launch sensor.""" + from pylaunches.api import Launches + + name = config[CONF_NAME] + + session = async_get_clientsession(hass) + launches = Launches(hass.loop, session) + sensor = [LaunchSensor(launches, name)] + async_add_entities(sensor, True) + + +class LaunchSensor(Entity): + """Representation of a launch Sensor.""" + + def __init__(self, launches, name): + """Initialize the sensor.""" + self.launches = launches + self._attributes = {} + self._name = name + self._state = None + + async def async_update(self): + """Get the latest data.""" + await self.launches.get_launches() + if self.launches.launches is None: + _LOGGER.error("No data recieved") + return + try: + data = self.launches.launches[0] + self._state = data['name'] + self._attributes['launch_time'] = data['start'] + self._attributes['agency'] = data['agency'] + agency_country_code = data['agency_country_code'] + self._attributes['agency_country_code'] = agency_country_code + self._attributes['stream'] = data['stream'] + self._attributes[ATTR_ATTRIBUTION] = ATTRIBUTION + except (KeyError, IndexError) as error: + _LOGGER.debug("Error getting data, %s", error) + + @property + def name(self): + """Return the name of the sensor.""" + return self._name + + @property + def state(self): + """Return the state of the sensor.""" + return self._state + + @property + def icon(self): + """Return the icon of the sensor.""" + return 'mdi:rocket' + + @property + def device_state_attributes(self): + """Return attributes for the sensor.""" + return self._attributes diff --git a/requirements_all.txt b/requirements_all.txt index 1f62dd3ef3f..e23e6e7ff77 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -985,6 +985,9 @@ pylacrosse==0.3.1 # homeassistant.components.sensor.lastfm pylast==2.4.0 +# homeassistant.components.sensor.launch +pylaunches==0.1.2 + # homeassistant.components.media_player.lg_netcast pylgnetcast-homeassistant==0.2.0.dev0