forked from sogood007/home-assistant-obihai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.py
157 lines (122 loc) · 4.46 KB
/
sensor.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
"""Support for Obihai Sensors."""
from datetime import timedelta
import logging
from pyobihai import PyObihai
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_HOST,
CONF_PASSWORD,
CONF_USERNAME,
DEVICE_CLASS_TIMESTAMP,
)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=5)
OBIHAI = "Obihai"
DEFAULT_USERNAME = "admin"
DEFAULT_PASSWORD = "admin"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_USERNAME, default=DEFAULT_USERNAME): cv.string,
vol.Optional(CONF_PASSWORD, default=DEFAULT_PASSWORD): cv.string,
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Obihai sensor platform."""
username = config[CONF_USERNAME]
password = config[CONF_PASSWORD]
host = config[CONF_HOST]
sensors = []
pyobihai = PyObihai(host, username, password)
login = pyobihai.check_account()
if not login:
_LOGGER.error("Invalid credentials")
return
serial = pyobihai.get_device_serial()
services = pyobihai.get_state()
line_services = pyobihai.get_line_state()
call_direction = pyobihai.get_call_direction()
for key in services:
sensors.append(ObihaiServiceSensors(pyobihai, serial, key))
if line_services is not None:
for key in line_services:
sensors.append(ObihaiServiceSensors(pyobihai, serial, key))
for key in call_direction:
sensors.append(ObihaiServiceSensors(pyobihai, serial, key))
add_entities(sensors)
class ObihaiServiceSensors(Entity):
"""Get the status of each Obihai Lines."""
def __init__(self, pyobihai, serial, service_name):
"""Initialize monitor sensor."""
self._service_name = service_name
self._state = None
self._name = f"{OBIHAI} {self._service_name}"
self._pyobihai = pyobihai
self._unique_id = f"{serial}-{self._service_name}"
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def available(self):
"""Return if sensor is available."""
if self._state is not None:
return True
return False
@property
def unique_id(self):
"""Return the unique ID."""
return self._unique_id
@property
def device_class(self):
"""Return the device class for uptime sensor."""
if self._service_name == "Last Reboot":
return DEVICE_CLASS_TIMESTAMP
return None
@property
def icon(self):
"""Return an icon."""
if self._service_name == "Call Direction":
if self._state == "No Active Calls":
return "mdi:phone-off"
if self._state == "Inbound Call":
return "mdi:phone-incoming"
return "mdi:phone-outgoing"
if "Caller Info" in self._service_name:
return "mdi:phone-log"
if "Port" in self._service_name:
if self._state == "Ringing":
return "mdi:phone-ring"
if self._state == "Off Hook":
return "mdi:phone-in-talk"
return "mdi:phone-hangup"
if "Service Status" in self._service_name:
if "OBiTALK Service Status" in self._service_name:
return "mdi:phone-check"
if self._state == "0":
return "mdi:phone-hangup"
return "mdi:phone-in-talk"
if "Reboot Required" in self._service_name:
if self._state == "false":
return "mdi:restart-off"
return "mdi:restart-alert"
return "mdi:phone"
def update(self):
"""Update the sensor."""
services = self._pyobihai.get_state()
if self._service_name in services:
self._state = services.get(self._service_name)
services = self._pyobihai.get_line_state()
if services is not None:
if self._service_name in services:
self._state = services.get(self._service_name)
call_direction = self._pyobihai.get_call_direction()
if self._service_name in call_direction:
self._state = call_direction.get(self._service_name)