-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathswitch.py
85 lines (65 loc) · 2.26 KB
/
switch.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
"""Support for powering relays in a DoorBird video doorbell."""
import datetime
import logging
try:
from homeassistant.components.switch import SwitchEntity
except ImportError:
from homeassistant.components.switch import SwitchDevice as SwitchEntity
import homeassistant.util.dt as dt_util
from .const import DOMAIN
from .entity import PIKIntercomEntity
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None):
acc = hass.data[DOMAIN]['account'].account
intercoms = []
entities = []
for apart in acc.apartments():
intercoms.extend(apart.intercoms())
for i in intercoms:
entities.append(
PIKIntercomSwitch(
i,
)
)
add_entities(entities)
class PIKIntercomSwitch(PIKIntercomEntity, SwitchEntity):
"""PIKIntercom device."""
def __init__(self, intercom):
"""Initialize PIKIntercom device."""
super().__init__(intercom)
self._intercom = intercom
self._state = False
self._assume_off = datetime.datetime.min
self._time = datetime.timedelta(seconds=5)
self._unique_id = f"{self._intercom.id()}"
@property
def unique_id(self):
"""Switch unique id."""
return self._unique_id
@property
def icon(self):
"""Return the icon to display."""
return "mdi:dip-switch"
@property
def is_on(self):
"""Get the assumed state of the relay."""
return self._state
def turn_on(self, **kwargs):
"""Open intercom"""
result = self._intercom.open()
if not result:
raise Exception("failed")
_LOGGER.info(f'Door {str(self._intercom)} open')
now = dt_util.utcnow()
self._assume_off = now + self._time
def turn_off(self, **kwargs):
"""Turn off the relays is not needed. They are time-based."""
raise NotImplementedError("PIKIntercom cannot be manually turned off.")
def update(self):
"""Wait for the correct amount of assumed time to pass."""
if self._state and self._assume_off <= dt_util.utcnow():
self._state = False
self._assume_off = datetime.datetime.min
@property
def model(self):
return "switch"