Minor comment updates and ordering (#4554)

This commit is contained in:
Fabian Affolter 2016-11-24 00:27:31 +01:00 committed by GitHub
parent b6d559da1f
commit b1b8715f7d

View File

@ -25,7 +25,6 @@ Another loop (DSMR class) is setup which reads the telegram queue,
stores/caches the latest telegram and notifies the Entities that the telegram stores/caches the latest telegram and notifies the Entities that the telegram
has been updated. has been updated.
""" """
import asyncio import asyncio
import logging import logging
from datetime import timedelta from datetime import timedelta
@ -37,33 +36,33 @@ from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_PORT, EVENT_HOMEASSISTANT_STOP from homeassistant.const import CONF_PORT, EVENT_HOMEASSISTANT_STOP
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
DOMAIN = 'dsmr' _LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['dsmr_parser==0.4'] REQUIREMENTS = ['dsmr_parser==0.4']
CONF_DSMR_VERSION = 'dsmr_version'
DEFAULT_DSMR_VERSION = '2.2'
DEFAULT_PORT = '/dev/ttyUSB0'
DOMAIN = 'dsmr'
ICON_GAS = 'mdi:fire'
ICON_POWER = 'mdi:flash'
# Smart meter sends telegram every 10 seconds # Smart meter sends telegram every 10 seconds
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=10) MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=10)
CONF_DSMR_VERSION = 'dsmr_version'
DEFAULT_PORT = '/dev/ttyUSB0'
DEFAULT_DSMR_VERSION = '2.2'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.string, vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.string,
vol.Optional(CONF_DSMR_VERSION, default=DEFAULT_DSMR_VERSION): vol.All( vol.Optional(CONF_DSMR_VERSION, default=DEFAULT_DSMR_VERSION): vol.All(
cv.string, vol.In(['4', '2.2'])), cv.string, vol.In(['4', '2.2'])),
}) })
_LOGGER = logging.getLogger(__name__)
ICON_POWER = 'mdi:flash'
ICON_GAS = 'mdi:fire'
@asyncio.coroutine @asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None): def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Setup DSMR sensors.""" """Set up the DSMR sensor."""
# suppres logging # Suppress logging
logging.getLogger('dsmr_parser').setLevel(logging.ERROR) logging.getLogger('dsmr_parser').setLevel(logging.ERROR)
from dsmr_parser import obis_references as obis from dsmr_parser import obis_references as obis
@ -71,7 +70,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
dsmr_version = config[CONF_DSMR_VERSION] dsmr_version = config[CONF_DSMR_VERSION]
# define list of name,obis mappings to generate entities # Define list of name,obis mappings to generate entities
obis_mapping = [ obis_mapping = [
['Power Consumption', obis.CURRENT_ELECTRICITY_USAGE], ['Power Consumption', obis.CURRENT_ELECTRICITY_USAGE],
['Power Production', obis.CURRENT_ELECTRICITY_DELIVERY], ['Power Production', obis.CURRENT_ELECTRICITY_DELIVERY],
@ -81,31 +80,30 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
['Power Production (low)', obis.ELECTRICITY_DELIVERED_TARIFF_1], ['Power Production (low)', obis.ELECTRICITY_DELIVERED_TARIFF_1],
['Power Production (normal)', obis.ELECTRICITY_DELIVERED_TARIFF_2], ['Power Production (normal)', obis.ELECTRICITY_DELIVERED_TARIFF_2],
] ]
# protocol version specific obis # Protocol version specific obis
if dsmr_version == '4': if dsmr_version == '4':
obis_mapping.append(['Gas Consumption', obis.HOURLY_GAS_METER_READING]) obis_mapping.append(['Gas Consumption', obis.HOURLY_GAS_METER_READING])
else: else:
obis_mapping.append(['Gas Consumption', obis.GAS_METER_READING]) obis_mapping.append(['Gas Consumption', obis.GAS_METER_READING])
# generate device entities # Generate device entities
devices = [DSMREntity(name, obis) for name, obis in obis_mapping] devices = [DSMREntity(name, obis) for name, obis in obis_mapping]
# setup devices
yield from async_add_devices(devices) yield from async_add_devices(devices)
def update_entities_telegram(telegram): def update_entities_telegram(telegram):
"""Update entities with latests telegram & trigger state update.""" """Update entities with latests telegram & trigger state update."""
# make all device entities aware of new telegram # Make all device entities aware of new telegram
for device in devices: for device in devices:
device.telegram = telegram device.telegram = telegram
hass.async_add_job(device.async_update_ha_state) hass.async_add_job(device.async_update_ha_state)
# creates a asyncio.Protocol for reading DSMR telegrams from serial # Creates a asyncio.Protocol for reading DSMR telegrams from serial
# and calls update_entities_telegram to update entities on arrival # and calls update_entities_telegram to update entities on arrival
dsmr = create_dsmr_reader(config[CONF_PORT], config[CONF_DSMR_VERSION], dsmr = create_dsmr_reader(config[CONF_PORT], config[CONF_DSMR_VERSION],
update_entities_telegram, loop=hass.loop) update_entities_telegram, loop=hass.loop)
# start DSMR asycnio.Protocol reader # Start DSMR asycnio.Protocol reader
transport, _ = yield from hass.loop.create_task(dsmr) transport, _ = yield from hass.loop.create_task(dsmr)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, transport.close) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, transport.close)
@ -116,15 +114,13 @@ class DSMREntity(Entity):
def __init__(self, name, obis): def __init__(self, name, obis):
""""Initialize entity.""" """"Initialize entity."""
# human readable name
self._name = name self._name = name
# DSMR spec. value identifier
self._obis = obis self._obis = obis
self.telegram = {} self.telegram = {}
def get_dsmr_object_attr(self, attribute): def get_dsmr_object_attr(self, attribute):
"""Read attribute from last received telegram for this DSMR object.""" """Read attribute from last received telegram for this DSMR object."""
# make sure telegram contains an object for this entities obis # Make sure telegram contains an object for this entities obis
if self._obis not in self.telegram: if self._obis not in self.telegram:
return None return None
@ -165,9 +161,8 @@ class DSMREntity(Entity):
@staticmethod @staticmethod
def translate_tariff(value): def translate_tariff(value):
"""Convert 2/1 to normal/low.""" """Convert 2/1 to normal/low."""
# DSMR V2.2: Note: Tariff code 1 is used for low tariff # DSMR V2.2: Note: Rate code 1 is used for low rate and rate code 2 is
# and tariff code 2 is used for normal tariff. # used for normal rate.
if value == '0002': if value == '0002':
return 'normal' return 'normal'
elif value == '0001': elif value == '0001':