Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion homeassistant/components/tahoma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
extra=vol.ALLOW_EXTRA,
)

TAHOMA_COMPONENTS = ["scene", "sensor", "cover", "switch", "binary_sensor"]
TAHOMA_COMPONENTS = ["binary_sensor", "cover", "lock", "scene", "sensor", "switch"]

TAHOMA_TYPES = {
"io:AwningValanceIOComponent": "cover",
Expand All @@ -52,6 +52,7 @@
"io:VerticalExteriorAwningIOComponent": "cover",
"io:VerticalInteriorBlindVeluxIOComponent": "cover",
"io:WindowOpenerVeluxIOComponent": "cover",
"opendoors:OpenDoorsSmartLockComponent": "lock",
"rtds:RTDSContactSensor": "sensor",
"rtds:RTDSMotionSensor": "sensor",
"rtds:RTDSSmokeSensor": "smoke",
Expand Down
87 changes: 87 additions & 0 deletions homeassistant/components/tahoma/lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""Support for Tahoma lock."""
from datetime import timedelta
import logging

from homeassistant.components.lock import LockDevice
from homeassistant.const import (
ATTR_BATTERY_LEVEL,
ATTR_NAME,
STATE_LOCKED,
STATE_UNLOCKED,
)

from . import DOMAIN as TAHOMA_DOMAIN, TahomaDevice

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(seconds=120)


def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Tahoma lock."""
controller = hass.data[TAHOMA_DOMAIN]["controller"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a guard clause here that checks if discovery_info is None and return if so.

devices = []
for device in hass.data[TAHOMA_DOMAIN]["devices"]["lock"]:
devices.append(TahomaLock(device, controller))
add_entities(devices, True)


class TahomaLock(TahomaDevice, LockDevice):
"""Representation a Tahoma lock."""

def __init__(self, tahoma_device, controller):
"""Initialize the device."""
super().__init__(tahoma_device, controller)
self._lock_status = None
self._available = False
Comment thread
springstan marked this conversation as resolved.
self._battery_level = None
self._name = None
Comment thread
vlebourl marked this conversation as resolved.

def update(self):
"""Update method."""
self.controller.get_states([self.tahoma_device])
self._battery_level = self.tahoma_device.active_states["core:BatteryState"]
self._name = self.tahoma_device.active_states["core:NameState"]
if self._battery_level == "low":
_LOGGER.warning("Lock %s has low battery", self._name)
if self._battery_level == "verylow":
_LOGGER.error("Lock %s has very low battery", self._name)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't log errors if the application is functioning normally. The low battery level is just a state at the device. It's not a problem with Home Assistant.

The user can make an automation that triggers an alert if the battery level attribute reaches a certain value.

if (
self.tahoma_device.active_states.get("core:LockedUnlockedState")
== STATE_LOCKED

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't use the home assistant state constant to compare with the tahoma state API. These are two different APIs and either may change independently of the other.

Please define a separate string constant for the tahoma API to use here.

):
self._lock_status = STATE_LOCKED
else:
self._lock_status = STATE_UNLOCKED
self._available = (
self.tahoma_device.active_states.get("core:AvailabilityState")
== "available"
)

def unlock(self, **kwargs):
"""Unlock method."""
_LOGGER.debug("Unlocking %s", self._name)
self.apply_action("unlock")

def lock(self, **kwargs):
"""Lock method."""
_LOGGER.debug("Locking %s", self._name)
self.apply_action("lock")

@property
def is_locked(self):
"""Return true if the lock is locked."""
return self._lock_status == STATE_LOCKED

@property
def device_state_attributes(self):
"""Return the device state attributes."""
attr = {
ATTR_BATTERY_LEVEL: self.tahoma_device.active_states["core:BatteryState"],
Comment thread
springstan marked this conversation as resolved.
Outdated
"availability": self.tahoma_device.active_states["core:AvailabilityState"],
ATTR_NAME: self.tahoma_device.active_states["core:NameState"],
Comment thread
springstan marked this conversation as resolved.
Outdated
}
super_attr = super().device_state_attributes
if super_attr is not None:
attr.update(super_attr)
return attr