Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions homeassistant/components/mobile_app/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@

from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID, CONF_WEBHOOK_ID, STATE_ON
from homeassistant.const import CONF_WEBHOOK_ID, STATE_ON
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import (
ATTR_DEVICE_NAME,
ATTR_SENSOR_ATTRIBUTES,
ATTR_SENSOR_DEVICE_CLASS,
ATTR_SENSOR_ENTITY_CATEGORY,
Expand All @@ -22,7 +21,7 @@
ATTR_SENSOR_UNIQUE_ID,
DOMAIN,
)
from .entity import MobileAppEntity, unique_id
from .entity import MobileAppEntity


async def async_setup_entry(
Expand Down Expand Up @@ -59,13 +58,6 @@ def handle_sensor_registration(data):
if data[CONF_WEBHOOK_ID] != webhook_id:
return

data[CONF_UNIQUE_ID] = unique_id(
data[CONF_WEBHOOK_ID], data[ATTR_SENSOR_UNIQUE_ID]
)
data[
CONF_NAME
] = f"{config_entry.data[ATTR_DEVICE_NAME]} {data[ATTR_SENSOR_NAME]}"

async_add_entities([MobileAppBinarySensor(data, config_entry)])

async_dispatcher_connect(
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/mobile_app/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

ATTR_SENSOR_ATTRIBUTES = "attributes"
ATTR_SENSOR_DEVICE_CLASS = "device_class"
ATTR_SENSOR_DEFAULT_DISABLED = "default_disabled"
ATTR_SENSOR_ENTITY_CATEGORY = "entity_category"
ATTR_SENSOR_ICON = "icon"
ATTR_SENSOR_NAME = "name"
Expand Down
43 changes: 13 additions & 30 deletions homeassistant/components/mobile_app/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,35 @@
from __future__ import annotations

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ICON,
CONF_NAME,
CONF_UNIQUE_ID,
CONF_WEBHOOK_ID,
STATE_UNAVAILABLE,
)
from homeassistant.const import ATTR_ICON, CONF_NAME, CONF_UNIQUE_ID, STATE_UNAVAILABLE
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.restore_state import RestoreEntity

from .const import (
ATTR_SENSOR_ATTRIBUTES,
ATTR_SENSOR_DEFAULT_DISABLED,
ATTR_SENSOR_DEVICE_CLASS,
ATTR_SENSOR_ENTITY_CATEGORY,
ATTR_SENSOR_ICON,
ATTR_SENSOR_STATE,
ATTR_SENSOR_TYPE,
ATTR_SENSOR_UNIQUE_ID,
SIGNAL_SENSOR_UPDATE,
)
from .helpers import device_info


def unique_id(webhook_id, sensor_unique_id):
"""Return a unique sensor ID."""
return f"{webhook_id}_{sensor_unique_id}"


class MobileAppEntity(RestoreEntity):
"""Representation of an mobile app entity."""

_attr_should_poll = False

def __init__(self, config: dict, entry: ConfigEntry) -> None:
"""Initialize the entity."""
self._config = config
self._entry = entry
self._registration = entry.data
self._unique_id = config[CONF_UNIQUE_ID]
self._entity_type = config[ATTR_SENSOR_TYPE]
self._name = config[CONF_NAME]
self._attr_unique_id = config[CONF_UNIQUE_ID]
self._name = self._config[CONF_NAME]

async def async_added_to_hass(self):
"""Register callbacks."""
Expand All @@ -67,16 +56,16 @@ def async_restore_last_state(self, last_state):
if ATTR_ICON in last_state.attributes:
self._config[ATTR_SENSOR_ICON] = last_state.attributes[ATTR_ICON]

@property
def should_poll(self) -> bool:
"""Declare that this entity pushes its state to HA."""
return False

@property
def name(self):
"""Return the name of the mobile app sensor."""
return self._name

@property
def entity_registry_enabled_default(self) -> bool:
"""Return if entity should be enabled by default."""
return not self._config.get(ATTR_SENSOR_DEFAULT_DISABLED)

@property
def device_class(self):
"""Return the device class."""
Expand All @@ -97,11 +86,6 @@ def entity_category(self):
"""Return the entity category, if any."""
return self._config.get(ATTR_SENSOR_ENTITY_CATEGORY)

@property
def unique_id(self):
"""Return the unique ID of this sensor."""
return self._unique_id

@property
def device_info(self):
"""Return device registry information for this entity."""
Expand All @@ -113,10 +97,9 @@ def available(self) -> bool:
return self._config.get(ATTR_SENSOR_STATE) != STATE_UNAVAILABLE

@callback
def _handle_update(self, data):
def _handle_update(self, incoming_id, data):
"""Handle async event updates."""
incoming_id = unique_id(data[CONF_WEBHOOK_ID], data[ATTR_SENSOR_UNIQUE_ID])
if incoming_id != self._unique_id:
if incoming_id != self._attr_unique_id:
return

self._config = {**self._config, **data}
Expand Down
17 changes: 2 additions & 15 deletions homeassistant/components/mobile_app/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@

from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_NAME,
CONF_UNIQUE_ID,
CONF_WEBHOOK_ID,
STATE_UNKNOWN,
)
from homeassistant.const import CONF_WEBHOOK_ID, STATE_UNKNOWN
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import dt as dt_util

from .const import (
ATTR_DEVICE_NAME,
ATTR_SENSOR_ATTRIBUTES,
ATTR_SENSOR_DEVICE_CLASS,
ATTR_SENSOR_ENTITY_CATEGORY,
Expand All @@ -32,7 +26,7 @@
ATTR_SENSOR_UOM,
DOMAIN,
)
from .entity import MobileAppEntity, unique_id
from .entity import MobileAppEntity


async def async_setup_entry(
Expand Down Expand Up @@ -70,13 +64,6 @@ def handle_sensor_registration(data):
if data[CONF_WEBHOOK_ID] != webhook_id:
return

data[CONF_UNIQUE_ID] = unique_id(
data[CONF_WEBHOOK_ID], data[ATTR_SENSOR_UNIQUE_ID]
)
data[
CONF_NAME
] = f"{config_entry.data[ATTR_DEVICE_NAME]} {data[ATTR_SENSOR_NAME]}"

async_add_entities([MobileAppSensor(data, config_entry)])

async_dispatcher_connect(
Expand Down
27 changes: 23 additions & 4 deletions homeassistant/components/mobile_app/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
ATTR_SERVICE,
ATTR_SERVICE_DATA,
ATTR_SUPPORTED_FEATURES,
CONF_NAME,
CONF_UNIQUE_ID,
CONF_WEBHOOK_ID,
)
from homeassistant.core import EventOrigin, HomeAssistant
Expand Down Expand Up @@ -62,6 +64,7 @@
ATTR_NO_LEGACY_ENCRYPTION,
ATTR_OS_VERSION,
ATTR_SENSOR_ATTRIBUTES,
ATTR_SENSOR_DEFAULT_DISABLED,
ATTR_SENSOR_DEVICE_CLASS,
ATTR_SENSOR_ENTITY_CATEGORY,
ATTR_SENSOR_ICON,
Expand Down Expand Up @@ -431,6 +434,11 @@ def _validate_state_class_sensor(value: dict):
return value


def _gen_unique_id(webhook_id, sensor_unique_id):
"""Return a unique sensor ID."""
return f"{webhook_id}_{sensor_unique_id}"


@WEBHOOK_COMMANDS.register("register_sensor")
@validate_schema(
vol.All(
Expand All @@ -449,6 +457,7 @@ def _validate_state_class_sensor(value: dict):
vol.Optional(ATTR_SENSOR_ENTITY_CATEGORY): ENTITY_CATEGORIES_SCHEMA,
vol.Optional(ATTR_SENSOR_ICON, default="mdi:cellphone"): cv.icon,
vol.Optional(ATTR_SENSOR_STATE_CLASS): vol.In(SENSOSR_STATE_CLASSES),
vol.Optional(ATTR_SENSOR_DEFAULT_DISABLED): bool,
},
_validate_state_class_sensor,
)
Expand All @@ -459,7 +468,7 @@ async def webhook_register_sensor(hass, config_entry, data):
unique_id = data[ATTR_SENSOR_UNIQUE_ID]
device_name = config_entry.data[ATTR_DEVICE_NAME]

unique_store_key = f"{config_entry.data[CONF_WEBHOOK_ID]}_{unique_id}"
unique_store_key = _gen_unique_id(config_entry.data[CONF_WEBHOOK_ID], unique_id)
entity_registry = er.async_get(hass)
existing_sensor = entity_registry.async_get_entity_id(
entity_type, DOMAIN, unique_store_key
Expand Down Expand Up @@ -493,8 +502,13 @@ async def webhook_register_sensor(hass, config_entry, data):
if changes:
entity_registry.async_update_entity(existing_sensor, **changes)

async_dispatcher_send(hass, SIGNAL_SENSOR_UPDATE, data)
async_dispatcher_send(hass, SIGNAL_SENSOR_UPDATE, unique_store_key, data)
else:
data[CONF_UNIQUE_ID] = unique_store_key
data[
CONF_NAME
] = f"{config_entry.data[ATTR_DEVICE_NAME]} {data[ATTR_SENSOR_NAME]}"

register_signal = f"{DOMAIN}_{data[ATTR_SENSOR_TYPE]}_register"
async_dispatcher_send(hass, register_signal, data)

Expand Down Expand Up @@ -543,7 +557,7 @@ async def webhook_update_sensor_states(hass, config_entry, data):

unique_id = sensor[ATTR_SENSOR_UNIQUE_ID]

unique_store_key = f"{config_entry.data[CONF_WEBHOOK_ID]}_{unique_id}"
unique_store_key = _gen_unique_id(config_entry.data[CONF_WEBHOOK_ID], unique_id)

entity_registry = er.async_get(hass)
if not entity_registry.async_get_entity_id(
Expand Down Expand Up @@ -578,7 +592,12 @@ async def webhook_update_sensor_states(hass, config_entry, data):
continue

sensor[CONF_WEBHOOK_ID] = config_entry.data[CONF_WEBHOOK_ID]
async_dispatcher_send(hass, SIGNAL_SENSOR_UPDATE, sensor)
async_dispatcher_send(
hass,
SIGNAL_SENSOR_UPDATE,
unique_store_key,
sensor,
)

resp[unique_id] = {"success": True}

Expand Down
33 changes: 33 additions & 0 deletions tests/components/mobile_app/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,36 @@ async def test_sensor_datetime(
assert entity.attributes["device_class"] == device_class
assert entity.domain == "sensor"
assert entity.state == state_value


async def test_default_disabling_entity(hass, create_registrations, webhook_client):
"""Test that sensors can be disabled by default upon registration."""
webhook_id = create_registrations[1]["webhook_id"]
webhook_url = f"/api/webhook/{webhook_id}"

reg_resp = await webhook_client.post(
webhook_url,
json={
"type": "register_sensor",
"data": {
"name": "Battery State",
"type": "sensor",
"unique_id": "battery_state",
"default_disabled": True,
},
},
)

assert reg_resp.status == HTTPStatus.CREATED

json = await reg_resp.json()
assert json == {"success": True}
await hass.async_block_till_done()

entity = hass.states.get("sensor.test_1_battery_state")
assert entity is None

assert (
er.async_get(hass).async_get("sensor.test_1_battery_state").disabled_by
== er.RegistryEntryDisabler.INTEGRATION
)