Explicitly set unique ids for GDACS integration (#32203)

* explicitly set unique ids

* use config flow's unique id

* use config's unique id
This commit is contained in:
Malte Franken 2020-02-28 20:47:49 +10:00 committed by GitHub
parent 2390a7f365
commit adb3bb3653
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 6 deletions

View File

@ -190,7 +190,11 @@ class GdacsFeedEntityManager:
async def _generate_entity(self, external_id): async def _generate_entity(self, external_id):
"""Generate new entity.""" """Generate new entity."""
async_dispatcher_send( async_dispatcher_send(
self._hass, self.async_event_new_entity(), self, external_id self._hass,
self.async_event_new_entity(),
self,
self._config_entry.unique_id,
external_id,
) )
async def _update_entity(self, external_id): async def _update_entity(self, external_id):

View File

@ -49,9 +49,9 @@ async def async_setup_entry(hass, entry, async_add_entities):
manager = hass.data[DOMAIN][FEED][entry.entry_id] manager = hass.data[DOMAIN][FEED][entry.entry_id]
@callback @callback
def async_add_geolocation(feed_manager, external_id): def async_add_geolocation(feed_manager, integration_id, external_id):
"""Add gelocation entity from feed.""" """Add gelocation entity from feed."""
new_entity = GdacsEvent(feed_manager, external_id) new_entity = GdacsEvent(feed_manager, integration_id, external_id)
_LOGGER.debug("Adding geolocation %s", new_entity) _LOGGER.debug("Adding geolocation %s", new_entity)
async_add_entities([new_entity], True) async_add_entities([new_entity], True)
@ -69,9 +69,10 @@ async def async_setup_entry(hass, entry, async_add_entities):
class GdacsEvent(GeolocationEvent): class GdacsEvent(GeolocationEvent):
"""This represents an external event with GDACS feed data.""" """This represents an external event with GDACS feed data."""
def __init__(self, feed_manager, external_id): def __init__(self, feed_manager, integration_id, external_id):
"""Initialize entity with data from feed entry.""" """Initialize entity with data from feed entry."""
self._feed_manager = feed_manager self._feed_manager = feed_manager
self._integration_id = integration_id
self._external_id = external_id self._external_id = external_id
self._title = None self._title = None
self._distance = None self._distance = None
@ -162,6 +163,11 @@ class GdacsEvent(GeolocationEvent):
self._vulnerability = round(self._vulnerability, 1) self._vulnerability = round(self._vulnerability, 1)
self._version = feed_entry.version self._version = feed_entry.version
@property
def unique_id(self) -> Optional[str]:
"""Return a unique ID containing latitude/longitude and external id."""
return f"{self._integration_id}_{self._external_id}"
@property @property
def icon(self): def icon(self):
"""Return the icon to use in the frontend, if any.""" """Return the icon to use in the frontend, if any."""

View File

@ -28,7 +28,7 @@ PARALLEL_UPDATES = 0
async def async_setup_entry(hass, entry, async_add_entities): async def async_setup_entry(hass, entry, async_add_entities):
"""Set up the GDACS Feed platform.""" """Set up the GDACS Feed platform."""
manager = hass.data[DOMAIN][FEED][entry.entry_id] manager = hass.data[DOMAIN][FEED][entry.entry_id]
sensor = GdacsSensor(entry.entry_id, entry.title, manager) sensor = GdacsSensor(entry.entry_id, entry.unique_id, entry.title, manager)
async_add_entities([sensor]) async_add_entities([sensor])
_LOGGER.debug("Sensor setup done") _LOGGER.debug("Sensor setup done")
@ -36,9 +36,10 @@ async def async_setup_entry(hass, entry, async_add_entities):
class GdacsSensor(Entity): class GdacsSensor(Entity):
"""This is a status sensor for the GDACS integration.""" """This is a status sensor for the GDACS integration."""
def __init__(self, config_entry_id, config_title, manager): def __init__(self, config_entry_id, config_unique_id, config_title, manager):
"""Initialize entity.""" """Initialize entity."""
self._config_entry_id = config_entry_id self._config_entry_id = config_entry_id
self._config_unique_id = config_unique_id
self._config_title = config_title self._config_title = config_title
self._manager = manager self._manager = manager
self._status = None self._status = None
@ -107,6 +108,11 @@ class GdacsSensor(Entity):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._total return self._total
@property
def unique_id(self) -> Optional[str]:
"""Return a unique ID containing latitude/longitude."""
return self._config_unique_id
@property @property
def name(self) -> Optional[str]: def name(self) -> Optional[str]:
"""Return the name of the entity.""" """Return the name of the entity."""