Add disabled entities support to WLED (#31040)
This commit is contained in:
parent
96dba7b91f
commit
db76b91ffa
3 changed files with 73 additions and 8 deletions
|
@ -118,10 +118,18 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
class WLEDEntity(Entity):
|
class WLEDEntity(Entity):
|
||||||
"""Defines a base WLED 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."""
|
"""Initialize the WLED entity."""
|
||||||
self._attributes: Dict[str, Union[str, int, float]] = {}
|
self._attributes: Dict[str, Union[str, int, float]] = {}
|
||||||
self._available = True
|
self._available = True
|
||||||
|
self._enabled_default = enabled_default
|
||||||
self._entry_id = entry_id
|
self._entry_id = entry_id
|
||||||
self._icon = icon
|
self._icon = icon
|
||||||
self._name = name
|
self._name = name
|
||||||
|
@ -143,6 +151,11 @@ class WLEDEntity(Entity):
|
||||||
"""Return True if entity is available."""
|
"""Return True if entity is available."""
|
||||||
return self._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
|
@property
|
||||||
def should_poll(self) -> bool:
|
def should_poll(self) -> bool:
|
||||||
"""Return the polling requirement of the entity."""
|
"""Return the polling requirement of the entity."""
|
||||||
|
@ -171,6 +184,9 @@ class WLEDEntity(Entity):
|
||||||
|
|
||||||
async def async_update(self) -> None:
|
async def async_update(self) -> None:
|
||||||
"""Update WLED entity."""
|
"""Update WLED entity."""
|
||||||
|
if not self.enabled:
|
||||||
|
return
|
||||||
|
|
||||||
if self.wled.device is None:
|
if self.wled.device is None:
|
||||||
self._available = False
|
self._available = False
|
||||||
return
|
return
|
||||||
|
|
|
@ -50,13 +50,14 @@ class WLEDSensor(WLEDDeviceEntity):
|
||||||
icon: str,
|
icon: str,
|
||||||
unit_of_measurement: str,
|
unit_of_measurement: str,
|
||||||
key: str,
|
key: str,
|
||||||
|
enabled_default: bool = True,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize WLED sensor."""
|
"""Initialize WLED sensor."""
|
||||||
self._state = None
|
self._state = None
|
||||||
self._unit_of_measurement = unit_of_measurement
|
self._unit_of_measurement = unit_of_measurement
|
||||||
self._key = key
|
self._key = key
|
||||||
|
|
||||||
super().__init__(entry_id, wled, name, icon)
|
super().__init__(entry_id, wled, name, icon, enabled_default)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self) -> str:
|
def unique_id(self) -> str:
|
||||||
|
@ -109,6 +110,7 @@ class WLEDUptimeSensor(WLEDSensor):
|
||||||
"mdi:clock-outline",
|
"mdi:clock-outline",
|
||||||
None,
|
None,
|
||||||
"uptime",
|
"uptime",
|
||||||
|
enabled_default=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -134,6 +136,7 @@ class WLEDFreeHeapSensor(WLEDSensor):
|
||||||
"mdi:memory",
|
"mdi:memory",
|
||||||
DATA_BYTES,
|
DATA_BYTES,
|
||||||
"free_heap",
|
"free_heap",
|
||||||
|
enabled_default=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _wled_update(self) -> None:
|
async def _wled_update(self) -> None:
|
||||||
|
|
|
@ -3,11 +3,13 @@ from datetime import datetime
|
||||||
|
|
||||||
from asynctest import patch
|
from asynctest import patch
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||||
from homeassistant.components.wled.const import (
|
from homeassistant.components.wled.const import (
|
||||||
ATTR_LED_COUNT,
|
ATTR_LED_COUNT,
|
||||||
ATTR_MAX_POWER,
|
ATTR_MAX_POWER,
|
||||||
CURRENT_MA,
|
CURRENT_MA,
|
||||||
DATA_BYTES,
|
DATA_BYTES,
|
||||||
|
DOMAIN,
|
||||||
)
|
)
|
||||||
from homeassistant.const import ATTR_ICON, ATTR_UNIT_OF_MEASUREMENT
|
from homeassistant.const import ATTR_ICON, ATTR_UNIT_OF_MEASUREMENT
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
@ -22,11 +24,31 @@ async def test_sensors(
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test the creation and values of the WLED sensors."""
|
"""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)
|
test_time = datetime(2019, 11, 11, 9, 10, 32, tzinfo=dt_util.UTC)
|
||||||
with patch("homeassistant.components.wled.sensor.utcnow", return_value=test_time):
|
with patch("homeassistant.components.wled.sensor.utcnow", return_value=test_time):
|
||||||
await init_integration(hass, aioclient_mock)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
|
||||||
|
|
||||||
state = hass.states.get("sensor.wled_rgb_light_estimated_current")
|
state = hass.states.get("sensor.wled_rgb_light_estimated_current")
|
||||||
assert state
|
assert state
|
||||||
|
@ -36,7 +58,7 @@ async def test_sensors(
|
||||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == CURRENT_MA
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == CURRENT_MA
|
||||||
assert state.state == "470"
|
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
|
||||||
assert entry.unique_id == "aabbccddeeff_estimated_current"
|
assert entry.unique_id == "aabbccddeeff_estimated_current"
|
||||||
|
|
||||||
|
@ -46,7 +68,7 @@ async def test_sensors(
|
||||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
|
||||||
assert state.state == "2019-11-11T09:10:00+00:00"
|
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
|
||||||
assert entry.unique_id == "aabbccddeeff_uptime"
|
assert entry.unique_id == "aabbccddeeff_uptime"
|
||||||
|
|
||||||
|
@ -56,6 +78,30 @@ async def test_sensors(
|
||||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_BYTES
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_BYTES
|
||||||
assert state.state == "14600"
|
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
|
||||||
assert entry.unique_id == "aabbccddeeff_free_heap"
|
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"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue