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 travel_time integration #28716

Closed
wants to merge 14 commits into from

Conversation

eifinger
Copy link
Contributor

@eifinger eifinger commented Nov 12, 2019

Breaking Change:

Description:

As discussed in #27237 and home-assistant/architecture#166 this PR adds a base travel_time integration to use for waze_travel_time, google_travel_time and here_travel_time.

This allows for code deduplication and standards for attributes etc.

Related issue (if applicable): fixes N/A

Pull request with documentation for home-assistant.io (if applicable): TBD

Example entry for configuration.yaml (if applicable):

TBD

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.
  • I have followed the development checklist

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

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

  • The manifest file has all fields filled out correctly. Update and include derived files by running python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

If the code does not interact with devices:

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

@eifinger
Copy link
Contributor Author

I would like to put https://github.com/home-assistant/home-assistant/pull/28716/files#diff-2bf8b028fb7894b26af233d297725765L96-L114 into travel_time itself but then the PLATFORM_SCHEMA cant be extended any more because its not a dict.

@eifinger
Copy link
Contributor Author

@robbiet480 you are codeowner for google_travel_time. Would you mind to get into the discussion?
@Petro31 @ljmerza you discussed in the architecture issue about this.

@eifinger eifinger force-pushed the travel_time_integration branch from f4defe0 to 05b0f40 Compare November 12, 2019 14:59
@eifinger
Copy link
Contributor Author

Working on the integration for here_weather (PR) I realized that I would like to use the get_location_from_entity() function as well.

The usecase would be to give me the weather forecast for the current location of a device_tracker each morning. Or combine it with my calendar and send me the forecast for the location I have to travel to for work tomorrow.

Therefore I would rather rename the function to get_coordinates_from_entity() because thats exactly what it does and move it to homeassistant.util.location

@eifinger
Copy link
Contributor Author

I did have the idea to remove the magic of getting the coordinates from a different entity and provide an additonal option origin_template. This would mean we could rely on the existing functionality of the template engine.

But then again the user would have to know the different ways to extract coordinates from a sensor, device_tracker, zone and so on.

Just now writing this I am thinking of a combined version:

Introducing a new jinja2 function get_coordinates_from_entity which can be used in origin_template. A full config would then look like this:

input_select:
  here_destination_preset:
    options:
      - zone.home
      - zone.office
      - zone.somewheredefault

travel_time:
  - platform: here_travel_time
    app_id: my_app_id
    app_code: my_app_code
    origin_template: {{ get_coordinates_from_entity(device_tracker.myphone) }}
    destination_template: {{ get_coordinates_from_entity(input_select.here_destination_preset) }}

@Mariusthvdb
Copy link
Contributor

Mariusthvdb commented Dec 12, 2019

very nice indeed, and just as I was building myself in templates see:

https://community.home-assistant.io/t/waze-travel-time-update/50955/286?u=mariusthvdb
thanks to you and @Petro31

I've made the input_select pick either a zone or a device_tracker, and have the origin and destination sensor sortout the lat/long

please continue, this would be very welcome indeed.
If you could do without the _template for origin and destination, that would be even better, let the user be agnostic to using a template or not.

also, shouldn't this be with quoted entities:

origin_template: {{ get_coordinates_from_entity('device_tracker.myphone') }}
destination_template: {{ get_coordinates_from_entity('input_select.here_destination_preset') }}

to be compliant with regular Jinja templating?

@eifinger eifinger force-pushed the travel_time_integration branch from 05b0f40 to 7ae5373 Compare December 17, 2019 08:33
@property
def state(self) -> Optional[str]:
"""Return the state of the travel_time entity."""
return None
Copy link
Member

Choose a reason for hiding this comment

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

We should always have the base class define the state property. It should return one of the other properties.


SCAN_INTERVAL = timedelta(minutes=5)

PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA)
Copy link
Member

Choose a reason for hiding this comment

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

We should specify a standardized schema that platforms have to follow to specify source and target location?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. For that I would like to have Add template extension coordinates #30019 merged first.

  2. Moreover the current travel_time integrations provide support for at least coordinates. Waze and Google allow names and Google allows for Google Place Ids as well.
    All integrations allow to supply entity_ids like device_trackers etc.

    Google and Waze both only have destination and origin which can hold everything.

    For HERE we introduced separate origin_latitude, origin_entity_id, and so on. The
    discussion can be found here: https://github.com/home-assistant/home-
    assistant/pull/24603#discussion_r311280860

    So I am not sure what should be standard given all these options.

  3. Given the above I implemented the check that exactly one method is used for here_travel_time in the following way: https://github.com/home-assistant/home-assistant/blob/7ae537304e8f8bc548c1f67b997b06fe07bb6da8/homeassistant/components/here_travel_time/travel_time.py#L83-L90 but do not know how to put that in a base schema. All the ways I tried lead to "schema cannot be extended because it is not 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.

#30019 can be added later.

It's up to the travel time integration to provide a way to specify the origin and destination. Platforms just need to provide travel time.

The standard should be to accept one of those options. Like you did with here_travel_time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So this means no standard platform schema in travel_time. Did I understand you correct?
I am not sure whether your initial comment on this was a question whether we should do so or your opinion that we should.

Copy link
Member

Choose a reason for hiding this comment

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

No I think we should have a standard platform schema and standardize how people define origin and destination. If we don't do that, then why standardize anything at all ?

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 implemented a standard TRAVEL_TIME_SCHEMA.
It allows origin and destination to either be defined by a name or a combination of latitude and longitude.
Moreover each of them can be a template:

origin_latitude: 0.0
origin_longitude: {{ state_attr('device_tracker.paulus', 'longitude') }}
destination_name: Berlin

return None

@property
def duration_in_traffic(self) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

Is this something that is always available ? Should we start with not having this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Distance and destination_name are used by the travel_time card by @ljmerza.
Having both duration and duration_in_traffic enables certain usecases without having to create two sensors. E.g. alert me when the current duration_in_traffic is more than 30% of the normal duration.
waze_travel_time is the only integration to currently not support duration_in_traffic.

Copy link
Member

Choose a reason for hiding this comment

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

ok let's keep it.

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.

Please remove the migration from here_travel_time to a platform for travel_time from this PR. Instead, add a demo platform. Also make sure to include tests.

@eifinger eifinger force-pushed the travel_time_integration branch from ae5ac53 to d7b24fe Compare February 16, 2020 19:49
@eifinger eifinger requested a review from a team as a code owner February 16, 2020 19:49
@codecov
Copy link

codecov bot commented Feb 16, 2020

Codecov Report

❗ No coverage uploaded for pull request base (dev@8423d18). Click here to learn what that means.
The diff coverage is 90.62%.

Impacted file tree graph

@@          Coverage Diff           @@
##             dev   #28716   +/-   ##
======================================
  Coverage       ?   94.67%           
======================================
  Files          ?      765           
  Lines          ?    55352           
  Branches       ?        0           
======================================
  Hits           ?    52407           
  Misses         ?     2945           
  Partials       ?        0
Impacted Files Coverage Δ
homeassistant/components/travel_time/const.py 100% <100%> (ø)
homeassistant/components/demo/travel_time.py 89.05% <89.05%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8423d18...09d651e. Read the comment docs.

@stale
Copy link

stale bot commented Mar 21, 2020

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
Thank you for your contributions.

@stale stale bot added the stale label Mar 21, 2020
@eifinger
Copy link
Contributor Author

Still open

@stale stale bot removed the stale label Mar 21, 2020
@eifinger eifinger force-pushed the travel_time_integration branch from 3407406 to 5dce233 Compare March 22, 2020 13:50
@eifinger eifinger changed the title WIP Add travel_time integration Add travel_time integration Mar 22, 2020
return await hass.data[DOMAIN].async_unload_entry(entry)


class TravelTimeEntity(Entity):
Copy link
Member

Choose a reason for hiding this comment

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

I've been thinking about this more and I think that we should have platforms implement entities. Shouldn't it just be services that take in a start, end and options and return a measurement which then is represented by an entity.

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 idea behind this was the other way round. Define a clear set of properties a TravelTime Entity should have so they can be used in Lovelace cards, automations, appdaemon etc.
Currently all of the above look different for Google, Waze, Here....
For example:

Google has an attribute destination_addresses which is a list.
Waze has destination_address HERE has destination_name.

Additional thought was that the configuration options should be named the same.

Copy link
Member

Choose a reason for hiding this comment

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

That would still work ? The travel_time integration defines thte entity and the exposed data, so it's the same. It just sources that data from a service object provided by integrations instead of having integrations provide the entity objects directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there an existing platform/integration which does that already so I could have a look?

Copy link
Member

Choose a reason for hiding this comment

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

Notification, but I don't like that implementation :(

I guess we can still use entities but just have each entity implement an abstract method that returns all info at once ?

I don't know. Maybe the existing approach is ok. 🤷‍♂ It just feels a little weird.

@stale
Copy link

stale bot commented Apr 25, 2020

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
Thank you for your contributions.

@stale stale bot added the stale label Apr 25, 2020
@stale stale bot closed this May 2, 2020
@lock lock bot locked and limited conversation to collaborators May 6, 2020
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.

4 participants