From 61d9067a49bdb1df8c5b7024f563b7ec480d65fe Mon Sep 17 00:00:00 2001 From: springstan <46536646+springstan@users.noreply.github.com> Date: Tue, 6 Oct 2020 18:08:53 +0200 Subject: [PATCH] Return empty dictionary at first for attributes (#41348) --- homeassistant/components/directv/media_player.py | 16 ++++++++-------- homeassistant/components/epson/media_player.py | 6 +++--- homeassistant/components/flo/binary_sensor.py | 14 +++++++------- .../components/garmin_connect/sensor.py | 14 +++++++------- .../components/geo_json_events/geo_location.py | 6 +++--- .../homekit_controller/alarm_control_panel.py | 6 +++--- .../components/homekit_controller/cover.py | 13 ++++++------- homeassistant/components/hue/light.py | 6 +++--- .../components/jewish_calendar/sensor.py | 7 +++---- .../manual_mqtt/alarm_control_panel.py | 12 ++++++------ homeassistant/components/maxcube/climate.py | 6 +++--- .../components/opnsense/device_tracker.py | 6 +++--- homeassistant/components/rflink/light.py | 12 ++++++------ .../components/sighthound/image_processing.py | 6 +++--- homeassistant/components/vesync/switch.py | 16 ++++++++-------- homeassistant/components/webostv/media_player.py | 6 +++--- 16 files changed, 75 insertions(+), 77 deletions(-) diff --git a/homeassistant/components/directv/media_player.py b/homeassistant/components/directv/media_player.py index 695aac9f96b..dfd88ca885b 100644 --- a/homeassistant/components/directv/media_player.py +++ b/homeassistant/components/directv/media_player.py @@ -126,14 +126,14 @@ class DIRECTVMediaPlayer(DIRECTVEntity, MediaPlayerEntity): @property def device_state_attributes(self): """Return device specific state attributes.""" - if not self._is_standby: - return { - ATTR_MEDIA_CURRENTLY_RECORDING: self.media_currently_recording, - ATTR_MEDIA_RATING: self.media_rating, - ATTR_MEDIA_RECORDED: self.media_recorded, - ATTR_MEDIA_START_TIME: self.media_start_time, - } - return {} + if self._is_standby: + return {} + return { + ATTR_MEDIA_CURRENTLY_RECORDING: self.media_currently_recording, + ATTR_MEDIA_RATING: self.media_rating, + ATTR_MEDIA_RECORDED: self.media_recorded, + ATTR_MEDIA_START_TIME: self.media_start_time, + } @property def name(self): diff --git a/homeassistant/components/epson/media_player.py b/homeassistant/components/epson/media_player.py index c701968b9ef..235caa8d1a3 100644 --- a/homeassistant/components/epson/media_player.py +++ b/homeassistant/components/epson/media_player.py @@ -235,6 +235,6 @@ class EpsonProjector(MediaPlayerEntity): @property def device_state_attributes(self): """Return device specific state attributes.""" - if self._cmode is not None: - return {ATTR_CMODE: self._cmode} - return {} + if self._cmode is None: + return {} + return {ATTR_CMODE: self._cmode} diff --git a/homeassistant/components/flo/binary_sensor.py b/homeassistant/components/flo/binary_sensor.py index 0f3a88f9446..a8bac498674 100644 --- a/homeassistant/components/flo/binary_sensor.py +++ b/homeassistant/components/flo/binary_sensor.py @@ -36,13 +36,13 @@ class FloPendingAlertsBinarySensor(FloEntity, BinarySensorEntity): @property def device_state_attributes(self): """Return the state attributes.""" - if self._device.has_alerts: - return { - "info": self._device.pending_info_alerts_count, - "warning": self._device.pending_warning_alerts_count, - "critical": self._device.pending_critical_alerts_count, - } - return {} + if not self._device.has_alerts: + return {} + return { + "info": self._device.pending_info_alerts_count, + "warning": self._device.pending_warning_alerts_count, + "critical": self._device.pending_critical_alerts_count, + } @property def device_class(self): diff --git a/homeassistant/components/garmin_connect/sensor.py b/homeassistant/components/garmin_connect/sensor.py index 19b81a3ba1d..9b678011053 100644 --- a/homeassistant/components/garmin_connect/sensor.py +++ b/homeassistant/components/garmin_connect/sensor.py @@ -121,13 +121,13 @@ class GarminConnectSensor(Entity): @property def device_state_attributes(self): """Return attributes for sensor.""" - if self._data.data: - return { - "source": self._data.data["source"], - "last_synced": self._data.data["lastSyncTimestampGMT"], - ATTR_ATTRIBUTION: ATTRIBUTION, - } - return {} + if not self._data.data: + return {} + return { + "source": self._data.data["source"], + "last_synced": self._data.data["lastSyncTimestampGMT"], + ATTR_ATTRIBUTION: ATTRIBUTION, + } @property def device_info(self) -> Dict[str, Any]: diff --git a/homeassistant/components/geo_json_events/geo_location.py b/homeassistant/components/geo_json_events/geo_location.py index a36295e9a4f..bb2d86539e9 100644 --- a/homeassistant/components/geo_json_events/geo_location.py +++ b/homeassistant/components/geo_json_events/geo_location.py @@ -203,6 +203,6 @@ class GeoJsonLocationEvent(GeolocationEvent): @property def device_state_attributes(self): """Return the device state attributes.""" - if self._external_id: - return {ATTR_EXTERNAL_ID: self._external_id} - return {} + if not self._external_id: + return {} + return {ATTR_EXTERNAL_ID: self._external_id} diff --git a/homeassistant/components/homekit_controller/alarm_control_panel.py b/homeassistant/components/homekit_controller/alarm_control_panel.py index 80ba73a0517..333eb462439 100644 --- a/homeassistant/components/homekit_controller/alarm_control_panel.py +++ b/homeassistant/components/homekit_controller/alarm_control_panel.py @@ -112,6 +112,6 @@ class HomeKitAlarmControlPanelEntity(HomeKitEntity, AlarmControlPanelEntity): """Return the optional state attributes.""" battery_level = self.service.value(CharacteristicsTypes.BATTERY_LEVEL) - if battery_level: - return {ATTR_BATTERY_LEVEL: battery_level} - return {} + if not battery_level: + return {} + return {ATTR_BATTERY_LEVEL: battery_level} diff --git a/homeassistant/components/homekit_controller/cover.py b/homeassistant/components/homekit_controller/cover.py index ab03ca0c546..5f43b3e8564 100644 --- a/homeassistant/components/homekit_controller/cover.py +++ b/homeassistant/components/homekit_controller/cover.py @@ -120,10 +120,9 @@ class HomeKitGarageDoorCover(HomeKitEntity, CoverEntity): obstruction_detected = self.service.value( CharacteristicsTypes.OBSTRUCTION_DETECTED ) - if obstruction_detected: - return {"obstruction-detected": obstruction_detected} - - return {} + if not obstruction_detected: + return {} + return {"obstruction-detected": obstruction_detected} class HomeKitWindowCover(HomeKitEntity, CoverEntity): @@ -250,6 +249,6 @@ class HomeKitWindowCover(HomeKitEntity, CoverEntity): obstruction_detected = self.service.value( CharacteristicsTypes.OBSTRUCTION_DETECTED ) - if obstruction_detected: - return {"obstruction-detected": obstruction_detected} - return {} + if not obstruction_detected: + return {} + return {"obstruction-detected": obstruction_detected} diff --git a/homeassistant/components/hue/light.py b/homeassistant/components/hue/light.py index 2980b9ba6f5..821d482ec25 100644 --- a/homeassistant/components/hue/light.py +++ b/homeassistant/components/hue/light.py @@ -458,6 +458,6 @@ class HueLight(CoordinatorEntity, LightEntity): @property def device_state_attributes(self): """Return the device state attributes.""" - if self.is_group: - return {ATTR_IS_HUE_GROUP: self.is_group} - return {} + if not self.is_group: + return {} + return {ATTR_IS_HUE_GROUP: self.is_group} diff --git a/homeassistant/components/jewish_calendar/sensor.py b/homeassistant/components/jewish_calendar/sensor.py index 606f4fffab1..7b3a3af9c11 100644 --- a/homeassistant/components/jewish_calendar/sensor.py +++ b/homeassistant/components/jewish_calendar/sensor.py @@ -113,10 +113,9 @@ class JewishCalendarSensor(Entity): @property def device_state_attributes(self): """Return the state attributes.""" - if self._type == "holiday": - return self._holiday_attrs - - return {} + if self._type != "holiday": + return {} + return self._holiday_attrs def get_state(self, daytime_date, after_shkia_date, after_tzais_date): """For a given type of sensor, return the state.""" diff --git a/homeassistant/components/manual_mqtt/alarm_control_panel.py b/homeassistant/components/manual_mqtt/alarm_control_panel.py index 95442545101..10c2a473a29 100644 --- a/homeassistant/components/manual_mqtt/alarm_control_panel.py +++ b/homeassistant/components/manual_mqtt/alarm_control_panel.py @@ -415,12 +415,12 @@ class ManualMQTTAlarm(alarm.AlarmControlPanelEntity): @property def device_state_attributes(self): """Return the state attributes.""" - if self.state == STATE_ALARM_PENDING: - return { - ATTR_PRE_PENDING_STATE: self._previous_state, - ATTR_POST_PENDING_STATE: self._state, - } - return {} + if self.state != STATE_ALARM_PENDING: + return {} + return { + ATTR_PRE_PENDING_STATE: self._previous_state, + ATTR_POST_PENDING_STATE: self._state, + } async def async_added_to_hass(self): """Subscribe to MQTT events.""" diff --git a/homeassistant/components/maxcube/climate.py b/homeassistant/components/maxcube/climate.py index 5853da070dd..e222784ca57 100644 --- a/homeassistant/components/maxcube/climate.py +++ b/homeassistant/components/maxcube/climate.py @@ -287,9 +287,9 @@ class MaxCubeClimate(ClimateEntity): cube = self._cubehandle.cube device = cube.device_by_rf(self._rf_address) - if cube.is_thermostat(device): - return {ATTR_VALVE_POSITION: device.valve_position} - return {} + if not cube.is_thermostat(device): + return {} + return {ATTR_VALVE_POSITION: device.valve_position} def update(self): """Get latest data from MAX! Cube.""" diff --git a/homeassistant/components/opnsense/device_tracker.py b/homeassistant/components/opnsense/device_tracker.py index c64e0b0679a..f7743d04a6d 100644 --- a/homeassistant/components/opnsense/device_tracker.py +++ b/homeassistant/components/opnsense/device_tracker.py @@ -61,6 +61,6 @@ class OPNSenseDeviceScanner(DeviceScanner): if device not in self.last_results: return None mfg = self.last_results[device].get("manufacturer") - if mfg: - return {"manufacturer": mfg} - return {} + if not mfg: + return {} + return {"manufacturer": mfg} diff --git a/homeassistant/components/rflink/light.py b/homeassistant/components/rflink/light.py index 96c8f267a50..6d63e12378d 100644 --- a/homeassistant/components/rflink/light.py +++ b/homeassistant/components/rflink/light.py @@ -197,9 +197,9 @@ class DimmableRflinkLight(SwitchableRflinkDevice, LightEntity): @property def device_state_attributes(self): """Return the device state attributes.""" - if self._brightness is not None: - return {ATTR_BRIGHTNESS: self._brightness} - return {} + if self._brightness is None: + return {} + return {ATTR_BRIGHTNESS: self._brightness} @property def supported_features(self): @@ -259,9 +259,9 @@ class HybridRflinkLight(SwitchableRflinkDevice, LightEntity): @property def device_state_attributes(self): """Return the device state attributes.""" - if self._brightness is not None: - return {ATTR_BRIGHTNESS: self._brightness} - return {} + if self._brightness is None: + return {} + return {ATTR_BRIGHTNESS: self._brightness} @property def supported_features(self): diff --git a/homeassistant/components/sighthound/image_processing.py b/homeassistant/components/sighthound/image_processing.py index f5ed187fb4c..e15fab1aaa3 100644 --- a/homeassistant/components/sighthound/image_processing.py +++ b/homeassistant/components/sighthound/image_processing.py @@ -172,6 +172,6 @@ class SighthoundEntity(ImageProcessingEntity): @property def device_state_attributes(self): """Return the attributes.""" - if self._last_detection: - return {"last_person": self._last_detection} - return {} + if not self._last_detection: + return {} + return {"last_person": self._last_detection} diff --git a/homeassistant/components/vesync/switch.py b/homeassistant/components/vesync/switch.py index db123673851..0ce4b931def 100644 --- a/homeassistant/components/vesync/switch.py +++ b/homeassistant/components/vesync/switch.py @@ -74,14 +74,14 @@ class VeSyncSwitchHA(VeSyncBaseSwitch, SwitchEntity): @property def device_state_attributes(self): """Return the state attributes of the device.""" - if hasattr(self.smartplug, "weekly_energy_total"): - return { - "voltage": self.smartplug.voltage, - "weekly_energy_total": self.smartplug.weekly_energy_total, - "monthly_energy_total": self.smartplug.monthly_energy_total, - "yearly_energy_total": self.smartplug.yearly_energy_total, - } - return {} + if not hasattr(self.smartplug, "weekly_energy_total"): + return {} + return { + "voltage": self.smartplug.voltage, + "weekly_energy_total": self.smartplug.weekly_energy_total, + "monthly_energy_total": self.smartplug.monthly_energy_total, + "yearly_energy_total": self.smartplug.yearly_energy_total, + } @property def current_power_w(self): diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index db40ad2979b..82950e4f7f8 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -320,9 +320,9 @@ class LgWebOSMediaPlayerEntity(MediaPlayerEntity): @property def device_state_attributes(self): """Return device specific state attributes.""" - if self._client.sound_output is not None and self.state != STATE_OFF: - return {ATTR_SOUND_OUTPUT: self._client.sound_output} - return {} + if self._client.sound_output is None and self.state == STATE_OFF: + return {} + return {ATTR_SOUND_OUTPUT: self._client.sound_output} @cmd async def async_turn_off(self):