Bump pyobihai, add unique ID and availability (#26922)

* Bump pyobihai, add unique ID and availability

* Fix unique ID

* Fetch serial correctly
This commit is contained in:
Daniel Shokouhi 2019-09-26 01:53:31 -07:00 committed by Pascal Vizeli
parent 770ad86f12
commit 62cea2b7ac
3 changed files with 29 additions and 21 deletions

View file

@ -3,7 +3,7 @@
"name": "Obihai",
"documentation": "https://www.home-assistant.io/components/obihai",
"requirements": [
"pyobihai==1.1.1"
"pyobihai==1.2.0"
],
"dependencies": [],
"codeowners": ["@dshokouhi"]

View file

@ -44,27 +44,29 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
sensors = []
pyobihai = PyObihai()
pyobihai = PyObihai(host, username, password)
login = pyobihai.check_account(host, username, password)
login = pyobihai.check_account()
if not login:
_LOGGER.error("Invalid credentials")
return
services = pyobihai.get_state(host, username, password)
serial = pyobihai.get_device_serial()
line_services = pyobihai.get_line_state(host, username, password)
services = pyobihai.get_state()
call_direction = pyobihai.get_call_direction(host, username, password)
line_services = pyobihai.get_line_state()
call_direction = pyobihai.get_call_direction()
for key in services:
sensors.append(ObihaiServiceSensors(pyobihai, host, username, password, key))
sensors.append(ObihaiServiceSensors(pyobihai, serial, key))
for key in line_services:
sensors.append(ObihaiServiceSensors(pyobihai, host, username, password, key))
sensors.append(ObihaiServiceSensors(pyobihai, serial, key))
for key in call_direction:
sensors.append(ObihaiServiceSensors(pyobihai, host, username, password, key))
sensors.append(ObihaiServiceSensors(pyobihai, serial, key))
add_entities(sensors)
@ -72,15 +74,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class ObihaiServiceSensors(Entity):
"""Get the status of each Obihai Lines."""
def __init__(self, pyobihai, host, username, password, service_name):
def __init__(self, pyobihai, serial, service_name):
"""Initialize monitor sensor."""
self._host = host
self._username = username
self._password = password
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):
@ -92,6 +92,18 @@ class ObihaiServiceSensors(Entity):
"""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."""
@ -101,21 +113,17 @@ class ObihaiServiceSensors(Entity):
def update(self):
"""Update the sensor."""
services = self._pyobihai.get_state(self._host, self._username, self._password)
services = self._pyobihai.get_state()
if self._service_name in services:
self._state = services.get(self._service_name)
services = self._pyobihai.get_line_state(
self._host, self._username, self._password
)
services = self._pyobihai.get_line_state()
if self._service_name in services:
self._state = services.get(self._service_name)
call_direction = self._pyobihai.get_call_direction(
self._host, self._username, self._password
)
call_direction = self._pyobihai.get_call_direction()
if self._service_name in call_direction:
self._state = call_direction.get(self._service_name)

View file

@ -1346,7 +1346,7 @@ pynx584==0.4
pynzbgetapi==0.2.0
# homeassistant.components.obihai
pyobihai==1.1.1
pyobihai==1.2.0
# homeassistant.components.ombi
pyombi==0.1.5