Add enabled attribute to zwave_js discovery model (#53645)

* Add attribute to zwave_js discovery model

* Fix binary sensor entity enabled logic

* Add tests
pull/53677/head
Raman Gupta 2021-07-29 03:30:50 -04:00 committed by GitHub
parent dc1b2b7687
commit 1019ee22ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 78 additions and 21 deletions

View File

@ -277,12 +277,6 @@ class ZWaveBooleanBinarySensor(ZWaveBaseEntity, BinarySensorEntity):
if self.info.primary_value.command_class == CommandClass.BATTERY
else None
)
# Legacy binary sensors are phased out (replaced by notification sensors)
# Disable by default to not confuse users
self._attr_entity_registry_enabled_default = bool(
self.info.primary_value.command_class != CommandClass.SENSOR_BINARY
or self.info.node.device_class.generic.key == 0x20
)
@property
def is_on(self) -> bool | None:

View File

@ -67,6 +67,8 @@ class ZwaveDiscoveryInfo:
platform_data: dict[str, Any] | None = None
# additional values that need to be watched by entity
additional_value_ids_to_watch: set[str] | None = None
# bool to specify whether entity should be enabled by default
entity_registry_enabled_default: bool = True
@dataclass
@ -135,6 +137,8 @@ class ZWaveDiscoverySchema:
allow_multi: bool = False
# [optional] bool to specify whether state is assumed and events should be fired on value update
assumed_state: bool = False
# [optional] bool to specify whether entity should be enabled by default
entity_registry_enabled_default: bool = True
def get_config_parameter_discovery_schema(
@ -161,6 +165,7 @@ def get_config_parameter_discovery_schema(
property_key_name=property_key_name,
type={"number"},
),
entity_registry_enabled_default=False,
**kwargs,
)
@ -428,12 +433,33 @@ DISCOVERY_SCHEMAS = [
],
),
# binary sensors
# When CC is Sensor Binary and device class generic is Binary Sensor, entity should
# be enabled by default
ZWaveDiscoverySchema(
platform="binary_sensor",
hint="boolean",
device_class_generic={"Binary Sensor"},
primary_value=ZWaveValueDiscoverySchema(
command_class={CommandClass.SENSOR_BINARY},
type={"boolean"},
),
),
# Legacy binary sensors are phased out (replaced by notification sensors)
# Disable by default to not confuse users
ZWaveDiscoverySchema(
platform="binary_sensor",
hint="boolean",
primary_value=ZWaveValueDiscoverySchema(
command_class={CommandClass.SENSOR_BINARY},
type={"boolean"},
),
entity_registry_enabled_default=False,
),
ZWaveDiscoverySchema(
platform="binary_sensor",
hint="boolean",
primary_value=ZWaveValueDiscoverySchema(
command_class={
CommandClass.SENSOR_BINARY,
CommandClass.BATTERY,
CommandClass.SENSOR_ALARM,
},
@ -456,13 +482,19 @@ DISCOVERY_SCHEMAS = [
platform="sensor",
hint="string_sensor",
primary_value=ZWaveValueDiscoverySchema(
command_class={
CommandClass.SENSOR_ALARM,
CommandClass.INDICATOR,
},
command_class={CommandClass.SENSOR_ALARM},
type={"string"},
),
),
ZWaveDiscoverySchema(
platform="sensor",
hint="string_sensor",
primary_value=ZWaveValueDiscoverySchema(
command_class={CommandClass.INDICATOR},
type={"string"},
),
entity_registry_enabled_default=False,
),
# generic numeric sensors
ZWaveDiscoverySchema(
platform="sensor",
@ -471,12 +503,20 @@ DISCOVERY_SCHEMAS = [
command_class={
CommandClass.SENSOR_MULTILEVEL,
CommandClass.SENSOR_ALARM,
CommandClass.INDICATOR,
CommandClass.BATTERY,
},
type={"number"},
),
),
ZWaveDiscoverySchema(
platform="sensor",
hint="numeric_sensor",
primary_value=ZWaveValueDiscoverySchema(
command_class={CommandClass.INDICATOR},
type={"number"},
),
entity_registry_enabled_default=False,
),
# numeric sensors for Meter CC
ZWaveDiscoverySchema(
platform="sensor",
@ -500,6 +540,7 @@ DISCOVERY_SCHEMAS = [
type={"number"},
),
allow_multi=True,
entity_registry_enabled_default=False,
),
# sensor for basic CC
ZWaveDiscoverySchema(
@ -512,6 +553,7 @@ DISCOVERY_SCHEMAS = [
type={"number"},
property={"currentValue"},
),
entity_registry_enabled_default=False,
),
# binary switches
ZWaveDiscoverySchema(
@ -697,6 +739,7 @@ def async_discover_values(node: ZwaveNode) -> Generator[ZwaveDiscoveryInfo, None
platform_data_template=schema.data_template,
platform_data=resolved_data,
additional_value_ids_to_watch=additional_value_ids_to_watch,
entity_registry_enabled_default=schema.entity_registry_enabled_default,
)
if not schema.allow_multi:

View File

@ -46,6 +46,9 @@ class ZWaveBaseEntity(Entity):
self._attr_unique_id = get_unique_id(
self.client.driver.controller.home_id, self.info.primary_value.value_id
)
self._attr_entity_registry_enabled_default = (
self.info.entity_registry_enabled_default
)
self._attr_assumed_state = self.info.assumed_state
# device is precreated in main handler
self._attr_device_info = {

View File

@ -118,15 +118,6 @@ class ZwaveSensorBase(ZWaveBaseEntity, SensorEntity):
self._attr_name = self.generate_name(include_value_name=True)
self._attr_device_class = self._get_device_class()
self._attr_state_class = self._get_state_class()
self._attr_entity_registry_enabled_default = True
# We hide some of the more advanced sensors by default to not overwhelm users
if self.info.primary_value.command_class in [
CommandClass.BASIC,
CommandClass.CONFIGURATION,
CommandClass.INDICATOR,
CommandClass.NOTIFICATION,
]:
self._attr_entity_registry_enabled_default = False
def _get_device_class(self) -> str | None:
"""

View File

@ -11,6 +11,8 @@ NOTIFICATION_MOTION_BINARY_SENSOR = (
"binary_sensor.multisensor_6_home_security_motion_detection"
)
NOTIFICATION_MOTION_SENSOR = "sensor.multisensor_6_home_security_motion_sensor_status"
INDICATOR_SENSOR = "sensor.z_wave_thermostat_indicator_value"
BASIC_SENSOR = "sensor.livingroomlight_basic"
PROPERTY_DOOR_STATUS_BINARY_SENSOR = (
"binary_sensor.august_smart_lock_pro_3rd_gen_the_current_status_of_the_door"
)

View File

@ -21,9 +21,11 @@ from homeassistant.helpers import entity_registry as er
from .common import (
AIR_TEMPERATURE_SENSOR,
BASIC_SENSOR,
ENERGY_SENSOR,
HUMIDITY_SENSOR,
ID_LOCK_CONFIG_PARAMETER_SENSOR,
INDICATOR_SENSOR,
NOTIFICATION_MOTION_SENSOR,
POWER_SENSOR,
)
@ -88,6 +90,28 @@ async def test_disabled_notification_sensor(hass, multisensor_6, integration):
assert state.attributes["value"] == 8
async def test_disabled_indcator_sensor(
hass, climate_radio_thermostat_ct100_plus, integration
):
"""Test sensor is created from Indicator CC and is disabled."""
ent_reg = er.async_get(hass)
entity_entry = ent_reg.async_get(INDICATOR_SENSOR)
assert entity_entry
assert entity_entry.disabled
assert entity_entry.disabled_by == er.DISABLED_INTEGRATION
async def test_disabled_basic_sensor(hass, ge_in_wall_dimmer_switch, integration):
"""Test sensor is created from Basic CC and is disabled."""
ent_reg = er.async_get(hass)
entity_entry = ent_reg.async_get(BASIC_SENSOR)
assert entity_entry
assert entity_entry.disabled
assert entity_entry.disabled_by == er.DISABLED_INTEGRATION
async def test_config_parameter_sensor(hass, lock_id_lock_as_id150, integration):
"""Test config parameter sensor is created."""
ent_reg = er.async_get(hass)