diff --git a/homeassistant/components/fido/sensor.py b/homeassistant/components/fido/sensor.py index 0723e0979670..0e61b5809020 100644 --- a/homeassistant/components/fido/sensor.py +++ b/homeassistant/components/fido/sensor.py @@ -4,6 +4,8 @@ Support for Fido. Get data from 'Usage Summary' page: https://www.fido.ca/pages/#/my-account/wireless """ +from __future__ import annotations + from datetime import timedelta import logging @@ -11,7 +13,11 @@ from pyfido import FidoClient from pyfido.client import PyFidoError import voluptuous as vol -from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity +from homeassistant.components.sensor import ( + PLATFORM_SCHEMA, + SensorEntity, + SensorEntityDescription, +) from homeassistant.const import ( CONF_MONITORED_VARIABLES, CONF_NAME, @@ -33,33 +39,135 @@ DEFAULT_NAME = "Fido" REQUESTS_TIMEOUT = 15 MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15) -SENSOR_TYPES = { - "fido_dollar": ["Fido dollar", PRICE, "mdi:cash-usd"], - "balance": ["Balance", PRICE, "mdi:cash-usd"], - "data_used": ["Data used", DATA_KILOBITS, "mdi:download"], - "data_limit": ["Data limit", DATA_KILOBITS, "mdi:download"], - "data_remaining": ["Data remaining", DATA_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"], - "text_int_used": ["International text used", MESSAGES, "mdi:message-alert"], - "text_int_limit": ["International text limit", MESSAGES, "mdi:message-alert"], - "text_int_remaining": ["International remaining", MESSAGES, "mdi:message-alert"], - "talk_used": ["Talk used", TIME_MINUTES, "mdi:cellphone"], - "talk_limit": ["Talk limit", TIME_MINUTES, "mdi:cellphone"], - "talk_remaining": ["Talk remaining", TIME_MINUTES, "mdi:cellphone"], - "other_talk_used": ["Other Talk used", TIME_MINUTES, "mdi:cellphone"], - "other_talk_limit": ["Other Talk limit", TIME_MINUTES, "mdi:cellphone"], - "other_talk_remaining": ["Other Talk remaining", TIME_MINUTES, "mdi:cellphone"], -} +SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( + SensorEntityDescription( + key="fido_dollar", + name="Fido dollar", + native_unit_of_measurement=PRICE, + icon="mdi:cash-usd", + ), + SensorEntityDescription( + key="balance", + name="Balance", + native_unit_of_measurement=PRICE, + icon="mdi:cash-usd", + ), + 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] PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { vol.Required(CONF_MONITORED_VARIABLES): vol.All( - cv.ensure_list, [vol.In(SENSOR_TYPES)] + cv.ensure_list, [vol.In(SENSOR_KEYS)] ), vol.Required(CONF_USERNAME): cv.string, vol.Required(CONF_PASSWORD): cv.string, @@ -70,8 +178,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the Fido sensor.""" - username = config.get(CONF_USERNAME) - password = config.get(CONF_PASSWORD) + username = config[CONF_USERNAME] + password = config[CONF_PASSWORD] httpsession = hass.helpers.aiohttp_client.async_get_clientsession() fido_data = FidoData(username, password, httpsession) @@ -79,49 +187,28 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= if ret is False: return - name = config.get(CONF_NAME) + 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 + ] - sensors = [] - for number in fido_data.client.get_phone_numbers(): - for variable in config[CONF_MONITORED_VARIABLES]: - sensors.append(FidoSensor(fido_data, variable, name, number)) - - async_add_entities(sensors, True) + async_add_entities(entities, True) class FidoSensor(SensorEntity): """Implementation of a Fido sensor.""" - def __init__(self, fido_data, sensor_type, name, number): + def __init__(self, fido_data, name, number, description: SensorEntityDescription): """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] - self._icon = SENSOR_TYPES[sensor_type][2] + self.entity_description = description self.fido_data = fido_data - self._state = None + self._number = number - @property - def name(self): - """Return the name of the sensor.""" - return f"{self.client_name} {self._number} {self._name}" - - @property - def native_value(self): - """Return the state of the sensor.""" - return self._state - - @property - def native_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 + self._attr_name = f"{name} {number} {description.name}" @property def extra_state_attributes(self): @@ -131,13 +218,15 @@ class FidoSensor(SensorEntity): async def async_update(self): """Get the latest data from Fido and update the state.""" await self.fido_data.async_update() - if self.type == "balance": - if self.fido_data.data.get(self.type) is not None: - self._state = round(self.fido_data.data[self.type], 2) + sensor_type = self.entity_description.key + if sensor_type == "balance": + if self.fido_data.data.get(sensor_type) is not None: + self._attr_native_value = round(self.fido_data.data[sensor_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) + 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 + ) class FidoData: diff --git a/tests/components/fido/test_sensor.py b/tests/components/fido/test_sensor.py index 3baaf2e350ce..bcece50f6e45 100644 --- a/tests/components/fido/test_sensor.py +++ b/tests/components/fido/test_sensor.py @@ -66,7 +66,13 @@ async def test_error(hass, caplog): """Test the Fido sensor errors.""" caplog.set_level(logging.ERROR) - config = {} + config = { + "platform": "fido", + "name": "fido", + "username": "myusername", + "password": "password", + "monitored_variables": ["balance", "data_remaining"], + } fake_async_add_entities = MagicMock() with patch("homeassistant.components.fido.sensor.FidoClient", FidoClientMockError): await fido.async_setup_platform(hass, config, fake_async_add_entities)