Add multi phone numbers support (#6605)

* Add multi phone numbers support

* Update fido.py
pull/6947/head
Thibault Cohen 2017-04-05 11:18:02 -04:00 committed by Pascal Vizeli
parent f1f033e5d2
commit 118bd34d74
2 changed files with 24 additions and 11 deletions

View File

@ -21,7 +21,7 @@ from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ["pyfido==0.1.4"]
REQUIREMENTS = ["pyfido==1.0.1"]
_LOGGER = logging.getLogger(__name__)
@ -35,7 +35,6 @@ DEFAULT_NAME = "Fido"
REQUESTS_TIMEOUT = 15
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15)
SENSOR_TYPES = {
'fido_dollar': ['Fido dollar',
PRICE, 'mdi:square-inc-cash'],
@ -103,8 +102,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
name = config.get(CONF_NAME)
sensors = []
for variable in config[CONF_MONITORED_VARIABLES]:
sensors.append(FidoSensor(fido_data, variable, name))
for number in fido_data.client.get_phone_numbers():
for variable in config[CONF_MONITORED_VARIABLES]:
sensors.append(FidoSensor(fido_data, variable, name, number))
add_devices(sensors, True)
@ -112,9 +112,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class FidoSensor(Entity):
"""Implementation of a Fido sensor."""
def __init__(self, fido_data, sensor_type, name):
def __init__(self, fido_data, sensor_type, name, number):
"""Initialize the sensor."""
self.client_name = name
self._number = number
self.type = sensor_type
self._name = SENSOR_TYPES[sensor_type][0]
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
@ -125,7 +126,7 @@ class FidoSensor(Entity):
@property
def name(self):
"""Return the name of the sensor."""
return '{} {}'.format(self.client_name, self._name)
return '{} {} {}'.format(self.client_name, self._number, self._name)
@property
def state(self):
@ -142,21 +143,33 @@ class FidoSensor(Entity):
"""Icon to use in the frontend, if any."""
return self._icon
@property
def device_state_attributes(self):
"""Return the state attributes of the sensor."""
return {
'number': self._number,
}
def update(self):
"""Get the latest data from Fido and update the state."""
self.fido_data.update()
if self.type in self.fido_data.data:
if self.fido_data.data[self.type] is not None:
if self._name == 'balance':
if self.fido_data.data.get(self.type) is not None:
self._state = round(self.fido_data.data[self.type], 2)
else:
if self.fido_data.data.get(self._number, {}).get(self.type) \
is not None:
self._state = self.fido_data.data[self._number][self.type]
self._state = round(self._state, 2)
class FidoData(object):
"""Get data from Fido."""
def __init__(self, number, password):
def __init__(self, username, password):
"""Initialize the data object."""
from pyfido import FidoClient
self.client = FidoClient(number, password, REQUESTS_TIMEOUT)
self.client = FidoClient(username, password, REQUESTS_TIMEOUT)
self.data = {}
@Throttle(MIN_TIME_BETWEEN_UPDATES)

View File

@ -515,7 +515,7 @@ pyemby==1.1
pyenvisalink==2.0
# homeassistant.components.sensor.fido
pyfido==0.1.4
pyfido==1.0.1
# homeassistant.components.ifttt
pyfttt==0.3