More Mobile app sensor fixes (#22914)
* Ensure we only add a sensor once * Ensure that we dont process updates for entities that arent what we were setup for * Add debug logging to ease development of apps * Use str representationpull/22935/head
parent
64ea13104e
commit
fd8d9747ef
|
@ -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)])
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)])
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue