Use _attr_should_poll in zha entities (#77175)

This commit is contained in:
epenet 2022-08-23 03:38:26 +02:00 committed by GitHub
parent ce3502291d
commit 325557c3e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 46 deletions

View File

@ -49,13 +49,14 @@ async def async_setup_entry(
class ZHADeviceScannerEntity(ScannerEntity, ZhaEntity): class ZHADeviceScannerEntity(ScannerEntity, ZhaEntity):
"""Represent a tracked device.""" """Represent a tracked device."""
_attr_should_poll = True # BaseZhaEntity defaults to False
def __init__(self, unique_id, zha_device, channels, **kwargs): def __init__(self, unique_id, zha_device, channels, **kwargs):
"""Initialize the ZHA device tracker.""" """Initialize the ZHA device tracker."""
super().__init__(unique_id, zha_device, channels, **kwargs) super().__init__(unique_id, zha_device, channels, **kwargs)
self._battery_channel = self.cluster_channels.get(CHANNEL_POWER_CONFIGURATION) self._battery_channel = self.cluster_channels.get(CHANNEL_POWER_CONFIGURATION)
self._connected = False self._connected = False
self._keepalive_interval = 60 self._keepalive_interval = 60
self._should_poll = True
self._battery_level = None self._battery_level = None
async def async_added_to_hass(self): async def async_added_to_hass(self):

View File

@ -49,12 +49,12 @@ class BaseZhaEntity(LogMixin, entity.Entity):
unique_id_suffix: str | None = None unique_id_suffix: str | None = None
_attr_has_entity_name = True _attr_has_entity_name = True
_attr_should_poll = False
def __init__(self, unique_id: str, zha_device: ZHADevice, **kwargs: Any) -> None: def __init__(self, unique_id: str, zha_device: ZHADevice, **kwargs: Any) -> None:
"""Init ZHA entity.""" """Init ZHA entity."""
self._name: str = "" self._name: str = ""
self._force_update: bool = False self._force_update: bool = False
self._should_poll: bool = False
self._unique_id: str = unique_id self._unique_id: str = unique_id
if self.unique_id_suffix: if self.unique_id_suffix:
self._unique_id += f"-{self.unique_id_suffix}" self._unique_id += f"-{self.unique_id_suffix}"
@ -89,11 +89,6 @@ class BaseZhaEntity(LogMixin, entity.Entity):
"""Force update this entity.""" """Force update this entity."""
return self._force_update return self._force_update
@property
def should_poll(self) -> bool:
"""Poll state from device."""
return self._should_poll
@property @property
def device_info(self) -> entity.DeviceInfo: def device_info(self) -> entity.DeviceInfo:
"""Return a device description for device registry.""" """Return a device description for device registry."""

View File

@ -266,15 +266,11 @@ class ElectricalMeasurement(Sensor):
SENSOR_ATTR = "active_power" SENSOR_ATTR = "active_power"
_attr_device_class: SensorDeviceClass = SensorDeviceClass.POWER _attr_device_class: SensorDeviceClass = SensorDeviceClass.POWER
_attr_should_poll = True # BaseZhaEntity defaults to False
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT _attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_unit = POWER_WATT _unit = POWER_WATT
_div_mul_prefix = "ac_power" _div_mul_prefix = "ac_power"
@property
def should_poll(self) -> bool:
"""Return True if HA needs to poll for state changes."""
return True
@property @property
def extra_state_attributes(self) -> dict[str, Any]: def extra_state_attributes(self) -> dict[str, Any]:
"""Return device state attrs for sensor.""" """Return device state attrs for sensor."""
@ -312,14 +308,10 @@ class ElectricalMeasurementApparentPower(
SENSOR_ATTR = "apparent_power" SENSOR_ATTR = "apparent_power"
_attr_device_class: SensorDeviceClass = SensorDeviceClass.APPARENT_POWER _attr_device_class: SensorDeviceClass = SensorDeviceClass.APPARENT_POWER
_attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor
_unit = POWER_VOLT_AMPERE _unit = POWER_VOLT_AMPERE
_div_mul_prefix = "ac_power" _div_mul_prefix = "ac_power"
@property
def should_poll(self) -> bool:
"""Poll indirectly by ElectricalMeasurementSensor."""
return False
@MULTI_MATCH(channel_names=CHANNEL_ELECTRICAL_MEASUREMENT) @MULTI_MATCH(channel_names=CHANNEL_ELECTRICAL_MEASUREMENT)
class ElectricalMeasurementRMSCurrent(ElectricalMeasurement, id_suffix="rms_current"): class ElectricalMeasurementRMSCurrent(ElectricalMeasurement, id_suffix="rms_current"):
@ -327,14 +319,10 @@ class ElectricalMeasurementRMSCurrent(ElectricalMeasurement, id_suffix="rms_curr
SENSOR_ATTR = "rms_current" SENSOR_ATTR = "rms_current"
_attr_device_class: SensorDeviceClass = SensorDeviceClass.CURRENT _attr_device_class: SensorDeviceClass = SensorDeviceClass.CURRENT
_attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor
_unit = ELECTRIC_CURRENT_AMPERE _unit = ELECTRIC_CURRENT_AMPERE
_div_mul_prefix = "ac_current" _div_mul_prefix = "ac_current"
@property
def should_poll(self) -> bool:
"""Poll indirectly by ElectricalMeasurementSensor."""
return False
@MULTI_MATCH(channel_names=CHANNEL_ELECTRICAL_MEASUREMENT) @MULTI_MATCH(channel_names=CHANNEL_ELECTRICAL_MEASUREMENT)
class ElectricalMeasurementRMSVoltage(ElectricalMeasurement, id_suffix="rms_voltage"): class ElectricalMeasurementRMSVoltage(ElectricalMeasurement, id_suffix="rms_voltage"):
@ -342,14 +330,10 @@ class ElectricalMeasurementRMSVoltage(ElectricalMeasurement, id_suffix="rms_volt
SENSOR_ATTR = "rms_voltage" SENSOR_ATTR = "rms_voltage"
_attr_device_class: SensorDeviceClass = SensorDeviceClass.CURRENT _attr_device_class: SensorDeviceClass = SensorDeviceClass.CURRENT
_attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor
_unit = ELECTRIC_POTENTIAL_VOLT _unit = ELECTRIC_POTENTIAL_VOLT
_div_mul_prefix = "ac_voltage" _div_mul_prefix = "ac_voltage"
@property
def should_poll(self) -> bool:
"""Poll indirectly by ElectricalMeasurementSensor."""
return False
@MULTI_MATCH(channel_names=CHANNEL_ELECTRICAL_MEASUREMENT) @MULTI_MATCH(channel_names=CHANNEL_ELECTRICAL_MEASUREMENT)
class ElectricalMeasurementFrequency(ElectricalMeasurement, id_suffix="ac_frequency"): class ElectricalMeasurementFrequency(ElectricalMeasurement, id_suffix="ac_frequency"):
@ -357,14 +341,10 @@ class ElectricalMeasurementFrequency(ElectricalMeasurement, id_suffix="ac_freque
SENSOR_ATTR = "ac_frequency" SENSOR_ATTR = "ac_frequency"
_attr_device_class: SensorDeviceClass = SensorDeviceClass.FREQUENCY _attr_device_class: SensorDeviceClass = SensorDeviceClass.FREQUENCY
_attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor
_unit = FREQUENCY_HERTZ _unit = FREQUENCY_HERTZ
_div_mul_prefix = "ac_frequency" _div_mul_prefix = "ac_frequency"
@property
def should_poll(self) -> bool:
"""Poll indirectly by ElectricalMeasurementSensor."""
return False
@MULTI_MATCH(channel_names=CHANNEL_ELECTRICAL_MEASUREMENT) @MULTI_MATCH(channel_names=CHANNEL_ELECTRICAL_MEASUREMENT)
class ElectricalMeasurementPowerFactor(ElectricalMeasurement, id_suffix="power_factor"): class ElectricalMeasurementPowerFactor(ElectricalMeasurement, id_suffix="power_factor"):
@ -372,13 +352,9 @@ class ElectricalMeasurementPowerFactor(ElectricalMeasurement, id_suffix="power_f
SENSOR_ATTR = "power_factor" SENSOR_ATTR = "power_factor"
_attr_device_class: SensorDeviceClass = SensorDeviceClass.POWER_FACTOR _attr_device_class: SensorDeviceClass = SensorDeviceClass.POWER_FACTOR
_attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor
_unit = PERCENTAGE _unit = PERCENTAGE
@property
def should_poll(self) -> bool:
"""Poll indirectly by ElectricalMeasurementSensor."""
return False
@MULTI_MATCH( @MULTI_MATCH(
generic_ids=CHANNEL_ST_HUMIDITY_CLUSTER, stop_on_match_group=CHANNEL_HUMIDITY generic_ids=CHANNEL_ST_HUMIDITY_CLUSTER, stop_on_match_group=CHANNEL_HUMIDITY
@ -521,10 +497,7 @@ class SmartEnergySummation(SmartEnergyMetering, id_suffix="summation_delivered")
class PolledSmartEnergySummation(SmartEnergySummation): class PolledSmartEnergySummation(SmartEnergySummation):
"""Polled Smart Energy Metering summation sensor.""" """Polled Smart Energy Metering summation sensor."""
@property _attr_should_poll = True # BaseZhaEntity defaults to False
def should_poll(self) -> bool:
"""Poll the entity for current state."""
return True
async def async_update(self) -> None: async def async_update(self) -> None:
"""Retrieve latest state.""" """Retrieve latest state."""
@ -770,6 +743,7 @@ class RSSISensor(Sensor, id_suffix="rssi"):
_attr_device_class: SensorDeviceClass = SensorDeviceClass.SIGNAL_STRENGTH _attr_device_class: SensorDeviceClass = SensorDeviceClass.SIGNAL_STRENGTH
_attr_entity_category = EntityCategory.DIAGNOSTIC _attr_entity_category = EntityCategory.DIAGNOSTIC
_attr_entity_registry_enabled_default = False _attr_entity_registry_enabled_default = False
_attr_should_poll = True # BaseZhaEntity defaults to False
unique_id_suffix: str unique_id_suffix: str
@classmethod @classmethod
@ -794,11 +768,6 @@ class RSSISensor(Sensor, id_suffix="rssi"):
"""Return the state of the entity.""" """Return the state of the entity."""
return getattr(self._zha_device.device, self.unique_id_suffix) return getattr(self._zha_device.device, self.unique_id_suffix)
@property
def should_poll(self) -> bool:
"""Poll the entity for current state."""
return True
@MULTI_MATCH(channel_names=CHANNEL_BASIC) @MULTI_MATCH(channel_names=CHANNEL_BASIC)
class LQISensor(RSSISensor, id_suffix="lqi"): class LQISensor(RSSISensor, id_suffix="lqi"):