Skip to content

Commit

Permalink
Merge pull request #29 from nobert/new_ha_unit_system
Browse files Browse the repository at this point in the history
Support HA's new native values, which handles unit conversions
  • Loading branch information
rsnodgrass authored Jul 12, 2022
2 parents bc5e1e7 + 7af5408 commit 4923d40
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 54 deletions.
32 changes: 13 additions & 19 deletions custom_components/sensorpush/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
from homeassistant.const import CONF_NAME, CONF_USERNAME, CONF_PASSWORD, CONF_SCAN_INTERVAL

from .const import (ATTR_BATTERY_VOLTAGE, ATTR_DEVICE_ID, ATTR_OBSERVED_TIME, ATTR_AGE,
ATTR_ATTRIBUTION, ATTRIBUTION, MEASURES, CONF_UNIT_SYSTEM, CONF_MAXIMUM_AGE,
ATTR_ATTRIBUTION, ATTRIBUTION, MEASURES, CONF_MAXIMUM_AGE,
ATTR_ALERT_MIN, ATTR_ALERT_MAX, ATTR_ALERT_ENABLED,
SENSORPUSH_DOMAIN, UNIT_SYSTEM_IMPERIAL, UNIT_SYSTEM_METRIC, UNIT_SYSTEMS)
SENSORPUSH_DOMAIN, UNIT_SYSTEMS)

LOG = logging.getLogger(__name__)

Expand All @@ -44,8 +44,6 @@
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_SCAN_INTERVAL, default=60):
vol.All(vol.Coerce(int), vol.Range(min=MIN_SCAN_INTERVAL_IN_SECONDS)),
vol.Optional(CONF_UNIT_SYSTEM, default=UNIT_SYSTEM_IMPERIAL):
vol.In( UNIT_SYSTEMS.keys() ),
vol.Optional(CONF_MAXIMUM_AGE, default=60): cv.positive_int
})
}, extra=vol.ALLOW_EXTRA
Expand All @@ -56,10 +54,6 @@ def setup(hass, config):
hass.data[SENSORPUSH_DOMAIN] = {}
conf = config[SENSORPUSH_DOMAIN]

unit_system = conf.get(CONF_UNIT_SYSTEM)
hass.data[SENSORPUSH_DOMAIN][CONF_UNIT_SYSTEM] = unit_system
LOG.info(f"Using unit system '{unit_system}'")

username = conf.get(CONF_USERNAME)
password = conf.get(CONF_PASSWORD)

Expand Down Expand Up @@ -100,7 +94,7 @@ def refresh_sensorpush_data(event_time):
dispatcher_send(hass, SIGNAL_SENSORPUSH_UPDATED)
else:
LOG.warn("Unable to fetch latest samples from SensorPush cloud")
except Exception as ex:
except Exception as ex:
LOG.warn(f"Unable to fetch latest samples from SensorPush cloud. Error: {ex}")

# subscribe for notifications that an update should be triggered
Expand All @@ -115,11 +109,10 @@ def refresh_sensorpush_data(event_time):
class SensorPushEntity(RestoreEntity):
"""Base Entity class for SensorPush devices"""

def __init__(self, hass, config, name_suffix, sensor_info, unit_system, measure):
def __init__(self, hass, config, name_suffix, sensor_info, measure):
self.hass = hass

self._field_name = measure
self._unit_system = unit_system
self._sensor_info = sensor_info
self._max_age = 7 * 1440
self._device_id = sensor_info.get('id')
Expand All @@ -137,7 +130,15 @@ def icon(self):
return MEASURES[self._field_name].get('icon') or 'mdi:gauge'

@property
def state(self):
def device_class(self):
return self._field_name

@property
def native_unit_of_measurement(self):
return UNIT_SYSTEMS[self._field_name]

@property
def native_value(self):
return self._state

@property
Expand Down Expand Up @@ -177,13 +178,6 @@ def _update_callback(self):
alert_min = alerts.get("min")
alert_max = alerts.get("max")

# NOTE: The SensorPush API currently does not return units for the min/max
# alert settings and always returns data in Fahrenheit. If user has
# specified metric unit system convert to Celsius.
if UNIT_SYSTEM_METRIC == self.hass.data[SENSORPUSH_DOMAIN][CONF_UNIT_SYSTEM]:
alert_min = (alert_min - 32) / 1.8
alert_max = (alert_max - 32) / 1.8

self._attrs.update({
ATTR_ALERT_MIN: alert_min,
ATTR_ALERT_MAX: alert_max,
Expand Down
31 changes: 7 additions & 24 deletions custom_components/sensorpush/const.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT

SENSORPUSH_DOMAIN = 'sensorpush'

ATTRIBUTION="Data by SensorPush"
Expand All @@ -13,7 +11,6 @@
ATTR_ALERT_MAX = 'alert_max'
ATTR_ALERT_ENABLED = 'alert_enabled'

CONF_UNIT_SYSTEM = 'unit_system'
CONF_MAXIMUM_AGE = 'maximum_age' # maximum age (in minutes) of observations before they expire

MEASURE_TEMP = "temperature"
Expand Down Expand Up @@ -49,25 +46,11 @@
}
}

UNIT_SYSTEM_IMPERIAL = 'imperial'
UNIT_SYSTEM_METRIC = 'metric'

UNIT_SYSTEMS = {
UNIT_SYSTEM_IMPERIAL: {
'system': 'imperial',
MEASURE_BAROMETRIC_PRESSURE: 'inHg',
MEASURE_DEWPOINT: TEMP_FAHRENHEIT,
MEASURE_HUMIDITY: '%', # 'Rh'
MEASURE_TEMP: TEMP_FAHRENHEIT,
MEASURE_VPD: 'kPa'
},

UNIT_SYSTEM_METRIC: {
'system': 'metric',
MEASURE_BAROMETRIC_PRESSURE: 'mbar',
MEASURE_DEWPOINT: TEMP_CELSIUS, # FIXME: icon mdi:temperature-celsius
MEASURE_HUMIDITY: '%', # 'Rh'
MEASURE_TEMP: TEMP_CELSIUS,
MEASURE_VPD: 'kPa'
}
}
'system': 'imperial',
MEASURE_BAROMETRIC_PRESSURE: 'inHg',
MEASURE_DEWPOINT: '°F',
MEASURE_HUMIDITY: '%', # 'Rh'
MEASURE_TEMP: '°F',
MEASURE_VPD: 'kPa'
}
15 changes: 4 additions & 11 deletions custom_components/sensorpush/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

from . import ( SensorPushEntity, SENSORPUSH_SERVICE, SENSORPUSH_SAMPLES)

from .const import (SENSORPUSH_DOMAIN, CONF_UNIT_SYSTEM, MEASURES,
UNIT_SYSTEM_IMPERIAL, UNIT_SYSTEM_METRIC, UNIT_SYSTEMS)
from .const import (SENSORPUSH_DOMAIN, MEASURES, UNIT_SYSTEMS)

LOG = logging.getLogger(__name__)

Expand All @@ -29,8 +28,6 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None):
LOG.error("NOT setting up SensorPush -- SENSORPUSH_SERVICE has not been initialized")
return

unit_system = hass.data[SENSORPUSH_DOMAIN][CONF_UNIT_SYSTEM]

hass_sensors = []
for sensor_info in sensorpush_service.sensors.values():
LOG.info(f"SensorInfo: {sensor_info} -- {type(sensor_info)}")
Expand All @@ -44,7 +41,7 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None):
for measure in MEASURES:
# only include measurements supported by this sensor
if measure in supported_measurements:
sensor = SensorPushMeasurement(hass, config, sensor_info, unit_system, measure)
sensor = SensorPushMeasurement(hass, config, sensor_info, measure)
hass_sensors.append(sensor)

# execute callback to add new entities
Expand All @@ -53,14 +50,10 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None):
# pylint: disable=too-many-instance-attributes
class SensorPushMeasurement(SensorPushEntity, SensorEntity):
"""Measurement sensor for a SensorPush device"""
def __init__(self, hass, config, sensor_info, unit_system, measure):
def __init__(self, hass, config, sensor_info, measure):
self._name = MEASURES[measure]['name']
self._state = None
super().__init__(hass, config, self._name, sensor_info, unit_system, measure)

@property
def unit_of_measurement(self):
return UNIT_SYSTEMS[self._unit_system][self._field_name]
super().__init__(hass, config, self._name, sensor_info, measure)

@property
def unique_id(self):
Expand Down

0 comments on commit 4923d40

Please sign in to comment.