-
-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Add Norwegian Public Transportation sensor (Ruter). #18237
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 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8966498
Add Norwegian Public Transportation sensor (Ruter).
ludeeus b332daa
Corrected typo.
ludeeus 9bba144
change stopid to stop_id, actually use attributes, changed logging, c…
ludeeus 9efcefa
Change to RuterSensor for the class, and move logic to me more readable.
ludeeus c9c16b7
Use correct sensor class.
ludeeus b0e66e0
Add return if blank list, remove else
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,95 @@ | ||
| """ | ||
| A sensor platform that give you information about next departures from Ruter. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://www.home-assistant.io/components/sensor.tautulli/ | ||
| """ | ||
| import logging | ||
| from datetime import timedelta | ||
|
|
||
| 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 = ['pyruter==1.0.2'] | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| CONF_STOPID = 'stopid' | ||
|
ludeeus marked this conversation as resolved.
Outdated
|
||
| CONF_DESTINATION = 'destination' | ||
| CONF_OFFSET = 'offset' | ||
|
|
||
| DEFAULT_NAME = 'Ruter' | ||
|
|
||
| TIME_BETWEEN_UPDATES = timedelta(seconds=10) | ||
|
ludeeus marked this conversation as resolved.
Outdated
|
||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_STOPID): cv.positive_int, | ||
| vol.Optional(CONF_DESTINATION): cv.string, | ||
| vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, | ||
| vol.Optional(CONF_OFFSET, default=1): cv.positive_int, | ||
| }) | ||
|
|
||
|
|
||
| async def async_setup_platform( | ||
| hass, config, async_add_entities, discovery_info=None): | ||
| """Create the sensor.""" | ||
| from pyruter.api import Departures | ||
|
|
||
| stopid = config[CONF_STOPID] | ||
| destination = config.get(CONF_DESTINATION) | ||
| name = config[CONF_NAME] | ||
| offset = config[CONF_OFFSET] | ||
|
|
||
| ruter = Departures(hass.loop, stopid, destination) | ||
| sensor = [TautulliSensor(ruter, name, offset)] | ||
|
ludeeus marked this conversation as resolved.
Outdated
|
||
| async_add_entities(sensor, True) | ||
|
|
||
|
|
||
| class TautulliSensor(Entity): | ||
|
ludeeus marked this conversation as resolved.
Outdated
|
||
| """Representation of a Sensor.""" | ||
|
ludeeus marked this conversation as resolved.
Outdated
|
||
|
|
||
| def __init__(self, ruter, name, offset): | ||
| """Initialize the sensor.""" | ||
| self.ruter = ruter | ||
| self._attributes = {} | ||
| self._name = name | ||
| self._offset = offset | ||
| self._line = None | ||
| self._destination = None | ||
| self._state = None | ||
|
|
||
| async def async_update(self): | ||
| """Get the latest data from the Ruter API.""" | ||
| await self.ruter.get_departures() | ||
| if self.ruter.departures is not None: | ||
|
ludeeus marked this conversation as resolved.
Outdated
|
||
| try: | ||
| data = self.ruter.departures[self._offset] | ||
| self._state = data['time'] | ||
| self._line = data['line'] | ||
| self._destination = data['destination'] | ||
| except (KeyError, IndexError) as error: | ||
| _LOGGER.error("Error getting data from Ruter, %s", error) | ||
|
ludeeus marked this conversation as resolved.
Outdated
|
||
|
|
||
| @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:bus' | ||
|
|
||
| @property | ||
| def device_state_attributes(self): | ||
|
ludeeus marked this conversation as resolved.
|
||
| """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.