Return attribute dict directly without temporary variable v2 (#41271)

This commit is contained in:
springstan 2020-10-06 16:55:16 +02:00 committed by GitHub
parent 33e69fe4bf
commit 513f03eb1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 90 additions and 124 deletions

View File

@ -209,10 +209,7 @@ class DeconzBaseLight(DeconzDevice, LightEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the device state attributes.""" """Return the device state attributes."""
attributes = {} return {"is_deconz_group": self._device.type == "LightGroup"}
attributes["is_deconz_group"] = self._device.type == "LightGroup"
return attributes
class DeconzLight(DeconzBaseLight): class DeconzLight(DeconzBaseLight):

View File

@ -308,14 +308,13 @@ class DenonDevice(MediaPlayerEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return device specific state attributes.""" """Return device specific state attributes."""
attributes = {}
if ( if (
self._sound_mode_raw is not None self._sound_mode_raw is not None
and self._sound_mode_support and self._sound_mode_support
and self._power == "ON" and self._power == "ON"
): ):
attributes[ATTR_SOUND_MODE_RAW] = self._sound_mode_raw return {ATTR_SOUND_MODE_RAW: self._sound_mode_raw}
return attributes return {}
def media_play_pause(self): def media_play_pause(self):
"""Play or pause the media player.""" """Play or pause the media player."""

View File

@ -213,8 +213,7 @@ class DerivativeSensor(RestoreEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""
state_attr = {ATTR_SOURCE_ID: self._sensor_source_id} return {ATTR_SOURCE_ID: self._sensor_source_id}
return state_attr
@property @property
def icon(self): def icon(self):

View File

@ -126,14 +126,14 @@ class DIRECTVMediaPlayer(DIRECTVEntity, MediaPlayerEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return device specific state attributes.""" """Return device specific state attributes."""
attributes = {}
if not self._is_standby: if not self._is_standby:
attributes[ATTR_MEDIA_CURRENTLY_RECORDING] = self.media_currently_recording return {
attributes[ATTR_MEDIA_RATING] = self.media_rating ATTR_MEDIA_CURRENTLY_RECORDING: self.media_currently_recording,
attributes[ATTR_MEDIA_RECORDED] = self.media_recorded ATTR_MEDIA_RATING: self.media_rating,
attributes[ATTR_MEDIA_START_TIME] = self.media_start_time ATTR_MEDIA_RECORDED: self.media_recorded,
ATTR_MEDIA_START_TIME: self.media_start_time,
return attributes }
return {}
@property @property
def name(self): def name(self):

View File

@ -112,11 +112,11 @@ class EightHeatSensor(EightSleepHeatEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return device state attributes.""" """Return device state attributes."""
state_attr = {ATTR_TARGET_HEAT: self._usrobj.target_heating_level} return {
state_attr[ATTR_ACTIVE_HEAT] = self._usrobj.now_heating ATTR_TARGET_HEAT: self._usrobj.target_heating_level,
state_attr[ATTR_DURATION_HEAT] = self._usrobj.heating_remaining ATTR_ACTIVE_HEAT: self._usrobj.now_heating,
ATTR_DURATION_HEAT: self._usrobj.heating_remaining,
return state_attr }
class EightUserSensor(EightSleepUserEntity): class EightUserSensor(EightSleepUserEntity):

View File

@ -253,17 +253,13 @@ class Enigma2Device(MediaPlayerEntity):
currservice_begin: is in the format '21:00'. currservice_begin: is in the format '21:00'.
currservice_end: is in the format '21:00'. currservice_end: is in the format '21:00'.
""" """
attributes = {} if self.e2_box.in_standby:
if not self.e2_box.in_standby: return {}
attributes[ATTR_MEDIA_CURRENTLY_RECORDING] = self.e2_box.status_info[ return {
"isRecording" ATTR_MEDIA_CURRENTLY_RECORDING: self.e2_box.status_info["isRecording"],
] ATTR_MEDIA_DESCRIPTION: self.e2_box.status_info[
attributes[ATTR_MEDIA_DESCRIPTION] = self.e2_box.status_info[
"currservice_fulldescription" "currservice_fulldescription"
] ],
attributes[ATTR_MEDIA_START_TIME] = self.e2_box.status_info[ ATTR_MEDIA_START_TIME: self.e2_box.status_info["currservice_begin"],
"currservice_begin" ATTR_MEDIA_END_TIME: self.e2_box.status_info["currservice_end"],
] }
attributes[ATTR_MEDIA_END_TIME] = self.e2_box.status_info["currservice_end"]
return attributes

View File

@ -235,7 +235,6 @@ class EpsonProjector(MediaPlayerEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return device specific state attributes.""" """Return device specific state attributes."""
attributes = {}
if self._cmode is not None: if self._cmode is not None:
attributes[ATTR_CMODE] = self._cmode return {ATTR_CMODE: self._cmode}
return attributes return {}

View File

@ -319,8 +319,7 @@ class SensorFilter(Entity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""
state_attr = {ATTR_ENTITY_ID: self._entity} return {ATTR_ENTITY_ID: self._entity}
return state_attr
class FilterState: class FilterState:

View File

@ -36,12 +36,13 @@ class FloPendingAlertsBinarySensor(FloEntity, BinarySensorEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
attr = {}
if self._device.has_alerts: if self._device.has_alerts:
attr["info"] = self._device.pending_info_alerts_count return {
attr["warning"] = self._device.pending_warning_alerts_count "info": self._device.pending_info_alerts_count,
attr["critical"] = self._device.pending_critical_alerts_count "warning": self._device.pending_warning_alerts_count,
return attr "critical": self._device.pending_critical_alerts_count,
}
return {}
@property @property
def device_class(self): def device_class(self):

View File

@ -121,14 +121,13 @@ class GarminConnectSensor(Entity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return attributes for sensor.""" """Return attributes for sensor."""
attributes = {}
if self._data.data: if self._data.data:
attributes = { return {
"source": self._data.data["source"], "source": self._data.data["source"],
"last_synced": self._data.data["lastSyncTimestampGMT"], "last_synced": self._data.data["lastSyncTimestampGMT"],
ATTR_ATTRIBUTION: ATTRIBUTION, ATTR_ATTRIBUTION: ATTRIBUTION,
} }
return attributes return {}
@property @property
def device_info(self) -> Dict[str, Any]: def device_info(self) -> Dict[str, Any]:

View File

@ -203,7 +203,6 @@ class GeoJsonLocationEvent(GeolocationEvent):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the device state attributes.""" """Return the device state attributes."""
attributes = {}
if self._external_id: if self._external_id:
attributes[ATTR_EXTERNAL_ID] = self._external_id return {ATTR_EXTERNAL_ID: self._external_id}
return attributes return {}

View File

@ -110,10 +110,8 @@ class HomeKitAlarmControlPanelEntity(HomeKitEntity, AlarmControlPanelEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the optional state attributes.""" """Return the optional state attributes."""
attributes = {}
battery_level = self.service.value(CharacteristicsTypes.BATTERY_LEVEL) battery_level = self.service.value(CharacteristicsTypes.BATTERY_LEVEL)
if battery_level:
attributes[ATTR_BATTERY_LEVEL] = battery_level
return attributes if battery_level:
return {ATTR_BATTERY_LEVEL: battery_level}
return {}

View File

@ -117,15 +117,13 @@ class HomeKitGarageDoorCover(HomeKitEntity, CoverEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the optional state attributes.""" """Return the optional state attributes."""
attributes = {}
obstruction_detected = self.service.value( obstruction_detected = self.service.value(
CharacteristicsTypes.OBSTRUCTION_DETECTED CharacteristicsTypes.OBSTRUCTION_DETECTED
) )
if obstruction_detected: if obstruction_detected:
attributes["obstruction-detected"] = obstruction_detected return {"obstruction-detected": obstruction_detected}
return attributes return {}
class HomeKitWindowCover(HomeKitEntity, CoverEntity): class HomeKitWindowCover(HomeKitEntity, CoverEntity):
@ -249,12 +247,9 @@ class HomeKitWindowCover(HomeKitEntity, CoverEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the optional state attributes.""" """Return the optional state attributes."""
attributes = {}
obstruction_detected = self.service.value( obstruction_detected = self.service.value(
CharacteristicsTypes.OBSTRUCTION_DETECTED CharacteristicsTypes.OBSTRUCTION_DETECTED
) )
if obstruction_detected: if obstruction_detected:
attributes["obstruction-detected"] = obstruction_detected return {"obstruction-detected": obstruction_detected}
return {}
return attributes

View File

@ -458,7 +458,6 @@ class HueLight(CoordinatorEntity, LightEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the device state attributes.""" """Return the device state attributes."""
attributes = {}
if self.is_group: if self.is_group:
attributes[ATTR_IS_HUE_GROUP] = self.is_group return {ATTR_IS_HUE_GROUP: self.is_group}
return attributes return {}

View File

@ -203,8 +203,7 @@ class IntegrationSensor(RestoreEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""
state_attr = {ATTR_SOURCE_ID: self._sensor_source_id} return {ATTR_SOURCE_ID: self._sensor_source_id}
return state_attr
@property @property
def icon(self): def icon(self):

View File

@ -394,13 +394,12 @@ class ManualAlarm(alarm.AlarmControlPanelEntity, RestoreEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
state_attr = {}
if self.state == STATE_ALARM_PENDING or self.state == STATE_ALARM_ARMING: if self.state == STATE_ALARM_PENDING or self.state == STATE_ALARM_ARMING:
state_attr[ATTR_PREVIOUS_STATE] = self._previous_state return {
state_attr[ATTR_NEXT_STATE] = self._state ATTR_PREVIOUS_STATE: self._previous_state,
ATTR_NEXT_STATE: self._state,
return state_attr }
return {}
@callback @callback
def async_scheduled_update(self, now): def async_scheduled_update(self, now):

View File

@ -415,13 +415,12 @@ class ManualMQTTAlarm(alarm.AlarmControlPanelEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
state_attr = {}
if self.state == STATE_ALARM_PENDING: if self.state == STATE_ALARM_PENDING:
state_attr[ATTR_PRE_PENDING_STATE] = self._previous_state return {
state_attr[ATTR_POST_PENDING_STATE] = self._state ATTR_PRE_PENDING_STATE: self._previous_state,
ATTR_POST_PENDING_STATE: self._state,
return state_attr }
return {}
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Subscribe to MQTT events.""" """Subscribe to MQTT events."""

View File

@ -286,12 +286,10 @@ class MaxCubeClimate(ClimateEntity):
"""Return the optional state attributes.""" """Return the optional state attributes."""
cube = self._cubehandle.cube cube = self._cubehandle.cube
device = cube.device_by_rf(self._rf_address) device = cube.device_by_rf(self._rf_address)
attributes = {}
if cube.is_thermostat(device): if cube.is_thermostat(device):
attributes[ATTR_VALVE_POSITION] = device.valve_position return {ATTR_VALVE_POSITION: device.valve_position}
return {}
return attributes
def update(self): def update(self):
"""Get latest data from MAX! Cube.""" """Get latest data from MAX! Cube."""

View File

@ -228,12 +228,11 @@ class MinMaxSensor(Entity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""
state_attr = { return {
attr: getattr(self, attr) attr: getattr(self, attr)
for attr in ATTR_TO_PROPERTY for attr in ATTR_TO_PROPERTY
if getattr(self, attr) is not None if getattr(self, attr) is not None
} }
return state_attr
@property @property
def icon(self): def icon(self):

View File

@ -197,10 +197,9 @@ class DimmableRflinkLight(SwitchableRflinkDevice, LightEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the device state attributes.""" """Return the device state attributes."""
attr = {}
if self._brightness is not None: if self._brightness is not None:
attr[ATTR_BRIGHTNESS] = self._brightness return {ATTR_BRIGHTNESS: self._brightness}
return attr return {}
@property @property
def supported_features(self): def supported_features(self):
@ -260,10 +259,9 @@ class HybridRflinkLight(SwitchableRflinkDevice, LightEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the device state attributes.""" """Return the device state attributes."""
attr = {}
if self._brightness is not None: if self._brightness is not None:
attr[ATTR_BRIGHTNESS] = self._brightness return {ATTR_BRIGHTNESS: self._brightness}
return attr return {}
@property @property
def supported_features(self): def supported_features(self):

View File

@ -88,8 +88,8 @@ class SesameDevice(LockEntity):
@property @property
def device_state_attributes(self) -> dict: def device_state_attributes(self) -> dict:
"""Return the state attributes.""" """Return the state attributes."""
attributes = {} return {
attributes[ATTR_DEVICE_ID] = self._device_id ATTR_DEVICE_ID: self._device_id,
attributes[ATTR_SERIAL_NO] = self._serial ATTR_SERIAL_NO: self._serial,
attributes[ATTR_BATTERY_LEVEL] = self._battery ATTR_BATTERY_LEVEL: self._battery,
return attributes }

View File

@ -172,7 +172,6 @@ class SighthoundEntity(ImageProcessingEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the attributes.""" """Return the attributes."""
attr = {}
if self._last_detection: if self._last_detection:
attr["last_person"] = self._last_detection return {"last_person": self._last_detection}
return attr return {}

View File

@ -262,14 +262,13 @@ class TodoistProjectDevice(CalendarEventDevice):
# No tasks, we don't REALLY need to show anything. # No tasks, we don't REALLY need to show anything.
return None return None
attributes = {} return {
attributes[DUE_TODAY] = self.data.event[DUE_TODAY] DUE_TODAY: self.data.event[DUE_TODAY],
attributes[OVERDUE] = self.data.event[OVERDUE] OVERDUE: self.data.event[OVERDUE],
attributes[ALL_TASKS] = self._cal_data[ALL_TASKS] ALL_TASKS: self._cal_data[ALL_TASKS],
attributes[PRIORITY] = self.data.event[PRIORITY] PRIORITY: self.data.event[PRIORITY],
attributes[LABELS] = self.data.event[LABELS] LABELS: self.data.event[LABELS],
}
return attributes
class TodoistProjectData: class TodoistProjectData:

View File

@ -233,9 +233,7 @@ class UniFiClientTracker(UniFiClient, ScannerEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the client state attributes.""" """Return the client state attributes."""
attributes = {} attributes = {"is_wired": self.is_wired}
attributes["is_wired"] = self.is_wired
if self.is_connected: if self.is_connected:
for variable in CLIENT_CONNECTED_ATTRIBUTES: for variable in CLIENT_CONNECTED_ATTRIBUTES:

View File

@ -74,13 +74,14 @@ class VeSyncSwitchHA(VeSyncBaseSwitch, SwitchEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the device.""" """Return the state attributes of the device."""
attr = {}
if hasattr(self.smartplug, "weekly_energy_total"): if hasattr(self.smartplug, "weekly_energy_total"):
attr["voltage"] = self.smartplug.voltage return {
attr["weekly_energy_total"] = self.smartplug.weekly_energy_total "voltage": self.smartplug.voltage,
attr["monthly_energy_total"] = self.smartplug.monthly_energy_total "weekly_energy_total": self.smartplug.weekly_energy_total,
attr["yearly_energy_total"] = self.smartplug.yearly_energy_total "monthly_energy_total": self.smartplug.monthly_energy_total,
return attr "yearly_energy_total": self.smartplug.yearly_energy_total,
}
return {}
@property @property
def current_power_w(self): def current_power_w(self):

View File

@ -320,10 +320,9 @@ class LgWebOSMediaPlayerEntity(MediaPlayerEntity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return device specific state attributes.""" """Return device specific state attributes."""
attributes = {}
if self._client.sound_output is not None and self.state != STATE_OFF: if self._client.sound_output is not None and self.state != STATE_OFF:
attributes[ATTR_SOUND_OUTPUT] = self._client.sound_output return {ATTR_SOUND_OUTPUT: self._client.sound_output}
return attributes return {}
@cmd @cmd
async def async_turn_off(self): async def async_turn_off(self):

View File

@ -106,9 +106,7 @@ class XboxSensor(Entity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
attributes = {} attributes = {"gamerscore": self._gamerscore, "tier": self._tier}
attributes["gamerscore"] = self._gamerscore
attributes["tier"] = self._tier
for device in self._presence: for device in self._presence:
for title in device["titles"]: for title in device["titles"]: