diff --git a/homeassistant/components/mobile_app/binary_sensor.py b/homeassistant/components/mobile_app/binary_sensor.py index 289a50584c92..50943bb6504d 100644 --- a/homeassistant/components/mobile_app/binary_sensor.py +++ b/homeassistant/components/mobile_app/binary_sensor.py @@ -8,9 +8,10 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from .const import (ATTR_SENSOR_STATE, ATTR_SENSOR_TYPE_BINARY_SENSOR as ENTITY_TYPE, + ATTR_SENSOR_UNIQUE_ID, DATA_DEVICES, DOMAIN) -from .entity import MobileAppEntity +from .entity import MobileAppEntity, sensor_id DEPENDENCIES = ['mobile_app'] @@ -36,6 +37,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities): if data[CONF_WEBHOOK_ID] != webhook_id: return + unique_id = sensor_id(data[CONF_WEBHOOK_ID], + data[ATTR_SENSOR_UNIQUE_ID]) + + entity = hass.data[DOMAIN][ENTITY_TYPE][unique_id] + + if 'added' in entity: + return + + entity['added'] = True + device = hass.data[DOMAIN][DATA_DEVICES][data[CONF_WEBHOOK_ID]] async_add_entities([MobileAppBinarySensor(data, device, config_entry)]) diff --git a/homeassistant/components/mobile_app/entity.py b/homeassistant/components/mobile_app/entity.py index 05736b3a689b..eca9d2b024bd 100644 --- a/homeassistant/components/mobile_app/entity.py +++ b/homeassistant/components/mobile_app/entity.py @@ -13,6 +13,11 @@ from .const import (ATTR_DEVICE_ID, ATTR_DEVICE_NAME, ATTR_MANUFACTURER, DOMAIN, SIGNAL_SENSOR_UPDATE) +def sensor_id(webhook_id, unique_id): + """Return a unique sensor ID.""" + return "{}_{}".format(webhook_id, unique_id) + + class MobileAppEntity(Entity): """Representation of an mobile app entity.""" @@ -22,8 +27,8 @@ class MobileAppEntity(Entity): self._device = device self._entry = entry self._registration = entry.data - self._sensor_id = "{}_{}".format(self._registration[CONF_WEBHOOK_ID], - config[ATTR_SENSOR_UNIQUE_ID]) + self._sensor_id = sensor_id(self._registration[CONF_WEBHOOK_ID], + config[ATTR_SENSOR_UNIQUE_ID]) self._entity_type = config[ATTR_SENSOR_TYPE] self.unsub_dispatcher = None @@ -94,5 +99,10 @@ class MobileAppEntity(Entity): @callback def _handle_update(self, data): """Handle async event updates.""" + incoming_id = sensor_id(data[CONF_WEBHOOK_ID], + data[ATTR_SENSOR_UNIQUE_ID]) + if incoming_id != self._sensor_id: + return + self._config = data self.async_schedule_update_ha_state() diff --git a/homeassistant/components/mobile_app/sensor.py b/homeassistant/components/mobile_app/sensor.py index b2846a6002b1..64ad69c5758e 100644 --- a/homeassistant/components/mobile_app/sensor.py +++ b/homeassistant/components/mobile_app/sensor.py @@ -7,9 +7,10 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from .const import (ATTR_SENSOR_STATE, ATTR_SENSOR_TYPE_SENSOR as ENTITY_TYPE, - ATTR_SENSOR_UOM, DATA_DEVICES, DOMAIN) + ATTR_SENSOR_UNIQUE_ID, ATTR_SENSOR_UOM, DATA_DEVICES, + DOMAIN) -from .entity import MobileAppEntity +from .entity import MobileAppEntity, sensor_id DEPENDENCIES = ['mobile_app'] @@ -35,6 +36,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities): if data[CONF_WEBHOOK_ID] != webhook_id: return + unique_id = sensor_id(data[CONF_WEBHOOK_ID], + data[ATTR_SENSOR_UNIQUE_ID]) + + entity = hass.data[DOMAIN][ENTITY_TYPE][unique_id] + + if 'added' in entity: + return + + entity['added'] = True + device = hass.data[DOMAIN][DATA_DEVICES][data[CONF_WEBHOOK_ID]] async_add_entities([MobileAppSensor(data, device, config_entry)]) diff --git a/homeassistant/components/mobile_app/webhook.py b/homeassistant/components/mobile_app/webhook.py index 2a3d23731cb9..1ef5f4ce5317 100644 --- a/homeassistant/components/mobile_app/webhook.py +++ b/homeassistant/components/mobile_app/webhook.py @@ -99,6 +99,9 @@ async def handle_webhook(hass: HomeAssistantType, webhook_id: str, data = webhook_payload + _LOGGER.debug("Received webhook payload for type %s: %s", webhook_type, + data) + if webhook_type in WEBHOOK_SCHEMAS: try: data = WEBHOOK_SCHEMAS[webhook_type](webhook_payload)