From dfa0f166cd7692f6bc40e5bc5deaff294cee5882 Mon Sep 17 00:00:00 2001 From: Andrey Khrolenok Date: Fri, 24 Apr 2020 16:37:36 +0300 Subject: [PATCH] Add raw sensor state attribute --- custom_components/jq300/__init__.py | 6 ++++++ custom_components/jq300/const.py | 5 +++-- custom_components/jq300/sensor.py | 9 +++++++-- tracker.json | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/custom_components/jq300/__init__.py b/custom_components/jq300/__init__.py index 86977c8..31fbd57 100644 --- a/custom_components/jq300/__init__.py +++ b/custom_components/jq300/__init__.py @@ -103,6 +103,7 @@ def __init__(self, username, password, receive_tvoc_in_ppb=False, self._session = requests.session() self._devices = {} self._sensors = {} + self._sensors_raw = {} self._units = {} for sensor_id, data in SENSORS.items(): @@ -308,6 +309,7 @@ def _fetch_sensors(self, device_id, ts_now, force=False) -> bool: res[sensor_id] = int(res[sensor_id]) self._sensors[device_id][ts_now] = res + self._sensors_raw[device_id] = res return True def get_sensors(self, device_id) -> Optional[dict]: @@ -338,3 +340,7 @@ def get_sensors(self, device_id) -> Optional[dict]: 0 if isinstance(res[sensor_id], int) else 3 ) return res + + def get_sensors_raw(self, device_id) -> Optional[dict]: + """Get raw values of states of available sensors for device.""" + return self._sensors_raw.get(device_id) diff --git a/custom_components/jq300/const.py b/custom_components/jq300/const.py index 659179c..a0cbd9b 100644 --- a/custom_components/jq300/const.py +++ b/custom_components/jq300/const.py @@ -18,7 +18,7 @@ # Base component constants DOMAIN = "jq300" -VERSION = "0.7.2" +VERSION = "0.7.3" ISSUE_URL = "https://github.com/Limych/ha-jq300/issues" ATTRIBUTION = None DATA_JQ300 = 'jq300' @@ -98,6 +98,7 @@ ATTR_DEVICE_ID = 'device_id' ATTR_DEVICE_BRAND = "device_brand" ATTR_DEVICE_MODEL = "device_model" +ATTR_RAW_STATE = 'raw_state' UPDATE_MIN_TIME = 20 # seconds -SENSORS_FILTER_TIME = 300 # seconds +SENSORS_FILTER_TIME = 900 # seconds diff --git a/custom_components/jq300/sensor.py b/custom_components/jq300/sensor.py index f555077..98cde15 100644 --- a/custom_components/jq300/sensor.py +++ b/custom_components/jq300/sensor.py @@ -19,7 +19,7 @@ from custom_components.jq300 import JqController from .const import DATA_JQ300, SENSORS, ATTR_DEVICE_ID, ATTR_DEVICE_BRAND, \ - ATTR_DEVICE_MODEL + ATTR_DEVICE_MODEL, ATTR_RAW_STATE _LOGGER = logging.getLogger(__name__) @@ -80,6 +80,7 @@ def __init__(self, hass, controller, device, sensor_id, sensor_state, self._name = "{0} {1}".format( device['pt_name'], SENSORS.get(sensor_id)[0]) self._state = sensor_state + self._state_raw = sensor_state self._units = controller.units[sensor_id] self._icon = SENSORS.get(sensor_id)[2] self._unique_id = '{}-{}-{}'.format( @@ -121,6 +122,7 @@ def device_state_attributes(self): ATTR_DEVICE_BRAND: self._device_brand, ATTR_DEVICE_MODEL: self._device_model, ATTR_DEVICE_ID: self._device_id, + ATTR_RAW_STATE: self._state_raw, } return attrs @@ -144,4 +146,7 @@ def update(self): ret = self._controller.get_sensors(self._device_id) if ret: self._state = ret[self._sensor_id] - _LOGGER.debug('Update state: %s = %s', self.entity_id, self._state) + self._state_raw = self._controller.get_sensors_raw( + self._device_id)[self._sensor_id] + _LOGGER.debug('Update state: %s = %s (%s)', self.entity_id, + self._state, self._state_raw) diff --git a/tracker.json b/tracker.json index cfab920..488bc65 100644 --- a/tracker.json +++ b/tracker.json @@ -1,6 +1,6 @@ { "jq300": { - "version": "0.7.2", + "version": "0.7.3", "local_location": "/custom_components/jq300/__init__.py", "remote_location": "https://raw.githubusercontent.com/Limych/ha-jq300/master/custom_components/jq300/__init__.py", "visit_repo": "https://github.com/Limych/ha-jq300",