2019-09-11 17:26:50 +00:00
|
|
|
"""Support for Obihai Sensors."""
|
|
|
|
from datetime import timedelta
|
2019-12-09 13:26:53 +00:00
|
|
|
import logging
|
2019-09-11 17:26:50 +00:00
|
|
|
|
|
|
|
from pyobihai import PyObihai
|
2019-12-09 13:26:53 +00:00
|
|
|
import voluptuous as vol
|
2019-09-11 17:26:50 +00:00
|
|
|
|
|
|
|
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
|
2019-12-09 13:26:53 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2019-09-11 17:26:50 +00:00
|
|
|
|
|
|
|
_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 = []
|
|
|
|
|
2019-09-26 08:53:31 +00:00
|
|
|
pyobihai = PyObihai(host, username, password)
|
2019-09-11 17:26:50 +00:00
|
|
|
|
2019-09-26 08:53:31 +00:00
|
|
|
login = pyobihai.check_account()
|
2019-09-25 18:26:15 +00:00
|
|
|
if not login:
|
|
|
|
_LOGGER.error("Invalid credentials")
|
|
|
|
return
|
|
|
|
|
2019-09-26 08:53:31 +00:00
|
|
|
serial = pyobihai.get_device_serial()
|
2019-09-11 17:26:50 +00:00
|
|
|
|
2019-09-26 08:53:31 +00:00
|
|
|
services = pyobihai.get_state()
|
2019-09-11 17:26:50 +00:00
|
|
|
|
2019-09-26 08:53:31 +00:00
|
|
|
line_services = pyobihai.get_line_state()
|
|
|
|
|
|
|
|
call_direction = pyobihai.get_call_direction()
|
2019-09-25 18:26:15 +00:00
|
|
|
|
2019-09-11 17:26:50 +00:00
|
|
|
for key in services:
|
2019-09-26 08:53:31 +00:00
|
|
|
sensors.append(ObihaiServiceSensors(pyobihai, serial, key))
|
2019-09-11 17:26:50 +00:00
|
|
|
|
2020-03-18 20:19:40 +00:00
|
|
|
if line_services is not None:
|
|
|
|
for key in line_services:
|
|
|
|
sensors.append(ObihaiServiceSensors(pyobihai, serial, key))
|
2019-09-11 17:26:50 +00:00
|
|
|
|
2019-09-25 18:26:15 +00:00
|
|
|
for key in call_direction:
|
2019-09-26 08:53:31 +00:00
|
|
|
sensors.append(ObihaiServiceSensors(pyobihai, serial, key))
|
2019-09-25 18:26:15 +00:00
|
|
|
|
2019-09-11 17:26:50 +00:00
|
|
|
add_entities(sensors)
|
|
|
|
|
|
|
|
|
|
|
|
class ObihaiServiceSensors(Entity):
|
|
|
|
"""Get the status of each Obihai Lines."""
|
|
|
|
|
2019-09-26 08:53:31 +00:00
|
|
|
def __init__(self, pyobihai, serial, service_name):
|
2019-09-11 17:26:50 +00:00
|
|
|
"""Initialize monitor sensor."""
|
|
|
|
self._service_name = service_name
|
|
|
|
self._state = None
|
|
|
|
self._name = f"{OBIHAI} {self._service_name}"
|
|
|
|
self._pyobihai = pyobihai
|
2019-09-26 08:53:31 +00:00
|
|
|
self._unique_id = f"{serial}-{self._service_name}"
|
2019-09-11 17:26:50 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
2019-09-26 08:53:31 +00:00
|
|
|
@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
|
|
|
|
|
2019-09-11 17:26:50 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class for uptime sensor."""
|
|
|
|
if self._service_name == "Last Reboot":
|
|
|
|
return DEVICE_CLASS_TIMESTAMP
|
|
|
|
return None
|
|
|
|
|
2019-10-01 00:42:06 +00:00
|
|
|
@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"
|
|
|
|
return "mdi:phone"
|
|
|
|
|
2019-09-11 17:26:50 +00:00
|
|
|
def update(self):
|
|
|
|
"""Update the sensor."""
|
2019-09-26 08:53:31 +00:00
|
|
|
services = self._pyobihai.get_state()
|
2019-09-11 17:26:50 +00:00
|
|
|
|
|
|
|
if self._service_name in services:
|
|
|
|
self._state = services.get(self._service_name)
|
|
|
|
|
2019-09-26 08:53:31 +00:00
|
|
|
services = self._pyobihai.get_line_state()
|
2019-09-11 17:26:50 +00:00
|
|
|
|
2020-03-18 20:19:40 +00:00
|
|
|
if services is not None:
|
|
|
|
if self._service_name in services:
|
|
|
|
self._state = services.get(self._service_name)
|
2019-09-25 18:26:15 +00:00
|
|
|
|
2019-09-26 08:53:31 +00:00
|
|
|
call_direction = self._pyobihai.get_call_direction()
|
2019-09-25 18:26:15 +00:00
|
|
|
|
|
|
|
if self._service_name in call_direction:
|
|
|
|
self._state = call_direction.get(self._service_name)
|