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 RMV public transport sensor #15814

Merged
merged 27 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c3f61e0
Add new public transport sensor for RMV (Rhein-Main area).
cgtobi Aug 2, 2018
b9b59aa
Add required module.
cgtobi Aug 2, 2018
41c34ea
Fix naming problem.
cgtobi Aug 2, 2018
f9e88b6
Add unit test.
cgtobi Aug 3, 2018
221167e
Update dependency version to 0.0.5.
cgtobi Aug 3, 2018
504049b
Add new requirements.
cgtobi Aug 3, 2018
db76dd0
Fix variable name.
cgtobi Aug 3, 2018
2d9c122
Fix issues pointed out in review.
cgtobi Aug 5, 2018
485ae89
Remove unnecessary code.
cgtobi Aug 5, 2018
6fcc48d
Fix linter error.
cgtobi Aug 5, 2018
741a711
Fix config value validation.
cgtobi Aug 6, 2018
1403293
Replace minutes as state by departure timestamp. (see ##14983)
cgtobi Aug 6, 2018
f67f22e
More work on the timestamp. (see ##14983)
cgtobi Aug 7, 2018
74117c3
Revert timestamp work until #14983 gets merged.
cgtobi Aug 7, 2018
2accb78
Simplify product validation.
cgtobi Aug 7, 2018
661bad1
Remove redundant code.
cgtobi Aug 7, 2018
26f1227
Address code change requests.
cgtobi Aug 9, 2018
b714364
Address more code change requests.
cgtobi Aug 9, 2018
8be8b07
Address even more code change requests.
cgtobi Aug 9, 2018
d21cec3
Simplify destination check.
cgtobi Aug 9, 2018
4341b8a
Fix linter problem.
cgtobi Aug 9, 2018
72c3b7e
Bump dependency version to 0.0.7.
cgtobi Aug 9, 2018
9f29d30
Name variable more explicit.
cgtobi Aug 10, 2018
844c439
Only query once a minute.
cgtobi Aug 10, 2018
cacfd46
Update test case.
cgtobi Aug 10, 2018
a9949a4
Fix config validation.
cgtobi Aug 10, 2018
1642b47
Remove unneeded import.
cgtobi Aug 10, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions homeassistant/components/sensor/rmvtransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
'IC': 'mdi:train',
'ICE': 'mdi:train',
'SEV': 'mdi:checkbox-blank-circle-outline',
'-': 'mdi:clock'
None: 'mdi:clock'
}
ATTRIBUTION = "Data provided by opendata.rmv.de"

Expand All @@ -58,7 +58,7 @@
vol.Optional(CONF_DIRECTIONS, default=['']): cv.ensure_list_csv,
vol.Optional(CONF_LINES, default=['']): cv.ensure_list_csv,
vol.Optional(CONF_PRODUCTS, default=DEFAULT_PRODUCT):
Copy link
Contributor

Choose a reason for hiding this comment

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

Better validate each product user configured is in the default product list

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I fixed that. Thanks for the hint.

cv.ensure_list_csv,
vol.All(cv.ensure_list, [vol.In(DEFAULT_PRODUCT)]),
vol.Optional(CONF_TIMEOFFSET, default=0): cv.positive_int,
vol.Optional(CONF_MAXJOURNEYS, default=5): cv.positive_int,
Copy link
Member

Choose a reason for hiding this comment

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

What do we call this in other sensor commuting platforms?

Copy link
Contributor Author

@cgtobi cgtobi Aug 9, 2018

Choose a reason for hiding this comment

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

products/CONF_PRODUCTS: MVG calls it products, which is the original name of the argument the actual HABAS backend. It is the only I found which has a similar setting.
UK transport calls it mode, which switches between bus or train.

max/CONF_MAXJOURNEYS: MVG calls it number, but I found that a bit unspecific. It is the only I found which has a similar setting.

timeoffset/CONF_TIMEOFFSET: MVG calls it timeoffset as well, Public Transit (GTFS) calls it offset and Västtrafik Public Transport calls it delay. A bit of a mix.

Copy link
Member

Choose a reason for hiding this comment

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

Ok. max isn't so specific either I think. But I don't have a better suggestion at the moment.

It would be good to come up with a descriptive name and then have all platforms use the same term. But it can wait to a different PR.

But please separate words by underscore in variable and constant names and strings. So change to CONF_TIME_OFFSET = 'time_offset' and CONF_MAX_JOURNEYS.

vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}]
Expand Down Expand Up @@ -93,7 +93,7 @@ def __init__(self, station, destinations, directions,
self.data = RMVDepartureData(station, destinations, directions,
lines, products, timeoffset, maxjourneys)
self._state = STATE_UNKNOWN
Copy link
Member

Choose a reason for hiding this comment

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

Init state as None and let that represent unknown state.

self._icon = ICONS['-']
self._icon = ICONS[None]

@property
def name(self):
Expand All @@ -103,9 +103,7 @@ def name(self):
@property
def available(self):
"""Return True if entity is available."""
if self._state is STATE_UNKNOWN:
return False
return True
return self._state != STATE_UNKNOWN
Copy link
Member

Choose a reason for hiding this comment

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

Don't use STATE_UNKNOWN, use None.

return self._state is not None

Is there no use to differ between unknown state and unavailable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll check that.


@property
def state(self):
Expand All @@ -119,10 +117,10 @@ def state_attributes(self):
try:
result = {
'next_departures': [val for val in self.data.departures[1:]],
'direction': self.data.departures[0].get('direction', '-'),
'line': self.data.departures[0].get('number', '-'),
'minutes': self.data.departures[0].get('minutes', '-'),
'product': self.data.departures[0].get('product', '-'),
'direction': self.data.departures[0].get('direction', None),
Copy link
Contributor

Choose a reason for hiding this comment

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

None is the default value for get method, no need to specific set here.

'line': self.data.departures[0].get('number', None),
'minutes': self.data.departures[0].get('minutes', None),
'product': self.data.departures[0].get('product', None),
}
except IndexError:
pass
Copy link
Member

Choose a reason for hiding this comment

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

We could return empty dict here instead of defining it at the top.

Expand All @@ -142,16 +140,16 @@ def update(self):
"""Get the latest data and update the state."""
self.data.update()
if not self.data.departures:
self._state = '-'
self._icon = ICONS['-']
self._state = None
self._icon = ICONS[None]
return
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

change to guard clause

if not self.data.departures:
  # something
  return

#something else

Copy link
Contributor

Choose a reason for hiding this comment

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

Since you have return in the if part, else became unnecessary

if self._name is DEFAULT_NAME:
if self._name == DEFAULT_NAME:
self._name = self.data.station
else:
self._name = self._name
self._station = self.data.station
self._state = self.data.departures[0].get('minutes', '-')
self._icon = ICONS[self.data.departures[0].get('product', '-')]
self._state = self.data.departures[0].get('minutes', None)
self._icon = ICONS[self.data.departures[0].get('product', None)]
return


class RMVDepartureData:
Expand Down Expand Up @@ -182,7 +180,7 @@ def update(self):
self.departures = {}
Copy link
Member

Choose a reason for hiding this comment

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

We set this as a list in init. We probably shouldn't change the type.

Copy link
Contributor Author

@cgtobi cgtobi Aug 9, 2018

Choose a reason for hiding this comment

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

Well spotted. Not sure when that happened. ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I rather initialise as a dict.

Copy link
Member

Choose a reason for hiding this comment

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

But we use self.departures as a list and it's updated to be a list during update.

_LOGGER.warning("Returned data not understood")
return
self.station = _data.get('station', '-')
self.station = _data.get('station', None)
Copy link
Member

Choose a reason for hiding this comment

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

None is the default value returned when the key is missing in the dict when using dict.get.

_deps = []
for journey in _data['journeys']:
# find the first departure meeting the criteria
Expand Down
4 changes: 2 additions & 2 deletions script/gen_requirements_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
'pymonoprice',
'pynx584',
'pyqwikswitch',
'PyRMVtransport',
'python-forecastio',
'python-nest',
'pytradfri\[async\]',
Expand All @@ -98,8 +99,7 @@
'yahoo-finance',
'pythonwhois',
'wakeonlan',
'vultr',
'PyRMVtransport'
'vultr'
)

IGNORE_PACKAGES = (
Expand Down