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 Entur departure information sensor #17286

Merged
merged 16 commits into from
Nov 30, 2018
Merged

Add Entur departure information sensor #17286

merged 16 commits into from
Nov 30, 2018

Conversation

hfurubotten
Copy link
Contributor

@hfurubotten hfurubotten commented Oct 9, 2018

Description:

Adding support for realtime departure information about public transport in Norway. Fetches timing information from the open api of entur once a minute to show how long before next bus, ferry, train or similar is leaving the station.

Long time user of home assistant here, and in the spirit of hacktoberfest i wanted to give a bit back to this awesome project! So first time contributing here and first time writing something proper in python, so be gentle with me. If something is not 100% correctly done or similar, its probably because I don't know better, and any tips are welcome!

Pull request in home-assistant.io with documentation: home-assistant/home-assistant.io#6643

Example entry for configuration.yaml:

# Example configuration.yaml entry
sensor:
  - platform: entur_public_transport
    stop_ids:
      - 'NSR:StopPlace:548'   # Bergen train station
      - 'NSR:StopPlace:737'   # Trondheim airport
      - 'NSR:StopPlace:5850'  # Grorud T bus stop
      - 'NSR:StopPlace:58652' # Mortavika ferry 
      - 'NSR:StopPlace:27639' # Sør-Hidle quay 
      - 'NSR:Quay:48550'      # Fiskepiren bus stop platform 1

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New or updated dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

@homeassistant

This comment has been minimized.

@homeassistant

This comment has been minimized.

@hfurubotten hfurubotten changed the title Added Entur departure information sensor. Add Entur departure information sensor. Oct 10, 2018
homeassistant/components/sensor/entur_public_transport.py Outdated Show resolved Hide resolved
ATTR_EXPECTED_IN = 'due_in'
ATTR_NEXT_UP_IN = 'next_due_in'

API_CLIENT_ID = 'home-assistant'
Copy link
Member

Choose a reason for hiding this comment

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

So, all instances are going to use the same ID?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The purpose of the client-name is for tracking of which applications are using the API, and are required for each request to the API to not get blocked from contacting the API. In other words it should be unique for the application and not the instance.

Translated from the norwegian only documentation:

ET-Client-Name
Description of the client that makes the request. Should be a value that describes the application that performs the API call. The value should include company / organization followed by dash and application name - "company-applicationName".

Source: https://www.entur.org/dev/api/header/

Changed the name of the const to reflect that its a name instead of id. Chose the name homeassistant-homeassistant as "organization" and application name is the same for HA.

Does it sound reasonable?
Thanks.

homeassistant/components/sensor/entur_public_transport.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/entur_public_transport.py Outdated Show resolved Hide resolved
if self._times[CONF_TRANSPORT_MODE] == 'air':
return 'mdi:airplane'

return 'mdi:bus'
Copy link
Member

Choose a reason for hiding this comment

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

How about a dict lookup? Perhaps something like this:

ICONS = {'bus': 'mdi:bus', 'air': 'mdi:airplane', 'water': 'mdi:ferry'}
return [v for k, v in ICONS.items() if self._times[CONF_TRANSPORT_MODE] in k][0]

Copy link
Contributor Author

@hfurubotten hfurubotten Nov 13, 2018

Choose a reason for hiding this comment

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

I implemented the dict, but implemented the lookup through the dict.get method with a default. I think this is easier to read and immediately understand the line of code. Hope that's OK.
Thanks.

homeassistant/components/sensor/entur_public_transport.py Outdated Show resolved Hide resolved
@MartinHjelmare MartinHjelmare changed the title Add Entur departure information sensor. Add Entur departure information sensor Nov 13, 2018
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
self.config = VALID_CONFIG
Copy link
Member

Choose a reason for hiding this comment

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

Just use the constant directly. No need for the instance attribute.

Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

Looks good! @fabaff?

@ghost ghost assigned fabaff Nov 29, 2018
@fabaff fabaff merged commit 4bee3f7 into home-assistant:dev Nov 30, 2018
@ghost ghost removed the in progress label Nov 30, 2018
@balloob balloob mentioned this pull request Dec 12, 2018
@dico
Copy link

dico commented Dec 21, 2018

Great job!
Is it or should it be possible to filter route/transport-type?

I would like to display the ferry timetables for NSR:StopPlace:58664. The problem is that this is also a stopplace for busses going in all directions.

Not sure if this can be filtered by the Entur's GraphQL? If not, maybe add a filter to for-loop before adding the entities?

The attached picture show that the route changes from "16-713 Flakk ferjekai" to "350 Rissa/Osen" on the next. I would like this sensor to only show the 16-713 route for current and next.

For the "sensor.transport_rrvik_ferjekai_platform_71720". i'm not really sure whats going on, as the next ferry departure should be 09:30. The "next_due_at" time (09:25) is for the "350 Rissa/Osen" and not the "16-713 Rørvik ferjekai" as the sensor states.

ha_sensors_02

@MartinHjelmare
Copy link
Member

Please open an issue if you suspect a bug.

If you want to suggest an enhancement please open a feature request in the Feature Requests section of our community forum.

Merged PRs should not be used for enhancement discussion or bug reports.

Thanks!

@home-assistant home-assistant locked as resolved and limited conversation to collaborators Dec 21, 2018
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.

8 participants