Skip to content
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

Add Norwegian Public Transportation sensor (Ruter). #18237

Merged
merged 6 commits into from Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Expand Up @@ -767,6 +767,7 @@ omit =
homeassistant/components/sensor/rainbird.py
homeassistant/components/sensor/ripple.py
homeassistant/components/sensor/rtorrent.py
homeassistant/components/sensor/ruter.py
homeassistant/components/sensor/scrape.py
homeassistant/components/sensor/sensehat.py
homeassistant/components/sensor/serial_pm.py
Expand Down
92 changes: 92 additions & 0 deletions homeassistant/components/sensor/ruter.py
@@ -0,0 +1,92 @@
"""
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.ruter/
"""
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 = ['pyruter==1.0.2']

_LOGGER = logging.getLogger(__name__)

CONF_STOP_ID = 'stop_id'
CONF_DESTINATION = 'destination'
CONF_OFFSET = 'offset'

DEFAULT_NAME = 'Ruter'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_STOP_ID): 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

stop_id = config[CONF_STOP_ID]
destination = config.get(CONF_DESTINATION)
name = config[CONF_NAME]
offset = config[CONF_OFFSET]

ruter = Departures(hass.loop, stop_id, destination)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want all libraries that use aiohttp to take a session argument so that we can reuse the home assistant session throughout the app.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not aware of that, will change it that and create a new PR later today :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it more like "we would like"? I believe that part of the deal with using third party libraries is that we don't get to dictate their interface.

Still, @ludeeus, please do continue with implementing that :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amelchio we don't have to dictate their interface, but very much like if they do take in an optional aiohttp session instead of creating their own if they use aiohttp.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and for a library created for Home Assistant that is no biggie. I was more concerned about rejecting a new platform because an existing library does not support passing in the session.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah no, we wouldn't do that.

sensor = [TautulliSensor(ruter, name, offset)]
ludeeus marked this conversation as resolved.
Show resolved Hide resolved
async_add_entities(sensor, True)


class TautulliSensor(Entity):
ludeeus marked this conversation as resolved.
Show resolved Hide resolved
"""Representation of a Sensor."""
ludeeus marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, ruter, name, offset):
"""Initialize the sensor."""
self.ruter = ruter
self._attributes = {}
self._name = name
self._offset = offset
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.
Show resolved Hide resolved
try:
data = self.ruter.departures[self._offset]
self._state = data['time']
self._attributes['line'] = data['line']
self._attributes['destination'] = data['destination']
except (KeyError, IndexError) as error:
_LOGGER.debug("Error getting data from Ruter, %s", error)
else:
_LOGGER.error("No data recieved from Ruter.")

@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.
Show resolved Hide resolved
"""Return attributes for the sensor."""
return self._attributes
3 changes: 3 additions & 0 deletions requirements_all.txt
Expand Up @@ -1077,6 +1077,9 @@ pyrainbird==0.1.6
# homeassistant.components.switch.recswitch
pyrecswitch==1.0.2

# homeassistant.components.sensor.ruter
pyruter==1.0.2

# homeassistant.components.sabnzbd
pysabnzbd==1.1.0

Expand Down