From 325557c3e9157c1ac9e56e9172721eac44f0a16b Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 23 Aug 2022 03:38:26 +0200 Subject: [PATCH] Use _attr_should_poll in zha entities (#77175) --- .../components/zha/device_tracker.py | 3 +- homeassistant/components/zha/entity.py | 7 +-- homeassistant/components/zha/sensor.py | 47 ++++--------------- 3 files changed, 11 insertions(+), 46 deletions(-) diff --git a/homeassistant/components/zha/device_tracker.py b/homeassistant/components/zha/device_tracker.py index eed80a05055..5a20273d085 100644 --- a/homeassistant/components/zha/device_tracker.py +++ b/homeassistant/components/zha/device_tracker.py @@ -49,13 +49,14 @@ async def async_setup_entry( class ZHADeviceScannerEntity(ScannerEntity, ZhaEntity): """Represent a tracked device.""" + _attr_should_poll = True # BaseZhaEntity defaults to False + def __init__(self, unique_id, zha_device, channels, **kwargs): """Initialize the ZHA device tracker.""" super().__init__(unique_id, zha_device, channels, **kwargs) self._battery_channel = self.cluster_channels.get(CHANNEL_POWER_CONFIGURATION) self._connected = False self._keepalive_interval = 60 - self._should_poll = True self._battery_level = None async def async_added_to_hass(self): diff --git a/homeassistant/components/zha/entity.py b/homeassistant/components/zha/entity.py index 2f609555c79..61dbe5339d0 100644 --- a/homeassistant/components/zha/entity.py +++ b/homeassistant/components/zha/entity.py @@ -49,12 +49,12 @@ class BaseZhaEntity(LogMixin, entity.Entity): unique_id_suffix: str | None = None _attr_has_entity_name = True + _attr_should_poll = False def __init__(self, unique_id: str, zha_device: ZHADevice, **kwargs: Any) -> None: """Init ZHA entity.""" self._name: str = "" self._force_update: bool = False - self._should_poll: bool = False self._unique_id: str = unique_id if 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.""" return self._force_update - @property - def should_poll(self) -> bool: - """Poll state from device.""" - return self._should_poll - @property def device_info(self) -> entity.DeviceInfo: """Return a device description for device registry.""" diff --git a/homeassistant/components/zha/sensor.py b/homeassistant/components/zha/sensor.py index e0f5bb958cd..61fad097fc8 100644 --- a/homeassistant/components/zha/sensor.py +++ b/homeassistant/components/zha/sensor.py @@ -266,15 +266,11 @@ class ElectricalMeasurement(Sensor): SENSOR_ATTR = "active_power" _attr_device_class: SensorDeviceClass = SensorDeviceClass.POWER + _attr_should_poll = True # BaseZhaEntity defaults to False _attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT _unit = POWER_WATT _div_mul_prefix = "ac_power" - @property - def should_poll(self) -> bool: - """Return True if HA needs to poll for state changes.""" - return True - @property def extra_state_attributes(self) -> dict[str, Any]: """Return device state attrs for sensor.""" @@ -312,14 +308,10 @@ class ElectricalMeasurementApparentPower( SENSOR_ATTR = "apparent_power" _attr_device_class: SensorDeviceClass = SensorDeviceClass.APPARENT_POWER + _attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor _unit = POWER_VOLT_AMPERE _div_mul_prefix = "ac_power" - @property - def should_poll(self) -> bool: - """Poll indirectly by ElectricalMeasurementSensor.""" - return False - @MULTI_MATCH(channel_names=CHANNEL_ELECTRICAL_MEASUREMENT) class ElectricalMeasurementRMSCurrent(ElectricalMeasurement, id_suffix="rms_current"): @@ -327,14 +319,10 @@ class ElectricalMeasurementRMSCurrent(ElectricalMeasurement, id_suffix="rms_curr SENSOR_ATTR = "rms_current" _attr_device_class: SensorDeviceClass = SensorDeviceClass.CURRENT + _attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor _unit = ELECTRIC_CURRENT_AMPERE _div_mul_prefix = "ac_current" - @property - def should_poll(self) -> bool: - """Poll indirectly by ElectricalMeasurementSensor.""" - return False - @MULTI_MATCH(channel_names=CHANNEL_ELECTRICAL_MEASUREMENT) class ElectricalMeasurementRMSVoltage(ElectricalMeasurement, id_suffix="rms_voltage"): @@ -342,14 +330,10 @@ class ElectricalMeasurementRMSVoltage(ElectricalMeasurement, id_suffix="rms_volt SENSOR_ATTR = "rms_voltage" _attr_device_class: SensorDeviceClass = SensorDeviceClass.CURRENT + _attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor _unit = ELECTRIC_POTENTIAL_VOLT _div_mul_prefix = "ac_voltage" - @property - def should_poll(self) -> bool: - """Poll indirectly by ElectricalMeasurementSensor.""" - return False - @MULTI_MATCH(channel_names=CHANNEL_ELECTRICAL_MEASUREMENT) class ElectricalMeasurementFrequency(ElectricalMeasurement, id_suffix="ac_frequency"): @@ -357,14 +341,10 @@ class ElectricalMeasurementFrequency(ElectricalMeasurement, id_suffix="ac_freque SENSOR_ATTR = "ac_frequency" _attr_device_class: SensorDeviceClass = SensorDeviceClass.FREQUENCY + _attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor _unit = FREQUENCY_HERTZ _div_mul_prefix = "ac_frequency" - @property - def should_poll(self) -> bool: - """Poll indirectly by ElectricalMeasurementSensor.""" - return False - @MULTI_MATCH(channel_names=CHANNEL_ELECTRICAL_MEASUREMENT) class ElectricalMeasurementPowerFactor(ElectricalMeasurement, id_suffix="power_factor"): @@ -372,13 +352,9 @@ class ElectricalMeasurementPowerFactor(ElectricalMeasurement, id_suffix="power_f SENSOR_ATTR = "power_factor" _attr_device_class: SensorDeviceClass = SensorDeviceClass.POWER_FACTOR + _attr_should_poll = False # Poll indirectly by ElectricalMeasurementSensor _unit = PERCENTAGE - @property - def should_poll(self) -> bool: - """Poll indirectly by ElectricalMeasurementSensor.""" - return False - @MULTI_MATCH( 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): """Polled Smart Energy Metering summation sensor.""" - @property - def should_poll(self) -> bool: - """Poll the entity for current state.""" - return True + _attr_should_poll = True # BaseZhaEntity defaults to False async def async_update(self) -> None: """Retrieve latest state.""" @@ -770,6 +743,7 @@ class RSSISensor(Sensor, id_suffix="rssi"): _attr_device_class: SensorDeviceClass = SensorDeviceClass.SIGNAL_STRENGTH _attr_entity_category = EntityCategory.DIAGNOSTIC _attr_entity_registry_enabled_default = False + _attr_should_poll = True # BaseZhaEntity defaults to False unique_id_suffix: str @classmethod @@ -794,11 +768,6 @@ class RSSISensor(Sensor, id_suffix="rssi"): """Return the state of the entity.""" 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) class LQISensor(RSSISensor, id_suffix="lqi"):