1
mirror of https://github.com/home-assistant/core synced 2024-10-01 05:30:36 +02:00
ha-core/homeassistant/components/tesla/lock.py
Paulus Schoutsen 4de97abc3a Black
2019-07-31 12:25:30 -07:00

49 lines
1.5 KiB
Python

"""Support for Tesla door locks."""
import logging
from homeassistant.components.lock import LockDevice
from homeassistant.const import STATE_LOCKED, STATE_UNLOCKED
from . import DOMAIN as TESLA_DOMAIN, TeslaDevice
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Tesla lock platform."""
devices = [
TeslaLock(device, hass.data[TESLA_DOMAIN]["controller"])
for device in hass.data[TESLA_DOMAIN]["devices"]["lock"]
]
add_entities(devices, True)
class TeslaLock(TeslaDevice, LockDevice):
"""Representation of a Tesla door lock."""
def __init__(self, tesla_device, controller):
"""Initialise of the lock."""
self._state = None
super().__init__(tesla_device, controller)
def lock(self, **kwargs):
"""Send the lock command."""
_LOGGER.debug("Locking doors for: %s", self._name)
self.tesla_device.lock()
def unlock(self, **kwargs):
"""Send the unlock command."""
_LOGGER.debug("Unlocking doors for: %s", self._name)
self.tesla_device.unlock()
@property
def is_locked(self):
"""Get whether the lock is in locked state."""
return self._state == STATE_LOCKED
def update(self):
"""Update state of the lock."""
_LOGGER.debug("Updating state for: %s", self._name)
self.tesla_device.update()
self._state = STATE_LOCKED if self.tesla_device.is_locked() else STATE_UNLOCKED