Skip to content

Commit

Permalink
Merge entities into one, add icons, fix tests for asyncio.
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Bloemberg committed Nov 12, 2016
1 parent 1cf1341 commit aabbe6e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
37 changes: 34 additions & 3 deletions homeassistant/components/sensor/dsmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@

_LOGGER = logging.getLogger(__name__)

ICON_POWER = 'mdi:flash'
ICON_GAS = 'mdi:fire'


@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
Expand Down Expand Up @@ -151,18 +154,46 @@ def name(self):
"""Return the name of the sensor."""
return self._name

@property
def icon(self):
"""Icon to use in the frontend, if any."""
if 'Power' in self._name:
return ICON_POWER
elif 'Gas' in self._name:
return ICON_GAS

@property
def state(self):
"""Return the state of the sensor, if available."""
return getattr(self._interface.telegram.get(self._obis, {}),
'value', None)
"""Return the state of sensor, if available, translate if needed."""
from dsmr_parser import obis_references as obis

value = getattr(self._interface.telegram.get(self._obis, {}),
'value', None)

if self._obis == obis.ELECTRICITY_ACTIVE_TARIFF:
return self.translate_tariff(value)
else:
return value

@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return getattr(self._interface.telegram.get(self._obis, {}),
'unit', None)

@staticmethod
def translate_tariff(value):
"""Convert 2/1 to normal/low."""
# DSMR V2.2: Note: Tariff code 1 is used for low tariff
# and tariff code 2 is used for normal tariff.

if value == '0002':
return 'normal'
elif value == '0001':
return 'low'
else:
return None


class DSMRTariff(DSMREntity):
"""Convert integer tariff value to text."""
Expand Down
35 changes: 24 additions & 11 deletions tests/components/sensor/test_dsmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import asyncio
from decimal import Decimal
from unittest.mock import patch

from homeassistant.bootstrap import async_setup_component
from tests.common import assert_setup_component


@asyncio.coroutine
def test_default_setup(hass):
def test_default_setup(hass, monkeypatch):
"""Test the default setup."""
from dsmr_parser.obis_references import (
CURRENT_ELECTRICITY_USAGE,
Expand All @@ -28,16 +27,30 @@ def test_default_setup(hass):
]),
}

# with patch('homeassistant.components.sensor.dsmr.DSMR.read_telegram',
# return_value=telegram), assert_setup_component(1):
yield from async_setup_component(hass, 'sensor', {'sensor': config})
# mock queue for injecting DSMR telegram
queue = asyncio.Queue(loop=hass.loop)
monkeypatch.setattr('asyncio.Queue', lambda: queue)

state = hass.states.get('sensor.power_consumption')
with assert_setup_component(1):
yield from async_setup_component(hass, 'sensor', {'sensor': config})

assert state.state == 'unknown'
assert state.attributes.get('unit_of_measurement') is 'kWh'
# make sure entities have been created and return 'unknown' state
power_consumption = hass.states.get('sensor.power_consumption')
assert power_consumption.state == 'unknown'
assert power_consumption.attributes.get('unit_of_measurement') is None

state = hass.states.get('sensor.power_tariff')
# simulate a telegram pushed from the smartmeter and parsed by dsmr_parser
yield from queue.put(telegram)

assert state.state == 'low'
assert state.attributes.get('unit_of_measurement') is None
# after receiving telegram entities need to have the chance to update
yield from asyncio.sleep(0, loop=hass.loop)

# ensure entities have new state value after incoming telegram
power_consumption = hass.states.get('sensor.power_consumption')
assert power_consumption.state == '0.1'
assert power_consumption.attributes.get('unit_of_measurement') is 'kWh'

# tariff should be translated in human readable and have no unit
power_tariff = hass.states.get('sensor.power_tariff')
assert power_tariff.state == 'low'
assert power_tariff.attributes.get('unit_of_measurement') is None

0 comments on commit aabbe6e

Please sign in to comment.