2017-02-15 20:21:38 +00:00
|
|
|
"""
|
|
|
|
Support for Fido.
|
|
|
|
|
|
|
|
Get data from 'Usage Summary' page:
|
|
|
|
https://www.fido.ca/pages/#/my-account/wireless
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.fido/
|
|
|
|
"""
|
2017-12-29 17:33:11 +00:00
|
|
|
import asyncio
|
2017-02-15 20:21:38 +00:00
|
|
|
import logging
|
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_USERNAME, CONF_PASSWORD,
|
|
|
|
CONF_NAME, CONF_MONITORED_VARIABLES)
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.util import Throttle
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2017-12-29 17:33:11 +00:00
|
|
|
REQUIREMENTS = ['pyfido==2.1.0']
|
2017-02-15 20:21:38 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-05-02 16:18:47 +00:00
|
|
|
KILOBITS = 'Kb' # type: str
|
|
|
|
PRICE = 'CAD' # type: str
|
|
|
|
MESSAGES = 'messages' # type: str
|
|
|
|
MINUTES = 'minutes' # type: str
|
2017-02-15 20:21:38 +00:00
|
|
|
|
2017-05-02 16:18:47 +00:00
|
|
|
DEFAULT_NAME = 'Fido'
|
2017-02-15 20:21:38 +00:00
|
|
|
|
|
|
|
REQUESTS_TIMEOUT = 15
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15)
|
|
|
|
|
|
|
|
SENSOR_TYPES = {
|
2017-05-02 16:18:47 +00:00
|
|
|
'fido_dollar': ['Fido dollar', PRICE, 'mdi:square-inc-cash'],
|
|
|
|
'balance': ['Balance', PRICE, 'mdi:square-inc-cash'],
|
|
|
|
'data_used': ['Data used', KILOBITS, 'mdi:download'],
|
|
|
|
'data_limit': ['Data limit', KILOBITS, 'mdi:download'],
|
|
|
|
'data_remaining': ['Data remaining', KILOBITS, 'mdi:download'],
|
|
|
|
'text_used': ['Text used', MESSAGES, 'mdi:message-text'],
|
|
|
|
'text_limit': ['Text limit', MESSAGES, 'mdi:message-text'],
|
|
|
|
'text_remaining': ['Text remaining', MESSAGES, 'mdi:message-text'],
|
|
|
|
'mms_used': ['MMS used', MESSAGES, 'mdi:message-image'],
|
|
|
|
'mms_limit': ['MMS limit', MESSAGES, 'mdi:message-image'],
|
|
|
|
'mms_remaining': ['MMS remaining', MESSAGES, 'mdi:message-image'],
|
2017-02-15 20:21:38 +00:00
|
|
|
'text_int_used': ['International text used',
|
|
|
|
MESSAGES, 'mdi:message-alert'],
|
|
|
|
'text_int_limit': ['International text limit',
|
2018-01-29 22:37:19 +00:00
|
|
|
MESSAGES, 'mdi:message-alert'],
|
|
|
|
'text_int_remaining': ['International remaining',
|
2017-02-15 20:21:38 +00:00
|
|
|
MESSAGES, 'mdi:message-alert'],
|
2017-05-02 16:18:47 +00:00
|
|
|
'talk_used': ['Talk used', MINUTES, 'mdi:cellphone'],
|
|
|
|
'talk_limit': ['Talk limit', MINUTES, 'mdi:cellphone'],
|
2018-01-29 22:37:19 +00:00
|
|
|
'talk_remaining': ['Talk remaining', MINUTES, 'mdi:cellphone'],
|
2017-05-02 16:18:47 +00:00
|
|
|
'other_talk_used': ['Other Talk used', MINUTES, 'mdi:cellphone'],
|
|
|
|
'other_talk_limit': ['Other Talk limit', MINUTES, 'mdi:cellphone'],
|
|
|
|
'other_talk_remaining': ['Other Talk remaining', MINUTES, 'mdi:cellphone'],
|
2017-02-15 20:21:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_MONITORED_VARIABLES):
|
|
|
|
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-12-29 17:33:11 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
2017-02-15 20:21:38 +00:00
|
|
|
"""Set up the Fido sensor."""
|
|
|
|
username = config.get(CONF_USERNAME)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
|
|
|
|
2017-12-29 17:33:11 +00:00
|
|
|
httpsession = hass.helpers.aiohttp_client.async_get_clientsession()
|
|
|
|
fido_data = FidoData(username, password, httpsession)
|
|
|
|
ret = yield from fido_data.async_update()
|
|
|
|
if ret is False:
|
|
|
|
return
|
2017-02-15 20:21:38 +00:00
|
|
|
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
|
|
|
|
sensors = []
|
2017-04-05 15:18:02 +00:00
|
|
|
for number in fido_data.client.get_phone_numbers():
|
|
|
|
for variable in config[CONF_MONITORED_VARIABLES]:
|
|
|
|
sensors.append(FidoSensor(fido_data, variable, name, number))
|
2017-02-15 20:21:38 +00:00
|
|
|
|
2017-12-29 17:33:11 +00:00
|
|
|
async_add_devices(sensors, True)
|
2017-02-15 20:21:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FidoSensor(Entity):
|
|
|
|
"""Implementation of a Fido sensor."""
|
|
|
|
|
2017-04-05 15:18:02 +00:00
|
|
|
def __init__(self, fido_data, sensor_type, name, number):
|
2017-02-15 20:21:38 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
self.client_name = name
|
2017-04-05 15:18:02 +00:00
|
|
|
self._number = number
|
2017-02-15 20:21:38 +00:00
|
|
|
self.type = sensor_type
|
|
|
|
self._name = SENSOR_TYPES[sensor_type][0]
|
|
|
|
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
|
|
|
self._icon = SENSOR_TYPES[sensor_type][2]
|
|
|
|
self.fido_data = fido_data
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2017-04-05 15:18:02 +00:00
|
|
|
return '{} {} {}'.format(self.client_name, self._number, self._name)
|
2017-02-15 20:21:38 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
|
|
|
return self._icon
|
|
|
|
|
2017-04-05 15:18:02 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the sensor."""
|
|
|
|
return {
|
|
|
|
'number': self._number,
|
|
|
|
}
|
|
|
|
|
2017-12-29 17:33:11 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_update(self):
|
2017-02-15 20:21:38 +00:00
|
|
|
"""Get the latest data from Fido and update the state."""
|
2017-12-29 17:33:11 +00:00
|
|
|
yield from self.fido_data.async_update()
|
2017-04-13 05:42:48 +00:00
|
|
|
if self.type == 'balance':
|
2017-04-05 15:18:02 +00:00
|
|
|
if self.fido_data.data.get(self.type) is not None:
|
2017-02-15 20:21:38 +00:00
|
|
|
self._state = round(self.fido_data.data[self.type], 2)
|
2017-04-05 15:18:02 +00:00
|
|
|
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)
|
2017-02-15 20:21:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FidoData(object):
|
|
|
|
"""Get data from Fido."""
|
|
|
|
|
2017-12-29 17:33:11 +00:00
|
|
|
def __init__(self, username, password, httpsession):
|
2017-02-15 20:21:38 +00:00
|
|
|
"""Initialize the data object."""
|
|
|
|
from pyfido import FidoClient
|
2017-12-29 17:33:11 +00:00
|
|
|
self.client = FidoClient(username, password,
|
|
|
|
REQUESTS_TIMEOUT, httpsession)
|
2017-02-15 20:21:38 +00:00
|
|
|
self.data = {}
|
|
|
|
|
2017-12-29 17:33:11 +00:00
|
|
|
@asyncio.coroutine
|
2017-02-15 20:21:38 +00:00
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
2017-12-29 17:33:11 +00:00
|
|
|
def async_update(self):
|
2017-02-15 20:21:38 +00:00
|
|
|
"""Get the latest data from Fido."""
|
|
|
|
from pyfido.client import PyFidoError
|
|
|
|
try:
|
2017-12-29 17:33:11 +00:00
|
|
|
yield from self.client.fetch_data()
|
|
|
|
except PyFidoError as exp:
|
|
|
|
_LOGGER.error("Error on receive last Fido data: %s", exp)
|
|
|
|
return False
|
2017-02-15 20:21:38 +00:00
|
|
|
# Update data
|
|
|
|
self.data = self.client.get_data()
|
2017-12-29 17:33:11 +00:00
|
|
|
return True
|