Use entity class attributes for august (#52744)

This commit is contained in:
Robert Hillis 2021-07-13 15:56:34 -04:00 committed by GitHub
parent 12ac666459
commit d76607e945
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 177 deletions

View file

@ -126,34 +126,18 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class AugustDoorBinarySensor(AugustEntityMixin, BinarySensorEntity):
"""Representation of an August Door binary sensor."""
_attr_device_class = DEVICE_CLASS_DOOR
def __init__(self, data, sensor_type, device):
"""Initialize the sensor."""
super().__init__(data, device)
self._data = data
self._sensor_type = sensor_type
self._device = device
self._attr_name = f"{device.device_name} Open"
self._attr_unique_id = f"{self._device_id}_open"
self._update_from_data()
@property
def available(self):
"""Return the availability of this sensor."""
return self._detail.bridge_is_online
@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self._detail.door_state == LockDoorStatus.OPEN
@property
def device_class(self):
"""Return the class of this device."""
return DEVICE_CLASS_DOOR
@property
def name(self):
"""Return the name of the binary sensor."""
return f"{self._device.device_name} Open"
@callback
def _update_from_data(self):
"""Get the latest state of the sensor and update activity."""
@ -173,11 +157,8 @@ class AugustDoorBinarySensor(AugustEntityMixin, BinarySensorEntity):
if bridge_activity is not None:
update_lock_detail_from_activity(self._detail, bridge_activity)
@property
def unique_id(self) -> str:
"""Get the unique of the door open binary sensor."""
return f"{self._device_id}_open"
self._attr_available = self._detail.bridge_is_online
self._attr_is_on = self._detail.door_state == LockDoorStatus.OPEN
class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity):
@ -189,36 +170,18 @@ class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity):
self._check_for_off_update_listener = None
self._data = data
self._sensor_type = sensor_type
self._device = device
self._state = None
self._available = False
self._attr_device_class = self._sensor_config[SENSOR_DEVICE_CLASS]
self._attr_name = f"{device.device_name} {self._sensor_config[SENSOR_NAME]}"
self._attr_unique_id = (
f"{self._device_id}_{self._sensor_config[SENSOR_NAME].lower()}"
)
self._update_from_data()
@property
def available(self):
"""Return the availability of this sensor."""
return self._available
@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self._state
@property
def _sensor_config(self):
"""Return the config for the sensor."""
return SENSOR_TYPES_DOORBELL[self._sensor_type]
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return self._sensor_config[SENSOR_DEVICE_CLASS]
@property
def name(self):
"""Return the name of the binary sensor."""
return f"{self._device.device_name} {self._sensor_config[SENSOR_NAME]}"
@property
def _state_provider(self):
"""Return the state provider for the binary sensor."""
@ -233,19 +196,19 @@ class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity):
def _update_from_data(self):
"""Get the latest state of the sensor."""
self._cancel_any_pending_updates()
self._state = self._state_provider(self._data, self._detail)
self._attr_is_on = self._state_provider(self._data, self._detail)
if self._is_time_based:
self._available = _retrieve_online_state(self._data, self._detail)
self._attr_available = _retrieve_online_state(self._data, self._detail)
self._schedule_update_to_recheck_turn_off_sensor()
else:
self._available = True
self._attr_available = True
def _schedule_update_to_recheck_turn_off_sensor(self):
"""Schedule an update to recheck the sensor to see if it is ready to turn off."""
# If the sensor is already off there is nothing to do
if not self._state:
if not self.is_on:
return
# self.hass is only available after setup is completed
@ -258,7 +221,7 @@ class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity):
"""Timer callback for sensor update."""
self._check_for_off_update_listener = None
self._update_from_data()
if not self._state:
if not self.is_on:
self.async_write_ha_state()
self._check_for_off_update_listener = async_call_later(
@ -277,8 +240,3 @@ class AugustDoorbellBinarySensor(AugustEntityMixin, BinarySensorEntity):
"""Call the mixin to subscribe and setup an async_track_point_in_utc_time to turn off the sensor if needed."""
self._schedule_update_to_recheck_turn_off_sensor()
await super().async_added_to_hass()
@property
def unique_id(self) -> str:
"""Get the unique id of the doorbell sensor."""
return f"{self._device_id}_{self._sensor_config[SENSOR_NAME].lower()}"