Use _attr_should_poll in zha entities (#77175)
parent
ce3502291d
commit
325557c3e9
|
@ -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):
|
||||
|
|
|
@ -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."""
|
||||
|
|
|
@ -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"):
|
||||
|
|
Loading…
Reference in New Issue