Skip to content

Commit

Permalink
Add raw sensor state attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Limych committed Apr 24, 2020
1 parent df82281 commit dfa0f16
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
6 changes: 6 additions & 0 deletions custom_components/jq300/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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)
5 changes: 3 additions & 2 deletions custom_components/jq300/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
9 changes: 7 additions & 2 deletions custom_components/jq300/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand All @@ -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)
2 changes: 1 addition & 1 deletion tracker.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit dfa0f16

Please sign in to comment.