From 413c3afa124a5a92a3207157545198518b694d43 Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Sat, 3 Jul 2021 11:16:55 -0500 Subject: [PATCH] Remove redundant property definitions in SimpliSafe (#52458) * Remove redundant property definitions in SimpliSafe * Remove useless init --- .../components/simplisafe/__init__.py | 59 ++++++---------- .../simplisafe/alarm_control_panel.py | 67 +++++++------------ .../components/simplisafe/binary_sensor.py | 34 ++-------- homeassistant/components/simplisafe/lock.py | 15 ++--- homeassistant/components/simplisafe/sensor.py | 28 +------- 5 files changed, 58 insertions(+), 145 deletions(-) diff --git a/homeassistant/components/simplisafe/__init__.py b/homeassistant/components/simplisafe/__init__.py index ce30b95f82a..fce1890b280 100644 --- a/homeassistant/components/simplisafe/__init__.py +++ b/homeassistant/components/simplisafe/__init__.py @@ -403,25 +403,25 @@ class SimpliSafeEntity(CoordinatorEntity): def __init__(self, simplisafe, system, name, *, serial=None): """Initialize.""" super().__init__(simplisafe.coordinator) - self._name = name - self._online = True - self._simplisafe = simplisafe - self._system = system if serial: self._serial = serial else: self._serial = system.serial - self._attrs = {ATTR_SYSTEM_ID: system.system_id} - - self._device_info = { + self._attr_extra_state_attributes = {ATTR_SYSTEM_ID: system.system_id} + self._attr_device_info = { "identifiers": {(DOMAIN, system.system_id)}, "manufacturer": "SimpliSafe", "model": system.version, "name": name, "via_device": (DOMAIN, system.serial), } + self._attr_name = f"{system.address} {name}" + self._attr_unique_id = self._serial + self._online = True + self._simplisafe = simplisafe + self._system = system @property def available(self): @@ -431,27 +431,11 @@ class SimpliSafeEntity(CoordinatorEntity): # the entity as available if: # 1. We can verify that the system is online (assuming True if we can't) # 2. We can verify that the entity is online - return not (self._system.version == 3 and self._system.offline) and self._online - - @property - def device_info(self): - """Return device registry information for this entity.""" - return self._device_info - - @property - def extra_state_attributes(self): - """Return the state attributes.""" - return self._attrs - - @property - def name(self): - """Return the name of the entity.""" - return f"{self._system.address} {self._name}" - - @property - def unique_id(self): - """Return the unique ID of the entity.""" - return self._serial + return ( + super().available + and self._online + and not (self._system.version == 3 and self._system.offline) + ) @callback def _handle_coordinator_update(self): @@ -476,15 +460,12 @@ class SimpliSafeBaseSensor(SimpliSafeEntity): def __init__(self, simplisafe, system, sensor): """Initialize.""" super().__init__(simplisafe, system, sensor.name, serial=sensor.serial) - self._device_info["identifiers"] = {(DOMAIN, sensor.serial)} - self._device_info["model"] = sensor.type.name - self._device_info["name"] = sensor.name - self._sensor = sensor - self._sensor_type_human_name = " ".join( - [w.title() for w in self._sensor.type.name.split("_")] - ) - @property - def name(self): - """Return the name of the sensor.""" - return f"{self._system.address} {self._name} {self._sensor_type_human_name}" + self._attr_device_info["identifiers"] = {(DOMAIN, sensor.serial)} + self._attr_device_info["model"] = sensor.type.name + self._attr_device_info["name"] = sensor.name + + human_friendly_name = " ".join([w.title() for w in sensor.type.name.split("_")]) + self._attr_name = f"{super().name} {human_friendly_name}" + + self._sensor = sensor diff --git a/homeassistant/components/simplisafe/alarm_control_panel.py b/homeassistant/components/simplisafe/alarm_control_panel.py index 1f224683a41..28013b69d55 100644 --- a/homeassistant/components/simplisafe/alarm_control_panel.py +++ b/homeassistant/components/simplisafe/alarm_control_panel.py @@ -63,51 +63,32 @@ class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanelEntity): def __init__(self, simplisafe, system): """Initialize the SimpliSafe alarm.""" super().__init__(simplisafe, system, "Alarm Control Panel") - self._changed_by = None + + if isinstance( + self._simplisafe.config_entry.options.get(CONF_CODE), str + ) and re.search("^\\d+$", self._simplisafe.config_entry.options[CONF_CODE]): + self._attr_code_format = FORMAT_NUMBER + else: + self._attr_code_format = FORMAT_TEXT + self._attr_supported_features = SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY self._last_event = None if system.alarm_going_off: - self._state = STATE_ALARM_TRIGGERED + self._attr_state = STATE_ALARM_TRIGGERED elif system.state == SystemStates.away: - self._state = STATE_ALARM_ARMED_AWAY + self._attr_state = STATE_ALARM_ARMED_AWAY elif system.state in ( SystemStates.away_count, SystemStates.exit_delay, SystemStates.home_count, ): - self._state = STATE_ALARM_ARMING + self._attr_state = STATE_ALARM_ARMING elif system.state == SystemStates.home: - self._state = STATE_ALARM_ARMED_HOME + self._attr_state = STATE_ALARM_ARMED_HOME elif system.state == SystemStates.off: - self._state = STATE_ALARM_DISARMED + self._attr_state = STATE_ALARM_DISARMED else: - self._state = None - - @property - def changed_by(self): - """Return info about who changed the alarm last.""" - return self._changed_by - - @property - def code_format(self): - """Return one or more digits/characters.""" - if not self._simplisafe.config_entry.options.get(CONF_CODE): - return None - if isinstance( - self._simplisafe.config_entry.options[CONF_CODE], str - ) and re.search("^\\d+$", self._simplisafe.config_entry.options[CONF_CODE]): - return FORMAT_NUMBER - return FORMAT_TEXT - - @property - def state(self): - """Return the state of the entity.""" - return self._state - - @property - def supported_features(self) -> int: - """Return the list of supported features.""" - return SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY + self._attr_state = None @callback def _is_code_valid(self, code, state): @@ -134,7 +115,7 @@ class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanelEntity): LOGGER.error('Error while disarming "%s": %s', self._system.system_id, err) return - self._state = STATE_ALARM_DISARMED + self._attr_state = STATE_ALARM_DISARMED self.async_write_ha_state() async def async_alarm_arm_home(self, code=None): @@ -150,7 +131,7 @@ class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanelEntity): ) return - self._state = STATE_ALARM_ARMED_HOME + self._attr_state = STATE_ALARM_ARMED_HOME self.async_write_ha_state() async def async_alarm_arm_away(self, code=None): @@ -166,14 +147,14 @@ class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanelEntity): ) return - self._state = STATE_ALARM_ARMING + self._attr_state = STATE_ALARM_ARMING self.async_write_ha_state() @callback def async_update_from_rest_api(self): """Update the entity with the provided REST API data.""" if self._system.version == 3: - self._attrs.update( + self._attr_extra_state_attributes.update( { ATTR_ALARM_DURATION: self._system.alarm_duration, ATTR_ALARM_VOLUME: VOLUME_STRING_MAP[self._system.alarm_volume], @@ -198,14 +179,14 @@ class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanelEntity): # SimpliSafe cloud can sporadically fail to send those updates as expected; so, # just in case, we synchronize the state via the REST API, too: if self._system.state == SystemStates.alarm: - self._state = STATE_ALARM_TRIGGERED + self._attr_state = STATE_ALARM_TRIGGERED elif self._system.state == SystemStates.away: - self._state = STATE_ALARM_ARMED_AWAY + self._attr_state = STATE_ALARM_ARMED_AWAY elif self._system.state in (SystemStates.away_count, SystemStates.exit_delay): - self._state = STATE_ALARM_ARMING + self._attr_state = STATE_ALARM_ARMING elif self._system.state == SystemStates.home: - self._state = STATE_ALARM_ARMED_HOME + self._attr_state = STATE_ALARM_ARMED_HOME elif self._system.state == SystemStates.off: - self._state = STATE_ALARM_DISARMED + self._attr_state = STATE_ALARM_DISARMED else: - self._state = None + self._attr_state = None diff --git a/homeassistant/components/simplisafe/binary_sensor.py b/homeassistant/components/simplisafe/binary_sensor.py index a92309c1123..70fa0fb0e51 100644 --- a/homeassistant/components/simplisafe/binary_sensor.py +++ b/homeassistant/components/simplisafe/binary_sensor.py @@ -71,49 +71,27 @@ class TriggeredBinarySensor(SimpliSafeBaseSensor, BinarySensorEntity): def __init__(self, simplisafe, system, sensor, device_class): """Initialize.""" super().__init__(simplisafe, system, sensor) - self._device_class = device_class - self._is_on = False - @property - def device_class(self): - """Return type of sensor.""" - return self._device_class - - @property - def is_on(self): - """Return true if the sensor is on.""" - return self._is_on + self._attr_device_class = device_class @callback def async_update_from_rest_api(self): """Update the entity with the provided REST API data.""" - self._is_on = self._sensor.triggered + self._attr_is_on = self._sensor.triggered class BatteryBinarySensor(SimpliSafeBaseSensor, BinarySensorEntity): """Define a SimpliSafe battery binary sensor entity.""" + _attr_device_class = DEVICE_CLASS_BATTERY + def __init__(self, simplisafe, system, sensor): """Initialize.""" super().__init__(simplisafe, system, sensor) - self._is_low = False - @property - def device_class(self): - """Return type of sensor.""" - return DEVICE_CLASS_BATTERY - - @property - def unique_id(self): - """Return unique ID of sensor.""" - return f"{self._sensor.serial}-battery" - - @property - def is_on(self): - """Return true if the battery is low.""" - return self._is_low + self._attr_unique_id = f"{super().unique_id}-battery" @callback def async_update_from_rest_api(self): """Update the entity with the provided REST API data.""" - self._is_low = self._sensor.low_battery + self._attr_is_on = self._sensor.low_battery diff --git a/homeassistant/components/simplisafe/lock.py b/homeassistant/components/simplisafe/lock.py index 8bfda08c1a5..982494d9b1c 100644 --- a/homeassistant/components/simplisafe/lock.py +++ b/homeassistant/components/simplisafe/lock.py @@ -35,13 +35,8 @@ class SimpliSafeLock(SimpliSafeEntity, LockEntity): def __init__(self, simplisafe, system, lock): """Initialize.""" super().__init__(simplisafe, system, lock.name, serial=lock.serial) - self._lock = lock - self._is_locked = None - @property - def is_locked(self): - """Return true if the lock is locked.""" - return self._is_locked + self._lock = lock async def async_lock(self, **kwargs): """Lock the lock.""" @@ -51,7 +46,7 @@ class SimpliSafeLock(SimpliSafeEntity, LockEntity): LOGGER.error('Error while locking "%s": %s', self._lock.name, err) return - self._is_locked = True + self._attr_is_locked = True self.async_write_ha_state() async def async_unlock(self, **kwargs): @@ -62,13 +57,13 @@ class SimpliSafeLock(SimpliSafeEntity, LockEntity): LOGGER.error('Error while unlocking "%s": %s', self._lock.name, err) return - self._is_locked = False + self._attr_is_locked = False self.async_write_ha_state() @callback def async_update_from_rest_api(self): """Update the entity with the provided REST API data.""" - self._attrs.update( + self._attr_extra_state_attributes.update( { ATTR_LOCK_LOW_BATTERY: self._lock.lock_low_battery, ATTR_JAMMED: self._lock.state == LockStates.jammed, @@ -76,4 +71,4 @@ class SimpliSafeLock(SimpliSafeEntity, LockEntity): } ) - self._is_locked = self._lock.state == LockStates.locked + self._attr_is_locked = self._lock.state == LockStates.locked diff --git a/homeassistant/components/simplisafe/sensor.py b/homeassistant/components/simplisafe/sensor.py index 9f93a6f9e87..76be7b6e4f0 100644 --- a/homeassistant/components/simplisafe/sensor.py +++ b/homeassistant/components/simplisafe/sensor.py @@ -29,32 +29,10 @@ async def async_setup_entry(hass, entry, async_add_entities): class SimplisafeFreezeSensor(SimpliSafeBaseSensor, SensorEntity): """Define a SimpliSafe freeze sensor entity.""" - def __init__(self, simplisafe, system, sensor): - """Initialize.""" - super().__init__(simplisafe, system, sensor) - self._state = None - - @property - def device_class(self): - """Return type of sensor.""" - return DEVICE_CLASS_TEMPERATURE - - @property - def unique_id(self): - """Return unique ID of sensor.""" - return self._sensor.serial - - @property - def unit_of_measurement(self): - """Return the unit of measurement.""" - return TEMP_FAHRENHEIT - - @property - def state(self): - """Return the sensor state.""" - return self._state + _attr_device_class = DEVICE_CLASS_TEMPERATURE + _attr_unit_of_measurement = TEMP_FAHRENHEIT @callback def async_update_from_rest_api(self): """Update the entity with the provided REST API data.""" - self._state = self._sensor.temperature + self._attr_state = self._sensor.temperature