From da0cfecb9077d191492efa0bdc5190bdba2a7ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Tue, 6 Nov 2018 19:24:54 +0100 Subject: [PATCH 1/7] Add new launch sensor to keep track of space launches. --- .coveragerc | 1 + homeassistant/components/sensor/launch.py | 84 +++++++++++++++++++++++ requirements_all.txt | 3 + 3 files changed, 88 insertions(+) create mode 100644 homeassistant/components/sensor/launch.py diff --git a/.coveragerc b/.coveragerc index 599e155f8f3c28..0e1fe343976b70 100644 --- a/.coveragerc +++ b/.coveragerc @@ -715,6 +715,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 00000000000000..3711c09e662f40 --- /dev/null +++ b/homeassistant/components/sensor/launch.py @@ -0,0 +1,84 @@ +""" +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/ +""" +import logging + +import voluptuous as vol + +import homeassistant.helpers.config_validation as cv +from homeassistant.components.sensor import PLATFORM_SCHEMA +from homeassistant.const import CONF_NAME +from homeassistant.helpers.entity import Entity + +REQUIREMENTS = ['pylaunches==0.0.2'] + +_LOGGER = logging.getLogger(__name__) + +DEFAULT_NAME = 'Launch' + +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] + + launches = Launches(hass.loop) + 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'] + self._attributes['agency_country_code'] = (data + ['agency_country_code']) + self._attributes['stream'] = data['stream'] + 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 8dd576e95830cf..e13d03520f314f 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -957,6 +957,9 @@ pylacrosse==0.3.1 # homeassistant.components.sensor.lastfm pylast==2.4.0 +# homeassistant.components.sensor.launch +pylaunches==0.0.2 + # homeassistant.components.media_player.lg_netcast pylgnetcast-homeassistant==0.2.0.dev0 From f13ee5dd0423fcf154af0c9f0f288003833bb7d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Tue, 6 Nov 2018 22:02:27 +0100 Subject: [PATCH 2/7] Added attribution to Launch Library. --- homeassistant/components/sensor/launch.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/sensor/launch.py b/homeassistant/components/sensor/launch.py index 3711c09e662f40..31c92edd2b2a5f 100644 --- a/homeassistant/components/sensor/launch.py +++ b/homeassistant/components/sensor/launch.py @@ -10,13 +10,15 @@ import homeassistant.helpers.config_validation as cv from homeassistant.components.sensor import PLATFORM_SCHEMA -from homeassistant.const import CONF_NAME +from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME from homeassistant.helpers.entity import Entity REQUIREMENTS = ['pylaunches==0.0.2'] _LOGGER = logging.getLogger(__name__) +CONF_ATTRIBUTION = "Data provided by Launch Library." + DEFAULT_NAME = 'Launch' PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ @@ -60,6 +62,7 @@ async def async_update(self): self._attributes['agency_country_code'] = (data ['agency_country_code']) self._attributes['stream'] = data['stream'] + self._attributes[ATTR_ATTRIBUTION] = CONF_ATTRIBUTION except (KeyError, IndexError) as error: _LOGGER.debug("Error getting data, %s", error) From 16c7af59e1ef2f5d41f0b838153501b4393a39a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Wed, 7 Nov 2018 19:58:21 +0100 Subject: [PATCH 3/7] Adds data class and throtle, reuse aiohttp session. --- homeassistant/components/sensor/launch.py | 28 +++++++++++++++++++---- requirements_all.txt | 2 +- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/sensor/launch.py b/homeassistant/components/sensor/launch.py index 31c92edd2b2a5f..963f7884d64427 100644 --- a/homeassistant/components/sensor/launch.py +++ b/homeassistant/components/sensor/launch.py @@ -4,6 +4,7 @@ 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 @@ -12,8 +13,10 @@ 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 +from homeassistant.util import Throttle -REQUIREMENTS = ['pylaunches==0.0.2'] +REQUIREMENTS = ['pylaunches==0.1.1'] _LOGGER = logging.getLogger(__name__) @@ -21,6 +24,8 @@ DEFAULT_NAME = 'Launch' +TIME_BETWEEN_UPDATES = timedelta(hours=1) + PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, }) @@ -33,7 +38,8 @@ async def async_setup_platform( name = config[CONF_NAME] - launches = Launches(hass.loop) + session = async_get_clientsession(hass) + launches = LaunchData(Launches(hass.loop, session)) sensor = [LaunchSensor(launches, name)] async_add_entities(sensor, True) @@ -50,12 +56,12 @@ def __init__(self, launches, name): async def async_update(self): """Get the latest data.""" - await self.launches.get_launches() - if self.launches.launches is None: + await self.launches.async_update() + if self.launches.api.launches is None: _LOGGER.error("No data recieved.") return try: - data = self.launches.launches[0] + data = self.launches.api.launches[0] self._state = data['name'] self._attributes['launch_time'] = data['start'] self._attributes['agency'] = data['agency'] @@ -85,3 +91,15 @@ def icon(self): def device_state_attributes(self): """Return attributes for the sensor.""" return self._attributes + +class LaunchData(): + """Get the latest data and update the states.""" + + def __init__(self, api): + """Initialize the data object.""" + self.api = api + + @Throttle(TIME_BETWEEN_UPDATES) + async def async_update(self): + """Get the latest launch data.""" + await self.api.get_launches() diff --git a/requirements_all.txt b/requirements_all.txt index e13d03520f314f..dadbd7b1c9e109 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -958,7 +958,7 @@ pylacrosse==0.3.1 pylast==2.4.0 # homeassistant.components.sensor.launch -pylaunches==0.0.2 +pylaunches==0.1.1 # homeassistant.components.media_player.lg_netcast pylgnetcast-homeassistant==0.2.0.dev0 From afeca97585aaadce171a89dbb274a805d7a68544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Wed, 7 Nov 2018 20:00:38 +0100 Subject: [PATCH 4/7] Add one extra blank line before the new class.. --- homeassistant/components/sensor/launch.py | 1 + 1 file changed, 1 insertion(+) diff --git a/homeassistant/components/sensor/launch.py b/homeassistant/components/sensor/launch.py index 963f7884d64427..01ca45ef5097ee 100644 --- a/homeassistant/components/sensor/launch.py +++ b/homeassistant/components/sensor/launch.py @@ -92,6 +92,7 @@ def device_state_attributes(self): """Return attributes for the sensor.""" return self._attributes + class LaunchData(): """Get the latest data and update the states.""" From dac1a1454b0c84c8f73623d1831500944b2355d4 Mon Sep 17 00:00:00 2001 From: ludeeus Date: Wed, 7 Nov 2018 22:10:47 +0100 Subject: [PATCH 5/7] Change throttle to simpler SCAN_INTERVAL. --- homeassistant/components/sensor/launch.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/homeassistant/components/sensor/launch.py b/homeassistant/components/sensor/launch.py index 01ca45ef5097ee..54613a24dc1808 100644 --- a/homeassistant/components/sensor/launch.py +++ b/homeassistant/components/sensor/launch.py @@ -14,7 +14,6 @@ from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME from homeassistant.helpers.entity import Entity from homeassistant.helpers.aiohttp_client import async_get_clientsession -from homeassistant.util import Throttle REQUIREMENTS = ['pylaunches==0.1.1'] @@ -24,7 +23,7 @@ DEFAULT_NAME = 'Launch' -TIME_BETWEEN_UPDATES = timedelta(hours=1) +SCAN_INTERVAL = timedelta(hours=1) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, @@ -100,7 +99,6 @@ def __init__(self, api): """Initialize the data object.""" self.api = api - @Throttle(TIME_BETWEEN_UPDATES) async def async_update(self): """Get the latest launch data.""" await self.api.get_launches() From 9862e198fc45b292c46788f4b55e8c3a50527db5 Mon Sep 17 00:00:00 2001 From: ludeeus Date: Wed, 7 Nov 2018 22:28:34 +0100 Subject: [PATCH 6/7] Remove the usage of the LaunchData class. --- homeassistant/components/sensor/launch.py | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/sensor/launch.py b/homeassistant/components/sensor/launch.py index 54613a24dc1808..669cfcb64221bb 100644 --- a/homeassistant/components/sensor/launch.py +++ b/homeassistant/components/sensor/launch.py @@ -38,7 +38,7 @@ async def async_setup_platform( name = config[CONF_NAME] session = async_get_clientsession(hass) - launches = LaunchData(Launches(hass.loop, session)) + launches = Launches(hass.loop, session) sensor = [LaunchSensor(launches, name)] async_add_entities(sensor, True) @@ -55,12 +55,12 @@ def __init__(self, launches, name): async def async_update(self): """Get the latest data.""" - await self.launches.async_update() - if self.launches.api.launches is None: + await self.launches.get_launches() + if self.launches.launches is None: _LOGGER.error("No data recieved.") return try: - data = self.launches.api.launches[0] + data = self.launches.launches[0] self._state = data['name'] self._attributes['launch_time'] = data['start'] self._attributes['agency'] = data['agency'] @@ -90,15 +90,3 @@ def icon(self): def device_state_attributes(self): """Return attributes for the sensor.""" return self._attributes - - -class LaunchData(): - """Get the latest data and update the states.""" - - def __init__(self, api): - """Initialize the data object.""" - self.api = api - - async def async_update(self): - """Get the latest launch data.""" - await self.api.get_launches() From a518a01da91995b8abbdfaf58933b01a3a600219 Mon Sep 17 00:00:00 2001 From: ludeeus Date: Thu, 8 Nov 2018 10:18:41 +0100 Subject: [PATCH 7/7] Bump pylaunches, remove . from log, fix line breaker for agency_country_code, remove CONF_ from ATTRIBUTION. --- homeassistant/components/sensor/launch.py | 12 ++++++------ requirements_all.txt | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/sensor/launch.py b/homeassistant/components/sensor/launch.py index 669cfcb64221bb..ce883d94cd3203 100644 --- a/homeassistant/components/sensor/launch.py +++ b/homeassistant/components/sensor/launch.py @@ -15,11 +15,11 @@ from homeassistant.helpers.entity import Entity from homeassistant.helpers.aiohttp_client import async_get_clientsession -REQUIREMENTS = ['pylaunches==0.1.1'] +REQUIREMENTS = ['pylaunches==0.1.2'] _LOGGER = logging.getLogger(__name__) -CONF_ATTRIBUTION = "Data provided by Launch Library." +ATTRIBUTION = "Data provided by Launch Library." DEFAULT_NAME = 'Launch' @@ -57,17 +57,17 @@ 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.") + _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'] - self._attributes['agency_country_code'] = (data - ['agency_country_code']) + agency_country_code = data['agency_country_code'] + self._attributes['agency_country_code'] = agency_country_code self._attributes['stream'] = data['stream'] - self._attributes[ATTR_ATTRIBUTION] = CONF_ATTRIBUTION + self._attributes[ATTR_ATTRIBUTION] = ATTRIBUTION except (KeyError, IndexError) as error: _LOGGER.debug("Error getting data, %s", error) diff --git a/requirements_all.txt b/requirements_all.txt index dadbd7b1c9e109..0a67b4fce25b74 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -958,7 +958,7 @@ pylacrosse==0.3.1 pylast==2.4.0 # homeassistant.components.sensor.launch -pylaunches==0.1.1 +pylaunches==0.1.2 # homeassistant.components.media_player.lg_netcast pylgnetcast-homeassistant==0.2.0.dev0