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
|
|
|
|
"""
|
2021-08-23 18:56:15 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-02-15 20:21:38 +00:00
|
|
|
from datetime import timedelta
|
2019-12-08 11:20:53 +00:00
|
|
|
import logging
|
2017-02-15 20:21:38 +00:00
|
|
|
|
2019-12-08 11:20:53 +00:00
|
|
|
from pyfido import FidoClient
|
|
|
|
from pyfido.client import PyFidoError
|
2017-02-15 20:21:38 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-08-23 18:56:15 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2017-02-15 20:21:38 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_MONITORED_VARIABLES,
|
2019-12-08 11:20:53 +00:00
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_USERNAME,
|
2020-02-13 16:52:58 +00:00
|
|
|
DATA_KILOBITS,
|
2020-02-23 20:09:24 +00:00
|
|
|
TIME_MINUTES,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-12-08 11:20:53 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-02-15 20:21:38 +00:00
|
|
|
from homeassistant.util import Throttle
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-09-07 06:48:58 +00:00
|
|
|
PRICE = "CAD"
|
|
|
|
MESSAGES = "messages"
|
2017-02-15 20:21:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Fido"
|
2017-02-15 20:21:38 +00:00
|
|
|
|
|
|
|
REQUESTS_TIMEOUT = 15
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15)
|
|
|
|
|
2021-08-23 18:56:15 +00:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="fido_dollar",
|
|
|
|
name="Fido dollar",
|
|
|
|
native_unit_of_measurement=PRICE,
|
2021-09-13 17:29:38 +00:00
|
|
|
icon="mdi:cash",
|
2021-08-23 18:56:15 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="balance",
|
|
|
|
name="Balance",
|
|
|
|
native_unit_of_measurement=PRICE,
|
2021-09-13 17:29:38 +00:00
|
|
|
icon="mdi:cash",
|
2021-08-23 18:56:15 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="data_used",
|
|
|
|
name="Data used",
|
|
|
|
native_unit_of_measurement=DATA_KILOBITS,
|
|
|
|
icon="mdi:download",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="data_limit",
|
|
|
|
name="Data limit",
|
|
|
|
native_unit_of_measurement=DATA_KILOBITS,
|
|
|
|
icon="mdi:download",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="data_remaining",
|
|
|
|
name="Data remaining",
|
|
|
|
native_unit_of_measurement=DATA_KILOBITS,
|
|
|
|
icon="mdi:download",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="text_used",
|
|
|
|
name="Text used",
|
|
|
|
native_unit_of_measurement=MESSAGES,
|
|
|
|
icon="mdi:message-text",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="text_limit",
|
|
|
|
name="Text limit",
|
|
|
|
native_unit_of_measurement=MESSAGES,
|
|
|
|
icon="mdi:message-text",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="text_remaining",
|
|
|
|
name="Text remaining",
|
|
|
|
native_unit_of_measurement=MESSAGES,
|
|
|
|
icon="mdi:message-text",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="mms_used",
|
|
|
|
name="MMS used",
|
|
|
|
native_unit_of_measurement=MESSAGES,
|
|
|
|
icon="mdi:message-image",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="mms_limit",
|
|
|
|
name="MMS limit",
|
|
|
|
native_unit_of_measurement=MESSAGES,
|
|
|
|
icon="mdi:message-image",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="mms_remaining",
|
|
|
|
name="MMS remaining",
|
|
|
|
native_unit_of_measurement=MESSAGES,
|
|
|
|
icon="mdi:message-image",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="text_int_used",
|
|
|
|
name="International text used",
|
|
|
|
native_unit_of_measurement=MESSAGES,
|
|
|
|
icon="mdi:message-alert",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="text_int_limit",
|
|
|
|
name="International text limit",
|
|
|
|
native_unit_of_measurement=MESSAGES,
|
|
|
|
icon="mdi:message-alert",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="text_int_remaining",
|
|
|
|
name="International remaining",
|
|
|
|
native_unit_of_measurement=MESSAGES,
|
|
|
|
icon="mdi:message-alert",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="talk_used",
|
|
|
|
name="Talk used",
|
|
|
|
native_unit_of_measurement=TIME_MINUTES,
|
|
|
|
icon="mdi:cellphone",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="talk_limit",
|
|
|
|
name="Talk limit",
|
|
|
|
native_unit_of_measurement=TIME_MINUTES,
|
|
|
|
icon="mdi:cellphone",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="talk_remaining",
|
|
|
|
name="Talk remaining",
|
|
|
|
native_unit_of_measurement=TIME_MINUTES,
|
|
|
|
icon="mdi:cellphone",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="other_talk_used",
|
|
|
|
name="Other Talk used",
|
|
|
|
native_unit_of_measurement=TIME_MINUTES,
|
|
|
|
icon="mdi:cellphone",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="other_talk_limit",
|
|
|
|
name="Other Talk limit",
|
|
|
|
native_unit_of_measurement=TIME_MINUTES,
|
|
|
|
icon="mdi:cellphone",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="other_talk_remaining",
|
|
|
|
name="Other Talk remaining",
|
|
|
|
native_unit_of_measurement=TIME_MINUTES,
|
|
|
|
icon="mdi:cellphone",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
2017-02-15 20:21:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_MONITORED_VARIABLES): vol.All(
|
2021-08-23 18:56:15 +00:00
|
|
|
cv.ensure_list, [vol.In(SENSOR_KEYS)]
|
2019-07-31 19:25:30 +00:00
|
|
|
),
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2017-02-15 20:21:38 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-02-15 20:21:38 +00:00
|
|
|
"""Set up the Fido sensor."""
|
2021-08-23 18:56:15 +00:00
|
|
|
username = config[CONF_USERNAME]
|
|
|
|
password = config[CONF_PASSWORD]
|
2017-02-15 20:21:38 +00:00
|
|
|
|
2017-12-29 17:33:11 +00:00
|
|
|
httpsession = hass.helpers.aiohttp_client.async_get_clientsession()
|
|
|
|
fido_data = FidoData(username, password, httpsession)
|
2018-10-01 06:55:43 +00:00
|
|
|
ret = await fido_data.async_update()
|
2017-12-29 17:33:11 +00:00
|
|
|
if ret is False:
|
|
|
|
return
|
2017-02-15 20:21:38 +00:00
|
|
|
|
2021-08-23 18:56:15 +00:00
|
|
|
name = config[CONF_NAME]
|
|
|
|
monitored_variables = config[CONF_MONITORED_VARIABLES]
|
|
|
|
entities = [
|
|
|
|
FidoSensor(fido_data, name, number, description)
|
|
|
|
for number in fido_data.client.get_phone_numbers()
|
|
|
|
for description in SENSOR_TYPES
|
|
|
|
if description.key in monitored_variables
|
|
|
|
]
|
2017-02-15 20:21:38 +00:00
|
|
|
|
2021-08-23 18:56:15 +00:00
|
|
|
async_add_entities(entities, True)
|
2017-02-15 20:21:38 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
class FidoSensor(SensorEntity):
|
2017-02-15 20:21:38 +00:00
|
|
|
"""Implementation of a Fido sensor."""
|
|
|
|
|
2021-08-23 18:56:15 +00:00
|
|
|
def __init__(self, fido_data, name, number, description: SensorEntityDescription):
|
2017-02-15 20:21:38 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-08-23 18:56:15 +00:00
|
|
|
self.entity_description = description
|
2017-02-15 20:21:38 +00:00
|
|
|
self.fido_data = fido_data
|
2021-08-23 18:56:15 +00:00
|
|
|
self._number = number
|
2017-02-15 20:21:38 +00:00
|
|
|
|
2021-08-23 18:56:15 +00:00
|
|
|
self._attr_name = f"{name} {number} {description.name}"
|
2017-02-15 20:21:38 +00:00
|
|
|
|
2017-04-05 15:18:02 +00:00
|
|
|
@property
|
2021-03-11 15:57:47 +00:00
|
|
|
def extra_state_attributes(self):
|
2017-04-05 15:18:02 +00:00
|
|
|
"""Return the state attributes of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return {"number": self._number}
|
2017-04-05 15:18:02 +00:00
|
|
|
|
2018-10-01 06:55:43 +00:00
|
|
|
async def async_update(self):
|
2017-02-15 20:21:38 +00:00
|
|
|
"""Get the latest data from Fido and update the state."""
|
2018-10-01 06:55:43 +00:00
|
|
|
await self.fido_data.async_update()
|
2021-10-17 18:05:11 +00:00
|
|
|
if (sensor_type := self.entity_description.key) == "balance":
|
2021-08-23 18:56:15 +00:00
|
|
|
if self.fido_data.data.get(sensor_type) is not None:
|
|
|
|
self._attr_native_value = round(self.fido_data.data[sensor_type], 2)
|
2017-04-05 15:18:02 +00:00
|
|
|
else:
|
2021-08-23 18:56:15 +00:00
|
|
|
if self.fido_data.data.get(self._number, {}).get(sensor_type) is not None:
|
|
|
|
self._attr_native_value = round(
|
|
|
|
self.fido_data.data[self._number][sensor_type], 2
|
|
|
|
)
|
2017-02-15 20:21:38 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class FidoData:
|
2017-02-15 20:21:38 +00:00
|
|
|
"""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."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
self.client = FidoClient(username, password, REQUESTS_TIMEOUT, httpsession)
|
2017-02-15 20:21:38 +00:00
|
|
|
self.data = {}
|
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
2018-03-10 03:38:51 +00:00
|
|
|
async def async_update(self):
|
2017-02-15 20:21:38 +00:00
|
|
|
"""Get the latest data from Fido."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-02-15 20:21:38 +00:00
|
|
|
try:
|
2018-03-10 03:38:51 +00:00
|
|
|
await self.client.fetch_data()
|
2017-12-29 17:33:11 +00:00
|
|
|
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
|