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 OASA Telematics greek public transport sensor component #22196

Merged
merged 6 commits into from
Apr 6, 2019
Merged

Add OASA Telematics greek public transport sensor component #22196

merged 6 commits into from
Apr 6, 2019

Conversation

panosmz
Copy link
Contributor

@panosmz panosmz commented Mar 19, 2019

Description:

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

Example entry for configuration.yaml (if applicable):

sensor:
  - platform: oasa_telematics
    stop_id: '090006'
    route_id: 1965

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.

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.

@@ -0,0 +1 @@
"""The OASA Telematics component."""

Choose a reason for hiding this comment

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

blank line at end of file

@bkbilly
Copy link

bkbilly commented Mar 20, 2019

Hi!
If you want take a look of my implementation of this because it seems simpler using coordinates:
https://gist.github.com/bkbilly/ee250990228f1acb3673b1b1d06574bb

@panosmz
Copy link
Contributor Author

panosmz commented Mar 20, 2019

Hi @bkbilly, thanks that's interesting! However, I use a different approach. I find it simpler (and more useful) to get the upcoming arrivals for a given bus stop, mimicking the way the real bus stop displays work. Nonetheless we could definitely add the actual bus coordinates as a different mode.

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.

Please clarify what we mean with "due in" and "next arrival".

For more info on the API see:
https://oasa-telematics-api.readthedocs.io/en/latest/
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.oasa_telematics/
Copy link
Member

Choose a reason for hiding this comment

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

We don't include the url in the docstring anymore. Just keep the very first line.

REQUIREMENTS = ['oasatelematics==0.2']
_LOGGER = logging.getLogger(__name__)

ATTR_STOP_ID = 'Stop ID'
Copy link
Member

Choose a reason for hiding this comment

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

State attribute keys should be lowercase snakecase.

})


def setup_platform(hass, config, add_devices, discovery_info=None):
Copy link
Member

Choose a reason for hiding this comment

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

Please rename add_devices to add_entities.

self._times = self.data.info
self._name_data = self.data.name_data
try:
self._state = self._times[0][ATTR_DUE_IN]
Copy link
Member

Choose a reason for hiding this comment

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

Instead of making the state minutes until departure, make it an absolute utc timestamp and set device_class property to timestamp.

This will allow the user to select a relative time representation of the state in the frontend.

https://developers.home-assistant.io/docs/en/entity_sensor.html#available-device-classes

if self._times is not None:
next_arrival = None
if len(self._times) > 1:
next_arrival = ('{} min'.format(
Copy link
Member

Choose a reason for hiding this comment

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

What does next arrival mean? What is arriving where?

next_arrival = ('{} min'.format(
str(self._times[1][ATTR_DUE_IN])))
params.update({
ATTR_DUE_IN: str(self._times[0][ATTR_DUE_IN]),
Copy link
Member

@MartinHjelmare MartinHjelmare Apr 3, 2019

Choose a reason for hiding this comment

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

What does due in mean?

I'm thinking we're interested in next departure time. I don't understand what we mean with the other terms here, next arrival and due in.

Copy link
Member

Choose a reason for hiding this comment

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

Seems like we use "due in" for state already. We shouldn't duplicate state in state attributes.

@staticmethod
def empty_name_data():
"""Object returned when no stop/route name data are found."""
return [{ATTR_STOP_NAME: 'n/a',
Copy link
Member

Choose a reason for hiding this comment

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

Instead of setting 'n/a', don't include these attributes in the state attributes at all if not known. If state isn't known we should set state to None.

if route:
return route[0].get('route_departure_eng')
except TypeError:
_LOGGER.debug("Cannot get route name from OASA API")
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be a warning at least. How frequently could this occur?

@panosmz
Copy link
Contributor Author

panosmz commented Apr 4, 2019

@MartinHjelmare This sensor fetches the next bus arrival (in minutes) of a given bus line to a given bus stop.

The estimated arrival time is what we poll the api for, but I agree that next_departure should be added. If we are only interested in next_departure, that's a different implementation.

due_in is the estimated arrival time (in minutes) of the bus to the stop, and next_arrival is the second next estimated arrival time (of the next bus in line heading to the stop). I will rename these for clarification.

@MartinHjelmare
Copy link
Member

Isn't departure more interesting than arrival, if this will be used to know what bus to catch? I'm thinking we could replace arrival with departure. But I'll leave this up to you. Thanks for clarifying the context.

@property
def unit_of_measurement(self):
"""Return the unit this state is expressed in."""
return 'ISO8601'
Copy link
Member

@MartinHjelmare MartinHjelmare Apr 5, 2019

Choose a reason for hiding this comment

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

The timestamp sensor device class doesn't use unit of measurement.


for result in results:
arrival_min = int(result.get('btime2'))
if arrival_min is not None:
Copy link
Member

@MartinHjelmare MartinHjelmare Apr 5, 2019

Choose a reason for hiding this comment

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

arrival_min can't be None at this point. There would have been an error at the line above where we copy to int in that case.

Move the check up before copying to int.

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.

Nice!

@MartinHjelmare
Copy link
Member

Can be merged when the merge conflict is solved and the build passes.

@panosmz
Copy link
Contributor Author

panosmz commented Apr 6, 2019

Thanks for the feedback @MartinHjelmare

@MartinHjelmare MartinHjelmare merged commit 6351c5c into home-assistant:dev Apr 6, 2019
@ghost ghost removed the in progress label Apr 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants