Fix WUnderground duplicate entity ids#13285
Conversation
|
Shouldn't the unique_id not be also based on the weather station id then? |
|
This platform doesn't have any But concerning entity ids it is true that they're not "stable", that was also the previous behavior though. Thankfully we have an |
|
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_metricThe sensors will be called (Compared to 0.62 this would technically be a breaking change, since in that version the namespaced sensors would be called |
|
Oh... lol.. I completely misread this 🤦♂️ |
| '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, |
There was a problem hiding this comment.
This is wrong. Entity namespace is implemented on the entity component level: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/helpers/entity_component.py#L138
There was a problem hiding this comment.
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({ |
There was a problem hiding this comment.
You are literally extending a platform schema here that includes entity namespace.
There was a problem hiding this comment.
As far as I saw, there was no ENTITY_NAMESPACE in the original platform schema:
| 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( |
There was a problem hiding this comment.
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…
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
Why would you generate this in every constructor call? Just do it once and pass it into the constructor
There was a problem hiding this comment.
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)
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):Checklist:
tox. Your PR cannot be merged unless tests passIf the code does not interact with devices: