-
-
Notifications
You must be signed in to change notification settings - Fork 37.2k
Add new launch sensor to keep track of space launches. #18274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da0cfec
Add new launch sensor to keep track of space launches.
ludeeus f13ee5d
Added attribution to Launch Library.
ludeeus 16c7af5
Adds data class and throtle, reuse aiohttp session.
ludeeus afeca97
Add one extra blank line before the new class..
ludeeus dac1a14
Change throttle to simpler SCAN_INTERVAL.
ludeeus 9862e19
Remove the usage of the LaunchData class.
ludeeus a518a01
Bump pylaunches, remove . from log, fix line breaker for agency_count…
ludeeus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.