Fix mobile_app sensor re-registration handling (#36567)
This commit is contained in:
parent
5975ec340b
commit
85ba29012f
3 changed files with 45 additions and 19 deletions
|
@ -78,7 +78,6 @@ from .const import (
|
|||
ERR_ENCRYPTION_NOT_AVAILABLE,
|
||||
ERR_ENCRYPTION_REQUIRED,
|
||||
ERR_INVALID_FORMAT,
|
||||
ERR_SENSOR_DUPLICATE_UNIQUE_ID,
|
||||
ERR_SENSOR_NOT_REGISTERED,
|
||||
SIGNAL_LOCATION_UPDATE,
|
||||
SIGNAL_SENSOR_UPDATE,
|
||||
|
@ -364,29 +363,30 @@ async def webhook_enable_encryption(hass, config_entry, data):
|
|||
async def webhook_register_sensor(hass, config_entry, data):
|
||||
"""Handle a register sensor webhook."""
|
||||
entity_type = data[ATTR_SENSOR_TYPE]
|
||||
|
||||
unique_id = data[ATTR_SENSOR_UNIQUE_ID]
|
||||
|
||||
unique_store_key = f"{config_entry.data[CONF_WEBHOOK_ID]}_{unique_id}"
|
||||
|
||||
if unique_store_key in hass.data[DOMAIN][entity_type]:
|
||||
_LOGGER.error("Refusing to re-register existing sensor %s!", unique_id)
|
||||
return error_response(
|
||||
ERR_SENSOR_DUPLICATE_UNIQUE_ID,
|
||||
f"{entity_type} {unique_id} already exists!",
|
||||
status=409,
|
||||
)
|
||||
existing_sensor = unique_store_key in hass.data[DOMAIN][entity_type]
|
||||
|
||||
data[CONF_WEBHOOK_ID] = config_entry.data[CONF_WEBHOOK_ID]
|
||||
|
||||
# If sensor already is registered, update current state instead
|
||||
if existing_sensor:
|
||||
_LOGGER.debug("Re-register existing sensor %s", unique_id)
|
||||
entry = hass.data[DOMAIN][entity_type][unique_store_key]
|
||||
data = {**entry, **data}
|
||||
|
||||
hass.data[DOMAIN][entity_type][unique_store_key] = data
|
||||
|
||||
hass.data[DOMAIN][DATA_STORE].async_delay_save(
|
||||
lambda: savable_state(hass), DELAY_SAVE
|
||||
)
|
||||
|
||||
register_signal = f"{DOMAIN}_{data[ATTR_SENSOR_TYPE]}_register"
|
||||
async_dispatcher_send(hass, register_signal, data)
|
||||
if existing_sensor:
|
||||
async_dispatcher_send(hass, SIGNAL_SENSOR_UPDATE, data)
|
||||
else:
|
||||
register_signal = f"{DOMAIN}_{data[ATTR_SENSOR_TYPE]}_register"
|
||||
async_dispatcher_send(hass, register_signal, data)
|
||||
|
||||
return webhook_response(
|
||||
{"success": True}, registration=config_entry.data, status=HTTP_CREATED,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue