Skip to content

Fix WUnderground duplicate entity ids#13285

Merged
OttoWinter merged 2 commits into
home-assistant:devfrom
OttoWinter:wunderground-duplicate-entity-id
Mar 17, 2018
Merged

Fix WUnderground duplicate entity ids#13285
OttoWinter merged 2 commits into
home-assistant:devfrom
OttoWinter:wunderground-duplicate-entity-id

Conversation

@OttoWinter
Copy link
Copy Markdown
Member

@OttoWinter OttoWinter commented Mar 17, 2018

Description:

With #12346, when using multiple wunderground stations duplicate entity ids would manually be generated, thus breaking the platform. This PR fixes that by manually keeping track of our own generated entity ids.

Also changed some type hints to allow passing in set()s to the entity_id methods, but code-wise nothing has changed there.

Related issue (if applicable): fixes #12852

Example entry for configuration.yaml (if applicable):

sensor:
  - platform: wunderground
    api_key: !secret wunderground_api_key
    monitored_conditions:
      - weather
      - weather_1d_metric
      - weather_1n_metric

Checklist:

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

If the code does not interact with devices:

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

Copy link
Copy Markdown
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!

@NovapaX
Copy link
Copy Markdown
Contributor

NovapaX commented Mar 17, 2018

Shouldn't the unique_id not be also based on the weather station id then?
What if the sensors will get initialized in another order then the first time, will they get a different unique-id then the first time?

@OttoWinter
Copy link
Copy Markdown
Member Author

This platform doesn't have any unique_id support and can't really get any unique_id support either (unless you want to change hundreds of entities every time you change the lat/long of the platform).

But concerning entity ids it is true that they're not "stable", that was also the previous behavior though. Thankfully we have an entity_namespace: option, I will put it in this PR.

@OttoWinter
Copy link
Copy Markdown
Member Author

OttoWinter commented Mar 17, 2018

I now implemented the "entity_namespace" option. If you specify

sensor:
  - platform: wunderground
    api_key: !secret wunderground_api_key
    entity_namespace: hi
    monitored_conditions:
      - weather
      - weather_1d_metric
      - weather_1n_metric

The sensors will be called sensor.hi_weather and so on instead of sensor.pws_weather.

(Compared to 0.62 this would technically be a breaking change, since in that version the namespaced sensors would be called sensor.hi_pws_weather, but compared to 0.63+ it's not; I personally like being able to hide the ugly pws...)

@NovapaX
Copy link
Copy Markdown
Contributor

NovapaX commented Mar 17, 2018

Oh... lol.. I completely misread this 🤦‍♂️
Good job!

@OttoWinter OttoWinter merged commit 3442b67 into home-assistant:dev Mar 17, 2018
@OttoWinter OttoWinter deleted the wunderground-duplicate-entity-id branch March 17, 2018 12:16
'Latitude and longitude must exist together'): cv.longitude,
vol.Required(CONF_MONITORED_CONDITIONS):
vol.All(cv.ensure_list, vol.Length(min=1), [vol.In(SENSOR_TYPES)]),
vol.Optional(CONF_ENTITY_NAMESPACE,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

@OttoWinter OttoWinter Mar 17, 2018

Choose a reason for hiding this comment

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

Yes, but we're generating our own entity ids here. So the namespace is never used in the entity component level - the entity namespace code is never executed in the entity component. we have to do it on our own here :/ (There might be another way though.)


DEFAULT_ENTITY_NAMESPACE = 'pws'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You are literally extending a platform schema here that includes entity namespace.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, there should be 🤔

self.rest.request_feature(SENSOR_TYPES[condition].feature)
current_ids = set(hass.states.async_entity_ids(sensor.DOMAIN))
current_ids |= hass.data[ADDED_ENTITY_IDS_KEY]
self.entity_id = async_generate_entity_id(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The real problem here in this component is that this entity is taking care of it's own entity ids. That is not the preferred approach. And guess what, because of this hack, we now need to add another hack to support entity namespace? Seems like a red flag to me…

Copy link
Copy Markdown
Member Author

@OttoWinter OttoWinter Mar 17, 2018

Choose a reason for hiding this comment

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

It is a hack - this whole platform is very hackish :/

I just don't see any other way we can do custom names + constant "stable" entity ids at the same time. I mean we could take a look at disabling custom names to clean up the platform. Or we could even at some point take a look at transitioning this to the weather component.

If you want I can revert this. I will also make sure to wait for more feedback before merging these sorts of PRs that are a bit of a hack.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that the real solution would be to do nothing with entity ids and just give it a unique id. Then people can rename it themselves.

For now let's keep it in, I guess? I don't really care that strongly.

self._entity_picture = None
self._unit_of_measurement = self._cfg_expand("unit_of_measurement")
self.rest.request_feature(SENSOR_TYPES[condition].feature)
current_ids = set(hass.states.async_entity_ids(sensor.DOMAIN))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why would you generate this in every constructor call? Just do it once and pass it into the constructor

Copy link
Copy Markdown
Member Author

@OttoWinter OttoWinter Mar 17, 2018

Choose a reason for hiding this comment

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

Hmm, true... that's inefficient indeed.

(on a side note: all entities (from all entity platforms) do not cache this: https://github.com/home-assistant/home-assistant/blob/4d3743f3f79a1127208ce885ae501991ec3e704e/homeassistant/helpers/entity_platform.py#L239-L240)

@OttoWinter OttoWinter mentioned this pull request Mar 18, 2018
3 tasks
@balloob balloob mentioned this pull request Mar 30, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Jul 26, 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.

Wunderground: Entity id already exists: sensor.pws_temp_f

5 participants