def _try_connect_and_fetch_basic_info(host, token): """Attempt to connect and call the ping endpoint and, if successful, fetch basic information.""" # Perform the ping. This doesn't validate authentication. controller = VilfoClient(host=host, token=token) result = {"type": None, "data": {}} try: controller.ping() except VilfoException: result["type"] = RESULT_CANNOT_CONNECT result["data"] = CannotConnect return result # Perform a call that requires authentication. try: controller.get_board_information() except VilfoAuthenticationException: result["type"] = RESULT_INVALID_AUTH result["data"] = InvalidAuth return result if controller.mac: result["data"][CONF_ID] = controller.mac result["data"][CONF_MAC] = controller.mac else: result["data"][CONF_ID] = host result["data"][CONF_MAC] = None result["type"] = RESULT_SUCCESS return result
class VilfoRouterData: """Define an object to hold sensor data.""" def __init__(self, hass, host, access_token): """Initialize.""" self._vilfo = VilfoClient(host, access_token) self.hass = hass self.host = host self.available = False self.firmware_version = None self.mac_address = self._vilfo.mac self.data = {} self._unavailable_logged = False @property def unique_id(self): """Get the unique_id for the Vilfo Router.""" if self.mac_address: return self.mac_address if self.host == ROUTER_DEFAULT_HOST: return self.host return self.host def _fetch_data(self): board_information = self._vilfo.get_board_information() load = self._vilfo.get_load() return { "board_information": board_information, "load": load, } @Throttle(DEFAULT_SCAN_INTERVAL) async def async_update(self): """Update data using calls to VilfoClient library.""" try: data = await self.hass.async_add_executor_job(self._fetch_data) self.firmware_version = data["board_information"]["version"] self.data[ATTR_BOOT_TIME] = data["board_information"]["bootTime"] self.data[ATTR_LOAD] = data["load"] self.available = True except VilfoException as error: if not self._unavailable_logged: _LOGGER.error( "Could not fetch data from %s, error: %s", self.host, error ) self._unavailable_logged = True self.available = False return if self.available and self._unavailable_logged: _LOGGER.info("Vilfo Router %s is available again", self.host) self._unavailable_logged = False
def __init__(self, opp, host, access_token): """Initialize.""" self._vilfo = VilfoClient(host, access_token) self.opp = opp self.host = host self.available = False self.firmware_version = None self.mac_address = self._vilfo.mac self.data = {} self._unavailable_logged = False