diff --git a/.coveragerc b/.coveragerc index a4b27469039639684feb5f82231730e021f9175d..2d4fb3f81a7c40966efb067dd2b5b8d18083f5bb 100644 --- a/.coveragerc +++ b/.coveragerc @@ -793,6 +793,7 @@ omit = homeassistant/components/sensor/hp_ilo.py homeassistant/components/sensor/htu21d.py homeassistant/components/sensor/upnp.py + homeassistant/components/sensor/iliad_italy.py homeassistant/components/sensor/imap_email_content.py homeassistant/components/sensor/imap.py homeassistant/components/sensor/influxdb.py diff --git a/homeassistant/components/sensor/iliad_italy.py b/homeassistant/components/sensor/iliad_italy.py new file mode 100644 index 0000000000000000000000000000000000000000..1e1e5077e8070278de206dc6dad8645f48780f58 --- /dev/null +++ b/homeassistant/components/sensor/iliad_italy.py @@ -0,0 +1,119 @@ +""" +Sensor to get Iliad Italy personal data. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/sensor.iliad_italy/ +""" +from datetime import timedelta +import logging + +import voluptuous as vol + +from homeassistant.components.sensor import PLATFORM_SCHEMA +from homeassistant.const import ATTR_ATTRIBUTION, CONF_PASSWORD, CONF_USERNAME +from homeassistant.helpers.aiohttp_client import async_get_clientsession +import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.entity import Entity +import homeassistant.util.dt as dt_util + +REQUIREMENTS = ['aioiliad==0.1.1'] + +_LOGGER = logging.getLogger(__name__) + +ATTRIBUTION = "Data provided by Iliad Italy" + +ICON = 'mdi:phone' + +SCAN_INTERVAL = timedelta(minutes=10) + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_USERNAME): cv.string, + vol.Required(CONF_PASSWORD): cv.string, +}) + + +async def async_setup_platform( + hass, conf, async_add_entities, discovery_info=None): + """Set up the Iliad Italy sensor platform.""" + from aioiliad import Iliad + iliad = Iliad(conf[CONF_USERNAME], conf[CONF_PASSWORD], + async_get_clientsession(hass), hass.loop) + await iliad.login() + + if not iliad.is_logged_in(): + _LOGGER.error("Check username and password") + return + + async_add_entities([IliadSensor(iliad)], True) + + +class IliadSensor(Entity): + """Representation of a Iliad Italy Sensor.""" + + def __init__(self, iliad): + """Initialize the Iliad Italy sensor.""" + from aioiliad.IliadData import IliadData + self._iliad = iliad + self._iliaddata = IliadData(self._iliad) + self._data = None + self._state = None + + @property + def name(self): + """Return the name of the sensor.""" + return "Iliad {}".format(self._data['info']['utente']) + + @property + def icon(self): + """Return the icon of the sensor.""" + return ICON + + @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 the sensor.""" + return '€' + + @property + def device_state_attributes(self): + """Return the state attributes of the sensor.""" + attr = { + ATTR_ATTRIBUTION: ATTRIBUTION, + 'next_renewal': + dt_util.utc_from_timestamp( + self._data['info']['rinnovo']).isoformat(), + 'italy_sent_sms': self._data['italy']['sms'], + 'italy_over_plan_sms': self._data['italy']['sms_extra'], + 'italy_sent_mms': self._data['italy']['mms'], + 'italy_over_plan_mms': self._data['italy']['mms_extra'], + 'italy_calls_seconds': self._data['italy']['chiamate'], + 'italy_over_plan_calls': self._data['italy']['chiamate_extra'], + 'italy_data': self._data['italy']['internet'], + 'italy_data_max': self._data['italy']['internet_max'], + 'italy_data_over_plan': self._data['italy']['internet_over'], + + 'abroad_sent_sms': self._data['estero']['sms'], + 'abroad_over_plan_sms': self._data['estero']['sms_extra'], + 'abroad_sent_mms': self._data['estero']['mms'], + 'abroad_over_plan_mms': self._data['estero']['mms_extra'], + 'abroad_calls_seconds': self._data['estero']['chiamate'], + 'abroad_over_plan_calls': self._data['estero']['chiamate_extra'], + 'abroad_data': self._data['estero']['internet'], + 'abroad_data_max': self._data['estero']['internet_max'], + 'abroad_data_over_plan': self._data['estero']['internet_over'], + } + return attr + + async def async_update(self): + """Update device state.""" + await self._iliaddata.update() + self._data = { + 'italy': self._iliaddata.get_italy(), + 'estero': self._iliaddata.get_estero(), + 'info': self._iliaddata.get_general_info() + } + self._state = self._data['info']['credito'].replace('€', '') diff --git a/requirements_all.txt b/requirements_all.txt index 9732bc8f0b93024ca72213f32c0d3089fb2e3079..32ce190532067ef55b748d459795c86e359607c0 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -114,6 +114,9 @@ aiohttp_cors==0.7.0 # homeassistant.components.hue aiohue==1.8.0 +# homeassistant.components.sensor.iliad_italy +aioiliad==0.1.1 + # homeassistant.components.sensor.imap aioimaplib==0.7.13