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

Use feed name as entity name in GeoJSON #108753

Merged
merged 2 commits into from Jan 25, 2024

Conversation

codyc1515
Copy link
Contributor

@codyc1515 codyc1515 commented Jan 24, 2024

Breaking change

Previously GeoJSON names were just the config entry ID. This was not very user friendly. Particularly so when there are many config entries and many, many entities from those same many config entries. This is how they look today.

image

Proposed change

The proposal is to add support for entity name in GeoJSON. If unavailable in the data source, falls back to the config entry (same as today). This is how they look after.

image

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the [development checklist][dev-checklist]
  • I have followed the [perfect PR recommendations][perfect-pr]
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

@home-assistant
Copy link

Hey there @exxamalte, mind taking a look at this pull request as it has been labeled with an integration (geo_json_events) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of geo_json_events can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign geo_json_events Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@codyc1515
Copy link
Contributor Author

Looks like the tests need updating to support this. I have no idea how to do that, sorry.

@exxamalte
Copy link
Contributor

Hm, I'm not sure if we're heading into the right direction here. Picking a random property and making it into the entity's name might look like a good idea, but there could be hundreds of feeds out there that don't define that particular property. The GeoJSON specification provides absolutely no schema for the properties.
Do you use many GeoJSON feeds that do have a "name" property?

@codyc1515
Copy link
Contributor Author

codyc1515 commented Jan 24, 2024

We aren't picking a random property - we're picking one specifically, called name.

The official GeoJSON website uses it in their minimalist example. LeafletJS, which HA uses for Maps, also endorses this usage. This is also how ArcGIS uses the properties. In fact, I've only ever seen GeoJSON used in the way I've described.

If we didn't find a name, we fallback to the previous case (the config_entry id). So, the change should not "break" for most people.

@exxamalte
Copy link
Contributor

Looks like the tests need updating to support this. I have no idea how to do that, sorry.

The tests appear to fail because the geo_location entities couldn't be added because there is something wrong with their names (or at least how the test is set up to generate names). The root cause appears to be:

ERROR:homeassistant.components.geo_location:Error adding entities for domain geo_location with platform geo_json_events
Traceback (most recent call last):
  File "/home/runner/work/core/core/homeassistant/helpers/entity_platform.py", line 515, in async_add_entities
    await asyncio.gather(*tasks)
  File "/home/runner/work/core/core/homeassistant/helpers/entity_platform.py", line 666, in _async_add_entity
    entry = entity_registry.async_get_or_create(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/core/core/homeassistant/helpers/entity_registry.py", line 695, in async_get_or_create
    entity_id = self.async_generate_entity_id(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/core/core/homeassistant/helpers/entity_registry.py", line 615, in async_generate_entity_id
    preferred_string = f"{domain}.{slugify(suggested_object_id)}"
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/core/core/homeassistant/util/__init__.py", line 47, in slugify
    slug = unicode_slug.slugify(text, separator=separator)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/core/core/venv/lib/python3.11/site-packages/slugify/slugify.py", line 95, in slugify
    text = str(text, 'utf-8', 'ignore')
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: decoding to str: need a bytes-like object, MagicMock found

I think if you fix the linter error, this might just go away.

Nevertheless it would be nice to add a test case to see if fetching the "name" property actually works.

@codyc1515
Copy link
Contributor Author

codyc1515 commented Jan 24, 2024

The linter warning is saying that the code looks pointless where we check if we can use the entity name or need to fall back to the config entry id or not.

@codyc1515 codyc1515 force-pushed the geojson_name branch 4 times, most recently from 90951a4 to 92bcf27 Compare January 24, 2024 08:46
@codyc1515 codyc1515 marked this pull request as ready for review January 24, 2024 08:57
@codyc1515
Copy link
Contributor Author

Looks like I broke the CI - it's just sitting on Waiting to run this check. Appreciate any assistance with resolving the test. It looks like the test is failing on the slugify which is due to something to do with the entity name not populating (for the purposes of the test only, to be clear). The integration itself is actually working fine with this change.

@codyc1515
Copy link
Contributor Author

Hm, I'm not sure if we're heading into the right direction here. Picking a random property and making it into the entity's name might look like a good idea, but there could be hundreds of feeds out there that don't define that particular property. The GeoJSON specification provides absolutely no schema for the properties. Do you use many GeoJSON feeds that do have a "name" property?

I'm wondering now if we should actually make this a config parameter somewhere. Still default it to name or fallback to the config_entry id but also give the option of a textbox through the config flow to choose another different key to use as the title, such as Address (as in street address). This could be configured to maybe only be an Advanced Users parameter.

Copy link
Member

@joostlek joostlek left a comment

Choose a reason for hiding this comment

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

Tests are failing, can you take a look

@home-assistant home-assistant bot marked this pull request as draft January 24, 2024 09:14
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@codyc1515
Copy link
Contributor Author

Tests are failing, can you take a look

Sorry, as above, I don't know how to fix that. The changes themselves work, simply the tests need to be updated.

@codyc1515
Copy link
Contributor Author

When I replace your change with the second code snippet I recommended to try, the unit tests pass on my machine without any modifications.

Sorry, I did try that and thought that it was failing. I must have tested it with a wrong feed (one that doesn't have a name propety). Can confirm it is working and the tests passed now :) Thank you very much for your help.

@codyc1515 codyc1515 marked this pull request as ready for review January 24, 2024 19:43
@codyc1515
Copy link
Contributor Author

I will update the documentation before we push this.

@home-assistant home-assistant bot marked this pull request as draft January 24, 2024 22:16
Previously GeoJSON names were just the config entry ID. This is not very user friendly. Particularly so when there are many config entries and many, many entities from those same many config entries.
@codyc1515 codyc1515 marked this pull request as ready for review January 25, 2024 00:13
@codyc1515 codyc1515 marked this pull request as draft January 25, 2024 00:16
@codyc1515 codyc1515 force-pushed the geojson_name branch 2 times, most recently from be4881d to cb8afbb Compare January 25, 2024 01:09
@codyc1515 codyc1515 marked this pull request as ready for review January 25, 2024 01:19
@codyc1515
Copy link
Contributor Author

codyc1515 commented Jan 25, 2024

I have added added tests to cover the new scenarios too. All tests passed.

Copy link
Contributor

@exxamalte exxamalte left a comment

Choose a reason for hiding this comment

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

This looks good to me. Thanks for adding the test case.

homeassistant/components/geo_json_events/geo_location.py Outdated Show resolved Hide resolved
@joostlek
Copy link
Member

Oh btw, a name change isn't a breaking change

@codyc1515
Copy link
Contributor Author

Thanks. I wasn't sure, so went conservative.

@joostlek joostlek changed the title Add support for entity name in GeoJSON Use feed name as entity name in GeoJSON Jan 25, 2024
@joostlek joostlek merged commit 6e59568 into home-assistant:dev Jan 25, 2024
23 checks passed
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 address the comment in a new PR, if needed. Thanks

@github-actions github-actions bot locked and limited conversation to collaborators Jan 26, 2024
@codyc1515 codyc1515 deleted the geojson_name branch January 27, 2024 20:53
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.

None yet

4 participants