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

Platform for Munich public transport departure times #6704

Merged
merged 6 commits into from
Mar 26, 2017
Merged

Platform for Munich public transport departure times #6704

merged 6 commits into from
Mar 26, 2017

Conversation

DavidMStraub
Copy link
Contributor

@DavidMStraub DavidMStraub commented Mar 19, 2017

I'm not sure if this is too local to become an official component but for me it's very useful so I thought I'd share it anyway.

Description:

The MVG live service (http://www.mvg-live.de) provides real-time departure information for the Munich public transort system (bus, tram, subway, suburban rail). This platform provides a sensor for the time to the next departure (optionally the next departure after an offset, e.g. the time it takes you to walk to the bus stop) at a stop or station. The destination and line number are given in the attributes. Optionally, specific lines or destinations can be selected.

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#2311

Example entry for configuration.yaml (if applicable):

sensor:
  platform: mvglive
  station: Marienplatz

Checklist:

  • Documentation added/updated in home-assistant.github.io
  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

@mention-bot
Copy link

@DavidMStraub, thanks for your PR! By analyzing the history of the files in this pull request, we identified @balloob, @fabaff and @rmkraus to be potential reviewers.

@balloob
Copy link
Member

balloob commented Mar 19, 2017

Nothing is too local 👍

import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)
# A typo in the file name of the PyPI version prevents installation from PyPI
Copy link
Member

@balloob balloob Mar 19, 2017

Choose a reason for hiding this comment

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

Make sure to open an issue for this on their repo so we can migrate to use the PyPi version in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, just replaced it with the PyPI version

Copy link
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

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

Few changes, code looks good 👍

self._line = line
self.data = MVGLiveData(station, destination, line,
offset, ubahn, tram, bus, sbahn)
self.update()
Copy link
Member

Choose a reason for hiding this comment

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

Please don't call update here but instead pass True as a second parameter to add_devices

# Details info is not useful.
# Having a more consistent interface simplifies
# usage of Template sensors later on
con.pop('productsymbol')
Copy link
Member

Choose a reason for hiding this comment

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

It's usually not a good idea to mutate data that you fetch from a library. They might store a reference in a cache that is now no longer what they expect it to be. Create new dictionaries instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the hints! I think I fixed everything.

_departures = self.mvg.getlivedata(station=self._station,
ubahn=self._ubahn,
tram=self._tram,
bus=self._bus,

Choose a reason for hiding this comment

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

continuation line over-indented for visual indent

try:
_departures = self.mvg.getlivedata(station=self._station,
ubahn=self._ubahn,
tram=self._tram,

Choose a reason for hiding this comment

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

continuation line over-indented for visual indent

"""Update the connection data."""
try:
_departures = self.mvg.getlivedata(station=self._station,
ubahn=self._ubahn,

Choose a reason for hiding this comment

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

continuation line over-indented for visual indent

_LOGGER.warning("Returned data not understood.")
return
_keys = ['destination', 'linename', 'time', 'direction', 'product']
self.departures = [{k: int(v) if k == 'time' and v.is_integer() else v
Copy link
Member

Choose a reason for hiding this comment

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

This code is unmaintainable. Please make it a for loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. Refactored and simplified.

def update(self):
"""Get the latest data and update the state."""
self.data.update()
if not self.data.departures:
Copy link
Member

Choose a reason for hiding this comment

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

This case can never happen because you always have an empty dictionary in your list, meaning it will always test truthy. (only an empty list is False)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. Refactored the code now - in fact, no need for the list as we're only interested in the 1st item.

continue
# now select the relevant data
_nextdep = {}
for k in ['destination', 'linename', 'time', 'direction', 'product']:

Choose a reason for hiding this comment

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

line too long (81 > 79 characters)

@fabaff fabaff dismissed balloob’s stale review March 26, 2017 17:06

All comments are addressed.

Copy link
Member

@fabaff fabaff left a comment

Choose a reason for hiding this comment

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

🐦

@fabaff fabaff merged commit 78b5eb7 into home-assistant:dev Mar 26, 2017
@fabaff fabaff mentioned this pull request Apr 6, 2017
@home-assistant home-assistant locked and limited conversation to collaborators Jul 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants