core/homeassistant/components/vilfo/sensor.py

95 lines
2.6 KiB
Python
Raw Normal View History

Add initial version of Vilfo Router integration (#31177) * Initial implementation of Vilfo router integration. This commit is a combination of several commits, with commit messages in bullet form below. * Added additional files to Vilfo integration. * Added generated files. * Fixed alphabetic order in generated config_flows. * Continued implementation of config flow for Vilfo integration. * Continued work on config_flow for Vilfo. * Updated requirements in manifest for Vilfo Router integration. * Some strings added to Vilfo Router integration. * Vilfo Router integration updated with sensor support. * Code style cleanup. * Additional cleanup of config flow. * Added additional UI strings for Vilfo Router * Updated tests of config flow and fixed formatting * Updated requirement upon vilfo-api-client. * Sensor refactoring including support for icons * Code style changes for Vilfo Router integration * Code cleanup * Fixed linting issues in Vilfo Router integration * Fixed import order in test for Vilfo integration. * Updates to Vilfo Router integration based on feedback Based on the feedback received, updates have been made to the Vilfo Router integration. A couple of the points mentioned have not been addressed yet, since the appropriate action has not yet been determined. These are: * https://github.com/home-assistant/home-assistant/pull/31177#discussion_r371124477 * https://github.com/home-assistant/home-assistant/pull/31177#discussion_r371202896 This commit consists of: * Removed unused folder/submodule * Fixes to __init__ * Fixes to config_flow * Fixes to const * Refactored sensors and applied fixes * Fix issue with wrong exception type in config flow * Updated tests for Vilfo integration config_flow * Updated dependency upon vilfo-api-client to improve testability * Import order fixes in test * Use constants instead of strings in tests * Updated the VilfoRouterData class to only use the hostname as unique_id when it is the default one (admin.vilfo.com). * Refactored based on feedback during review. * Changes to constant names, * Blocking IO separated to executor job, * Data for uptime sensor changed from being computed to being a timestamp, * Started refactoring uptime sensor in terms of naming and unit. * Updated constants for boot time (previously uptime) sensor. * Refactored test of Vilfo config flow to avoid patching code under test. * UI naming fixes and better exception handling. * Removed unused exception class. * Various changes to Vilfo Router integration. * Removed unit of measurement for boot time sensor, * Added support for a sensor not having a unit, * Updated the config_flow to handle when the integration is already configured, * Updated tests to avoid mocking the code under test and also to cover the aforementioned changes. * Exception handling in Vilfo Router config flow refactored to be more readable. * Refactored constant usage, fixed sensor availability and fix API client library doing I/O in async context. * Updated signature with hass first * Update call to constructor with changed order of arguments
2020-02-12 18:11:15 +00:00
"""Support for Vilfo Router sensors."""
from homeassistant.helpers.entity import Entity
from .const import (
ATTR_API_DATA_FIELD,
ATTR_DEVICE_CLASS,
ATTR_ICON,
ATTR_LABEL,
ATTR_UNIT,
DOMAIN,
ROUTER_DEFAULT_MODEL,
ROUTER_DEFAULT_NAME,
ROUTER_MANUFACTURER,
SENSOR_TYPES,
)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Add Vilfo Router entities from a config_entry."""
vilfo = hass.data[DOMAIN][config_entry.entry_id]
sensors = []
for sensor_type in SENSOR_TYPES:
sensors.append(VilfoRouterSensor(sensor_type, vilfo))
async_add_entities(sensors, True)
class VilfoRouterSensor(Entity):
"""Define a Vilfo Router Sensor."""
def __init__(self, sensor_type, api):
"""Initialize."""
self.api = api
self.sensor_type = sensor_type
self._device_info = {
"identifiers": {(DOMAIN, api.host, api.mac_address)},
"name": ROUTER_DEFAULT_NAME,
"manufacturer": ROUTER_MANUFACTURER,
"model": ROUTER_DEFAULT_MODEL,
"sw_version": api.firmware_version,
}
self._unique_id = f"{self.api.unique_id}_{self.sensor_type}"
self._state = None
@property
def available(self):
"""Return whether the sensor is available or not."""
return self.api.available
@property
def device_info(self):
"""Return the device info."""
return self._device_info
@property
def device_class(self):
"""Return the device class."""
return SENSOR_TYPES[self.sensor_type].get(ATTR_DEVICE_CLASS)
@property
def icon(self):
"""Return the icon for the sensor."""
return SENSOR_TYPES[self.sensor_type][ATTR_ICON]
@property
def name(self):
"""Return the name of the sensor."""
parent_device_name = self._device_info["name"]
sensor_name = SENSOR_TYPES[self.sensor_type][ATTR_LABEL]
return f"{parent_device_name} {sensor_name}"
@property
def state(self):
"""Return the state."""
return self._state
@property
def unique_id(self):
"""Return a unique_id for this entity."""
return self._unique_id
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity."""
return SENSOR_TYPES[self.sensor_type].get(ATTR_UNIT)
async def async_update(self):
"""Update the router data."""
await self.api.async_update()
self._state = self.api.data.get(
SENSOR_TYPES[self.sensor_type][ATTR_API_DATA_FIELD]
)