"""Hive Device Attribute Module.""" # pylint: skip-file import logging from .helper.const import HIVETOHA _LOGGER = logging.getLogger(__name__) class HiveAttributes: """Device Attributes Code.""" def __init__(self, session: object = None): """Initialise attributes. Args: session (object, optional): Session to interact with hive account. Defaults to None. """ self.session = session self.type = "Attribute" def stateAttributes(self, n_id: str, _type: str): """Get HA State Attributes. Args: n_id (str): The id of the device. _type (str): The device type. Returns: dict: Set of attributes. """ attr = {} if n_id in self.session.data.products or n_id in self.session.data.devices: attr.update({"available": (self.onlineOffline(n_id))}) if n_id in self.session.config.battery: battery = self.getBattery(n_id) if battery is not None: attr.update({"battery": str(battery) + "%"}) if n_id in self.session.config.mode: attr.update({"mode": (self.getMode(n_id))}) return attr def onlineOffline(self, n_id: str): """Check if device is online. Args: n_id (str): The id of the device. Returns: boolean: True/False if device online. """ state = None try: data = self.session.data.devices[n_id] state = data["props"]["online"] except KeyError as e: _LOGGER.error(e) return state def getMode(self, n_id: str): """Get sensor mode. Args: n_id (str): The id of the device Returns: str: The mode of the device. """ state = None final = None try: data = self.session.data.products[n_id] state = data["state"]["mode"] final = HIVETOHA[self.type].get(state, state) except KeyError as e: _LOGGER.error(e) return final def getBattery(self, n_id: str): """Get device battery level. Args: n_id (str): The id of the device. Returns: str: Battery level of device. """ state = None final = None try: data = self.session.data.devices[n_id] state = data["props"]["battery"] final = state self.session.helper.errorCheck(n_id, self.type, state) except KeyError as e: _LOGGER.error(e) return final