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

Add disabled entities support to WLED #31040

Merged
merged 1 commit into from Jan 22, 2020
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
18 changes: 17 additions & 1 deletion homeassistant/components/wled/__init__.py
Expand Up @@ -118,10 +118,18 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
class WLEDEntity(Entity):
"""Defines a base WLED entity."""

def __init__(self, entry_id: str, wled: WLED, name: str, icon: str) -> None:
def __init__(
self,
entry_id: str,
wled: WLED,
name: str,
icon: str,
enabled_default: bool = True,
) -> None:
"""Initialize the WLED entity."""
self._attributes: Dict[str, Union[str, int, float]] = {}
self._available = True
self._enabled_default = enabled_default
self._entry_id = entry_id
self._icon = icon
self._name = name
Expand All @@ -143,6 +151,11 @@ def available(self) -> bool:
"""Return True if entity is available."""
return self._available

@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return self._enabled_default

@property
def should_poll(self) -> bool:
"""Return the polling requirement of the entity."""
Expand Down Expand Up @@ -171,6 +184,9 @@ def _schedule_immediate_update(self, entry_id: str) -> None:

async def async_update(self) -> None:
"""Update WLED entity."""
if not self.enabled:
return

if self.wled.device is None:
self._available = False
return
Expand Down
5 changes: 4 additions & 1 deletion homeassistant/components/wled/sensor.py
Expand Up @@ -50,13 +50,14 @@ def __init__(
icon: str,
unit_of_measurement: str,
key: str,
enabled_default: bool = True,
) -> None:
"""Initialize WLED sensor."""
self._state = None
self._unit_of_measurement = unit_of_measurement
self._key = key

super().__init__(entry_id, wled, name, icon)
super().__init__(entry_id, wled, name, icon, enabled_default)

@property
def unique_id(self) -> str:
Expand Down Expand Up @@ -109,6 +110,7 @@ def __init__(self, entry_id: str, wled: WLED) -> None:
"mdi:clock-outline",
None,
"uptime",
enabled_default=False,
)

@property
Expand All @@ -134,6 +136,7 @@ def __init__(self, entry_id: str, wled: WLED) -> None:
"mdi:memory",
DATA_BYTES,
"free_heap",
enabled_default=False,
)

async def _wled_update(self) -> None:
Expand Down
58 changes: 52 additions & 6 deletions tests/components/wled/test_sensor.py
Expand Up @@ -3,11 +3,13 @@

from asynctest import patch

from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.wled.const import (
ATTR_LED_COUNT,
ATTR_MAX_POWER,
CURRENT_MA,
DATA_BYTES,
DOMAIN,
)
from homeassistant.const import ATTR_ICON, ATTR_UNIT_OF_MEASUREMENT
from homeassistant.core import HomeAssistant
Expand All @@ -22,11 +24,31 @@ async def test_sensors(
) -> None:
"""Test the creation and values of the WLED sensors."""

entry = await init_integration(hass, aioclient_mock, skip_setup=True)
registry = await hass.helpers.entity_registry.async_get_registry()

# Pre-create registry entries for disabled by default sensors
registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
"aabbccddeeff_uptime",
suggested_object_id="wled_rgb_light_uptime",
disabled_by=None,
)

registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
"aabbccddeeff_free_heap",
suggested_object_id="wled_rgb_light_free_memory",
disabled_by=None,
)

# Setup
test_time = datetime(2019, 11, 11, 9, 10, 32, tzinfo=dt_util.UTC)
with patch("homeassistant.components.wled.sensor.utcnow", return_value=test_time):
await init_integration(hass, aioclient_mock)

entity_registry = await hass.helpers.entity_registry.async_get_registry()
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

state = hass.states.get("sensor.wled_rgb_light_estimated_current")
assert state
Expand All @@ -36,7 +58,7 @@ async def test_sensors(
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == CURRENT_MA
assert state.state == "470"

entry = entity_registry.async_get("sensor.wled_rgb_light_estimated_current")
entry = registry.async_get("sensor.wled_rgb_light_estimated_current")
assert entry
assert entry.unique_id == "aabbccddeeff_estimated_current"

Expand All @@ -46,7 +68,7 @@ async def test_sensors(
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
assert state.state == "2019-11-11T09:10:00+00:00"

entry = entity_registry.async_get("sensor.wled_rgb_light_uptime")
entry = registry.async_get("sensor.wled_rgb_light_uptime")
assert entry
assert entry.unique_id == "aabbccddeeff_uptime"

Expand All @@ -56,6 +78,30 @@ async def test_sensors(
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_BYTES
assert state.state == "14600"

entry = entity_registry.async_get("sensor.wled_rgb_light_free_memory")
entry = registry.async_get("sensor.wled_rgb_light_free_memory")
assert entry
assert entry.unique_id == "aabbccddeeff_free_heap"


async def test_disabled_by_default_sensors(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the disabled by default WLED sensors."""
await init_integration(hass, aioclient_mock)
registry = await hass.helpers.entity_registry.async_get_registry()

state = hass.states.get("sensor.wled_rgb_light_uptime")
assert state is None

entry = registry.async_get("sensor.wled_rgb_light_uptime")
assert entry
assert entry.disabled
assert entry.disabled_by == "integration"

state = hass.states.get("sensor.wled_rgb_light_free_memory")
assert state is None

entry = registry.async_get("sensor.wled_rgb_light_free_memory")
assert entry
assert entry.disabled
assert entry.disabled_by == "integration"