class PyNUTData: """Stores the data retrieved from NUT. For each entity to use, acts as the single point responsible for fetching updates from the server. """ def __init__(self, host, port, alias, username, password): """Initialize the data object.""" self._host = host self._alias = alias # Establish client with persistent=False to open/close connection on # each update call. This is more reliable with async. self._client = PyNUTClient(self._host, port, username, password, 5, False) self.ups_list = None self._status = None @property def status(self): """Get latest update if throttle allows. Return status.""" return self._status @property def name(self): """Return the name of the ups.""" return self._alias def _get_alias(self): """Get the ups alias from NUT.""" try: ups_list = self._client.list_ups() except PyNUTError as err: _LOGGER.error("Failure getting NUT ups alias, %s", err) return None if not ups_list: _LOGGER.error("Empty list while getting NUT ups aliases") return None self.ups_list = ups_list return list(ups_list)[0] def _get_status(self): """Get the ups status from NUT.""" if self._alias is None: self._alias = self._get_alias() try: return self._client.list_vars(self._alias) except (PyNUTError, ConnectionResetError) as err: _LOGGER.debug("Error getting NUT vars for host %s: %s", self._host, err) return None def update(self, **kwargs): """Fetch the latest status from NUT.""" self._status = self._get_status()
class PyNUTData(object): """Stores the data retrieved from NUT. For each entity to use, acts as the single point responsible for fetching updates from the server. """ def __init__(self, host, port, alias, username, password): """Initialize the data object.""" from pynut2.nut2 import PyNUTClient, PyNUTError self._host = host self._port = port self._alias = alias self._username = username self._password = password self.pynuterror = PyNUTError # Establish client with persistent=False to open/close connection on # each update call. This is more reliable with async. self._client = PyNUTClient(self._host, self._port, self._username, self._password, 5, False) self._status = None @property def status(self): """Get latest update if throttle allows. Return status.""" self.update() return self._status def _get_alias(self): """Get the ups alias from NUT.""" try: return next(iter(self._client.list_ups())) except self.pynuterror as err: _LOGGER.error("Failure getting NUT ups alias, %s", err) return None def _get_status(self): """Get the ups status from NUT.""" if self._alias is None: self._alias = self._get_alias() try: return self._client.list_vars(self._alias) except (self.pynuterror, ConnectionResetError) as err: _LOGGER.debug( "Error getting NUT vars for host %s: %s", self._host, err) return None @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self, **kwargs): """Fetch the latest status from NUT.""" self._status = self._get_status()
class PyNUTData: """Stores the data retrieved from NUT. For each entity to use, acts as the single point responsible for fetching updates from the server. """ def __init__(self, host, port, alias, username, password): """Initialize the data object.""" from pynut2.nut2 import PyNUTClient, PyNUTError self._host = host self._port = port self._alias = alias self._username = username self._password = password self.pynuterror = PyNUTError # Establish client with persistent=False to open/close connection on # each update call. This is more reliable with async. self._client = PyNUTClient(self._host, self._port, self._username, self._password, 5, False) self._status = None @property def status(self): """Get latest update if throttle allows. Return status.""" self.update() return self._status def _get_alias(self): """Get the ups alias from NUT.""" try: return next(iter(self._client.list_ups())) except self.pynuterror as err: _LOGGER.error("Failure getting NUT ups alias, %s", err) return None def _get_status(self): """Get the ups status from NUT.""" if self._alias is None: self._alias = self._get_alias() try: return self._client.list_vars(self._alias) except (self.pynuterror, ConnectionResetError) as err: _LOGGER.debug( "Error getting NUT vars for host %s: %s", self._host, err) return None @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self, **kwargs): """Fetch the latest status from NUT.""" self._status = self._get_status()
class PyNUTData: """Stores the data retrieved from NUT. For each entity to use, acts as the single point responsible for fetching updates from the server. """ def __init__(self, host, port, alias, username, password): """Initialize the data object.""" self._host = host self._alias = alias # Establish client with persistent=False to open/close connection on # each update call. This is more reliable with async. self._client = PyNUTClient(self._host, port, username, password, 5, False) self.ups_list = None self._status = None self._device_info = None @property def status(self): """Get latest update if throttle allows. Return status.""" return self._status @property def name(self): """Return the name of the ups.""" return self._alias @property def device_info(self): """Return the device info for the ups.""" return self._device_info or {} def _get_alias(self): """Get the ups alias from NUT.""" try: ups_list = self._client.list_ups() except PyNUTError as err: _LOGGER.error("Failure getting NUT ups alias, %s", err) return None if not ups_list: _LOGGER.error("Empty list while getting NUT ups aliases") return None self.ups_list = ups_list return list(ups_list)[0] def _get_device_info(self): """Get the ups device info from NUT.""" if not self._status: return None manufacturer = _manufacturer_from_status(self._status) model = _model_from_status(self._status) firmware = _firmware_from_status(self._status) device_info = {} if model: device_info[ATTR_MODEL] = model if manufacturer: device_info[ATTR_MANUFACTURER] = manufacturer if firmware: device_info[ATTR_SW_VERSION] = firmware return device_info def _get_status(self): """Get the ups status from NUT.""" if self._alias is None: self._alias = self._get_alias() try: return self._client.list_vars(self._alias) except (PyNUTError, ConnectionResetError) as err: _LOGGER.debug("Error getting NUT vars for host %s: %s", self._host, err) return None def update(self): """Fetch the latest status from NUT.""" self._status = self._get_status() if self._device_info is None: self._device_info = self._get_device_info()
class PyNUTData: """Stores the data retrieved from NUT. For each entity to use, acts as the single point responsible for fetching updates from the server. """ def __init__( self, host: str, port: int, alias: str | None, username: str | None, password: str | None, ) -> None: """Initialize the data object.""" self._host = host self._alias = alias # Establish client with persistent=False to open/close connection on # each update call. This is more reliable with async. self._client = PyNUTClient(self._host, port, username, password, 5, False) self.ups_list: dict[str, str] | None = None self._status: dict[str, str] | None = None self._device_info: NUTDeviceInfo | None = None @property def status(self) -> dict[str, str] | None: """Get latest update if throttle allows. Return status.""" return self._status @property def name(self) -> str: """Return the name of the ups.""" return self._alias or f"Nut-{self._host}" @property def device_info(self) -> NUTDeviceInfo: """Return the device info for the ups.""" return self._device_info or NUTDeviceInfo() def _get_alias(self) -> str | None: """Get the ups alias from NUT.""" try: ups_list: dict[str, str] = self._client.list_ups() except PyNUTError as err: _LOGGER.error("Failure getting NUT ups alias, %s", err) return None if not ups_list: _LOGGER.error("Empty list while getting NUT ups aliases") return None self.ups_list = ups_list return list(ups_list)[0] def _get_device_info(self) -> NUTDeviceInfo | None: """Get the ups device info from NUT.""" if not self._status: return None manufacturer = _manufacturer_from_status(self._status) model = _model_from_status(self._status) firmware = _firmware_from_status(self._status) device_info = NUTDeviceInfo(manufacturer, model, firmware) return device_info def _get_status(self) -> dict[str, str] | None: """Get the ups status from NUT.""" if self._alias is None: self._alias = self._get_alias() try: status: dict[str, str] = self._client.list_vars(self._alias) except (PyNUTError, ConnectionResetError) as err: _LOGGER.debug("Error getting NUT vars for host %s: %s", self._host, err) return None return status def update(self) -> None: """Fetch the latest status from NUT.""" self._status = self._get_status() if self._device_info is None: self._device_info = self._get_device_info()