Add precision parameter to dsmr sensor (#19873)

* Added precision parameter to dsmr sensor

* Added precision parameter to dsmr sensor, added whitespace after comma

* Added precision parameter to dsmr sensor

* Added precision parameter to dsmr sensor, fixed test

* Changed try except as requested
This commit is contained in:
Sander Zumbrink 2019-01-31 16:46:54 +01:00 committed by Martin Hjelmare
parent c9971673da
commit e20c2aa113
2 changed files with 17 additions and 5 deletions

View file

@ -24,9 +24,12 @@ REQUIREMENTS = ['dsmr_parser==0.12']
CONF_DSMR_VERSION = 'dsmr_version'
CONF_RECONNECT_INTERVAL = 'reconnect_interval'
CONF_PRECISION = 'precision'
DEFAULT_DSMR_VERSION = '2.2'
DEFAULT_PORT = '/dev/ttyUSB0'
DEFAULT_PRECISION = 3
DOMAIN = 'dsmr'
ICON_GAS = 'mdi:fire'
@ -45,6 +48,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_DSMR_VERSION, default=DEFAULT_DSMR_VERSION): vol.All(
cv.string, vol.In(['5', '4', '2.2'])),
vol.Optional(CONF_RECONNECT_INTERVAL, default=30): int,
vol.Optional(CONF_PRECISION, default=DEFAULT_PRECISION): vol.Coerce(int),
})
@ -146,7 +150,7 @@ async def async_setup_platform(hass, config, async_add_entities,
]
# Generate device entities
devices = [DSMREntity(name, obis) for name, obis in obis_mapping]
devices = [DSMREntity(name, obis, config) for name, obis in obis_mapping]
# Protocol version specific obis
if dsmr_version in ('4', '5'):
@ -156,8 +160,8 @@ async def async_setup_platform(hass, config, async_add_entities,
# Add gas meter reading and derivative for usage
devices += [
DSMREntity('Gas Consumption', gas_obis),
DerivativeDSMREntity('Hourly Gas Consumption', gas_obis),
DSMREntity('Gas Consumption', gas_obis, config),
DerivativeDSMREntity('Hourly Gas Consumption', gas_obis, config),
]
async_add_entities(devices)
@ -224,10 +228,11 @@ async def async_setup_platform(hass, config, async_add_entities,
class DSMREntity(Entity):
"""Entity reading values from DSMR telegram."""
def __init__(self, name, obis):
def __init__(self, name, obis, config):
"""Initialize entity."""
self._name = name
self._obis = obis
self._config = config
self.telegram = {}
def get_dsmr_object_attr(self, attribute):
@ -267,6 +272,11 @@ class DSMREntity(Entity):
if self._obis == obis.ELECTRICITY_ACTIVE_TARIFF:
return self.translate_tariff(value)
try:
value = round(float(value), self._config[CONF_PRECISION])
except TypeError:
pass
if value is not None:
return value