This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlock.py
163 lines (140 loc) · 5.73 KB
/
lock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""Platform for lock integration."""
import logging
from datetime import timedelta
from pytedee import TedeeClient
from pytedee import TedeeClientException
from pytedee import Lock
import voluptuous as vol
import pkg_resources
from pkg_resources import parse_version
import homeassistant.helpers.config_validation as cv
from homeassistant.core import callback
from homeassistant.helpers.event import async_call_later
# Import the device class from the component that you want to support
from homeassistant.components.lock import PLATFORM_SCHEMA, SUPPORT_OPEN, LockEntity
from homeassistant.const import ATTR_BATTERY_LEVEL, ATTR_ID, CONF_ACCESS_TOKEN, STATE_LOCKED, STATE_UNLOCKED
ATTR_NUMERIC_STATE = "numeric_state"
ATTR_SUPPORT_PULLSPING = "support_pullspring"
ATTR_DURATION_PULLSPRING = "duration_pullspring"
ATTR_CONNECTED = "connected"
ATTR_CHARGING = "charging"
_LOGGER = logging.getLogger(__name__)
# Validation of the user's configuration
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_ACCESS_TOKEN, default='no access token given'): cv.string,
})
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Tedee Lock platform."""
try:
#_LOGGER.error("Creds: %s, %s", config[CONF_ACCESS_TOKEN])
tedee = TedeeClient(config[CONF_ACCESS_TOKEN])
except TedeeClientException as exc:
_LOGGER.error(exc)
return
available_locks = tedee.get_locks()
_LOGGER.debug("available_locks: %s", available_locks)
if not available_locks:
# No locks found; abort setup routine.
_LOGGER.info("No locks found in your account")
return
add_entities([TedeeLock(lock, tedee) for lock in available_locks], True)
class TedeeLock(LockEntity):
"""Representation of a Tedee lock."""
def __init__(self, tedee_lock, client):
_LOGGER.debug("LockEntity: %s", tedee_lock.get_name())
"""Initialize the lock."""
self._sensor = tedee_lock
self._lock_id = tedee_lock.get_id()
self._client = client
self._state = STATE_LOCKED
self._battery_level = tedee_lock.get_battery_level()
self._available = True
self._entity_attrs = {
ATTR_ID: self._lock_id,
ATTR_BATTERY_LEVEL: self._battery_level
}
if parse_version('0.0.2') <= parse_version(pkg_resources.get_distribution('pytedee').version):
self._entity_attrs[ATTR_SUPPORT_PULLSPING] = self._sensor.get_is_enabled_pullspring()
self._entity_attrs[ATTR_DURATION_PULLSPRING] = self._sensor.get_duration_pullspring()
@property
def supported_features(self):
"""Flag supported features."""
if parse_version('0.0.2') <= parse_version(pkg_resources.get_distribution('pytedee').version):
if self._sensor.get_is_enabled_pullspring():
return SUPPORT_OPEN
else:
return None
else:
return SUPPORT_OPEN
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._available
def update(self):
self._available = self._client.update(self._lock_id)
self._battery_level = self._sensor.get_battery_level()
self._state = self.decode_state(self._sensor.get_state())
@property
def name(self):
"""Return the name of the lock."""
name = self._sensor.get_name()
return name
@property
def is_locked(self):
"""Return true if lock is locked."""
return self.decode_state(self._sensor.get_state())
@property
def extra_state_attributes(self):
"""Return the entity specific state attributes."""
self._entity_attrs[ATTR_BATTERY_LEVEL] = self._battery_level
self._entity_attrs[ATTR_NUMERIC_STATE] = self._sensor.get_state()
if parse_version('0.0.2') <= parse_version(pkg_resources.get_distribution('pytedee').version):
self._entity_attrs[ATTR_CONNECTED] = self._sensor.is_connected()
self._entity_attrs[ATTR_CHARGING] = self._sensor.get_is_charging()
return self._entity_attrs
@property
def unique_id(self):
return self._lock_id
def unlock(self, **kwargs):
"""Unlock the door."""
try:
self._client.unlock(self._lock_id)
except TedeeClientException:
_LOGGER.debug("Failed to unlock the door.")
else:
#self._state = self.decode_state(self._sensor.get_state())
#self.async_write_ha_state()
async_call_later(self.hass, 3, self.force_update)
def lock(self, **kwargs):
"""Lock the door."""
try:
self._client.lock(self._lock_id)
except TedeeClientException:
_LOGGER.debug("Failed to lock the door.")
else:
#self._state = self.decode_state(self._sensor.get_state())
#self.async_write_ha_state()
async_call_later(self.hass, 3, self.force_update)
def open(self, **kwargs):
"""Unlatch the door."""
try:
self._client.open(self._lock_id)
except TedeeClientException:
_LOGGER.debug("Failed to unlatch the door.")
else:
#self._state = self.decode_state(self._sensor.get_state())
#self.async_write_ha_state()
async_call_later(self.hass, 5, self.force_update)
@callback
def force_update(self, _):
self._state = self.decode_state(self._sensor.get_state())
self._battery_level = self._sensor.get_battery_level()
self.async_write_ha_state()
def decode_state(self, state):
#_LOGGER.error("decode_state: %d", state)
if state == 5:
return True
elif state == 6:
return True
else:
return False