Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log warning if disabled entities receive updates. #26143

Merged
merged 3 commits into from Aug 22, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions homeassistant/helpers/entity.py
Expand Up @@ -99,6 +99,9 @@ class Entity:
# If we reported if this entity was slow
_slow_reported = False

# If we reported this entity is updated while disabled
_disabled_reported = False

# Protect for multiple updates
_update_staged = False

Expand Down Expand Up @@ -273,6 +276,16 @@ def async_write_ha_state(self):
@callback
def _async_write_ha_state(self):
"""Write the state to the state machine."""
if self.registry_entry and self.registry_entry.disabled_by:
if not self._disabled_reported:
self._disabled_reported = True
_LOGGER.warning(
"Entity %s is incorrectly being triggered for updates while it is disabled. This is a bug in the %s integration.",
self.entity_id,
self.platform.platform_name,
)
return

start = timer()

attr = {}
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/helpers/entity_platform.py
Expand Up @@ -349,6 +349,9 @@ async def _async_add_entity(
disabled_by=disabled_by,
)

entity.registry_entry = entry
entity.entity_id = entry.entity_id

if entry.disabled:
self.logger.info(
"Not adding entity %s because it's disabled",
Expand All @@ -358,9 +361,6 @@ async def _async_add_entity(
)
return

entity.registry_entry = entry
entity.entity_id = entry.entity_id

# We won't generate an entity ID if the platform has already set one
# We will however make sure that platform cannot pick a registered ID
elif entity.entity_id is not None and entity_registry.async_is_registered(
Expand Down
19 changes: 16 additions & 3 deletions tests/components/config/test_entity_registry.py
Expand Up @@ -127,13 +127,13 @@ async def test_update_entity(hass, client):
assert state is not None
assert state.name == "before update"

# UPDATE NAME
await client.send_json(
{
"id": 6,
"type": "config/entity_registry/update",
"entity_id": "test_domain.world",
"name": "after update",
"disabled_by": "user",
}
)

Expand All @@ -142,7 +142,7 @@ async def test_update_entity(hass, client):
assert msg["result"] == {
"config_entry_id": None,
"device_id": None,
"disabled_by": "user",
"disabled_by": None,
"platform": "test_platform",
"entity_id": "test_domain.world",
"name": "after update",
Expand All @@ -151,11 +151,24 @@ async def test_update_entity(hass, client):
state = hass.states.get("test_domain.world")
assert state.name == "after update"

# UPDATE DISABLED_BY TO USER
await client.send_json(
{
"id": 7,
"type": "config/entity_registry/update",
"entity_id": "test_domain.world",
"disabled_by": "user",
}
)

msg = await client.receive_json()

assert registry.entities["test_domain.world"].disabled_by == "user"

# UPDATE DISABLED_BY TO NONE
await client.send_json(
{
"id": 7,
"id": 8,
"type": "config/entity_registry/update",
"entity_id": "test_domain.world",
"disabled_by": None,
Expand Down
31 changes: 29 additions & 2 deletions tests/helpers/test_entity.py
Expand Up @@ -7,13 +7,13 @@

import pytest

import homeassistant.helpers.entity as entity
from homeassistant.helpers import entity, entity_registry
from homeassistant.core import Context
from homeassistant.const import ATTR_HIDDEN, ATTR_DEVICE_CLASS
from homeassistant.config import DATA_CUSTOMIZE
from homeassistant.helpers.entity_values import EntityValues

from tests.common import get_test_home_assistant
from tests.common import get_test_home_assistant, mock_registry


def test_generate_entity_id_requires_hass_or_ids():
Expand Down Expand Up @@ -499,3 +499,30 @@ async def test_set_context_expired(hass):
assert hass.states.get("hello.world").context != context
assert ent._context is None
assert ent._context_set is None


async def test_warn_disabled(hass, caplog):
"""Test we warn once if we write to a disabled entity."""
entry = entity_registry.RegistryEntry(
entity_id="hello.world",
unique_id="test-unique-id",
platform="test-platform",
disabled_by="user",
)
mock_registry(hass, {"hello.world": entry})

ent = entity.Entity()
ent.hass = hass
ent.entity_id = "hello.world"
ent.registry_entry = entry
ent.platform = MagicMock(platform_name="test-platform")

caplog.clear()
ent.async_write_ha_state()
assert hass.states.get("hello.world") is None
assert "Entity hello.world is incorrectly being triggered" in caplog.text

caplog.clear()
ent.async_write_ha_state()
assert hass.states.get("hello.world") is None
assert caplog.text == ""
2 changes: 1 addition & 1 deletion tests/helpers/test_entity_platform.py
Expand Up @@ -491,7 +491,7 @@ async def test_registry_respect_entity_disabled(hass):
platform = MockEntityPlatform(hass)
entity = MockEntity(unique_id="1234")
await platform.async_add_entities([entity])
assert entity.entity_id is None
assert entity.entity_id == "test_domain.world"
assert hass.states.async_entity_ids() == []


Expand Down