hass-core/homeassistant/components/lock/august.py
ehendrix23 f504e5ef61 Add doorsense sensor for August 3rd Gen Smart Lock Pro (#17299)
* Add doorsense sensor for August 3rd Gen Smart Lock Pro

Add a binary sensor to August for the August 3rd Gen Smart Lock Pro doorsense sensor.

This is a re-do from PR 17116 https://github.com/home-assistant/home-assistant/pull/17116 that I closed due to rebase issue on my end.

* Changed to use snjoetw provided code

Going through the py-august I found that snjoetw had provided updated versions for the august component (august.py and binary_sensor/august.py) to include DoorSense sensor.
Changed what I did to to what snjoetw provided instead as he split it into 2 classes; much cleaner I think.

I modified his coding with:
   Fixes that were done to the August component and not part of the coding snjoetw provided.
   Added the debug logging improvement I had done in the code.

Note, fix I committed earlier for lock atribute (lock/august.py) is thus still the same.

* Reverted change from add_device to add_entities

Missed an item when merging snjoetw's code with current. Fixed.

* Updated call from add_devices to add_entities as well

Updated the call from add_devices to add_entities.

* Fixed permissions on files

Fixed permissions on components/august.py and binary_snesor/august.py

* Changed if/else to if/continue

Changed logic so that if the door sensor state is unknown during initalization the debug log is written and then continue the loop instead of using if/else logic.

* Added available property for Door Sensor

Added the available property for the Door Sensor and setting it to False if a status unknown is received.

* Updated setting self._available

Changed line for setting self._available to what Martin provided. Much  more efficient to read. :-)
2018-10-19 09:37:02 +02:00

89 lines
2.4 KiB
Python

"""
Support for August lock.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/lock.august/
"""
import logging
from datetime import timedelta
from homeassistant.components.august import DATA_AUGUST
from homeassistant.components.lock import LockDevice
from homeassistant.const import ATTR_BATTERY_LEVEL
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['august']
SCAN_INTERVAL = timedelta(seconds=5)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up August locks."""
data = hass.data[DATA_AUGUST]
devices = []
for lock in data.locks:
_LOGGER.debug("Adding lock for %s", lock.device_name)
devices.append(AugustLock(data, lock))
add_entities(devices, True)
class AugustLock(LockDevice):
"""Representation of an August lock."""
def __init__(self, data, lock):
"""Initialize the lock."""
self._data = data
self._lock = lock
self._lock_status = None
self._lock_detail = None
self._changed_by = None
def lock(self, **kwargs):
"""Lock the device."""
self._data.lock(self._lock.device_id)
def unlock(self, **kwargs):
"""Unlock the device."""
self._data.unlock(self._lock.device_id)
def update(self):
"""Get the latest state of the sensor."""
self._lock_status = self._data.get_lock_status(self._lock.device_id)
self._lock_detail = self._data.get_lock_detail(self._lock.device_id)
from august.activity import ActivityType
activity = self._data.get_latest_device_activity(
self._lock.device_id,
ActivityType.LOCK_OPERATION)
if activity is not None:
self._changed_by = activity.operated_by
@property
def name(self):
"""Return the name of this device."""
return self._lock.device_name
@property
def is_locked(self):
"""Return true if device is on."""
from august.lock import LockStatus
return self._lock_status is LockStatus.LOCKED
@property
def changed_by(self):
"""Last change triggered by."""
return self._changed_by
@property
def device_state_attributes(self):
"""Return the device specific state attributes."""
if self._lock_detail is None:
return None
return {
ATTR_BATTERY_LEVEL: self._lock_detail.battery_level,
}