Move imports in dsmr component (#27974)

* Move imports in dsmr component

* Review

* Fix tests
pull/28114/head
Diefferson Koderer Môro 2019-10-22 16:50:49 +00:00 committed by cgtobi
parent 0226b76e0a
commit 04dbe5bc84
2 changed files with 12 additions and 11 deletions

View File

@ -4,6 +4,9 @@ from datetime import timedelta
from functools import partial
import logging
from dsmr_parser import obis_references as obis_ref
from dsmr_parser.clients.protocol import create_dsmr_reader, create_tcp_dsmr_reader
import serial
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
@ -52,10 +55,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
# Suppress logging
logging.getLogger("dsmr_parser").setLevel(logging.ERROR)
from dsmr_parser import obis_references as obis_ref
from dsmr_parser.clients.protocol import create_dsmr_reader, create_tcp_dsmr_reader
import serial
dsmr_version = config[CONF_DSMR_VERSION]
# Define list of name,obis mappings to generate entities
@ -212,11 +211,9 @@ class DSMREntity(Entity):
@property
def state(self):
"""Return the state of sensor, if available, translate if needed."""
from dsmr_parser import obis_references as obis
value = self.get_dsmr_object_attr("value")
if self._obis == obis.ELECTRICITY_ACTIVE_TARIFF:
if self._obis == obis_ref.ELECTRICITY_ACTIVE_TARIFF:
return self.translate_tariff(value)
try:

View File

@ -11,9 +11,11 @@ from decimal import Decimal
from unittest.mock import Mock
import asynctest
import pytest
from homeassistant.bootstrap import async_setup_component
from homeassistant.components.dsmr.sensor import DerivativeDSMREntity
import pytest
from tests.common import assert_setup_component
@ -34,10 +36,11 @@ def mock_connection_factory(monkeypatch):
# apply the mock to both connection factories
monkeypatch.setattr(
"dsmr_parser.clients.protocol.create_dsmr_reader", connection_factory
"homeassistant.components.dsmr.sensor.create_dsmr_reader", connection_factory
)
monkeypatch.setattr(
"dsmr_parser.clients.protocol.create_tcp_dsmr_reader", connection_factory
"homeassistant.components.dsmr.sensor.create_tcp_dsmr_reader",
connection_factory,
)
return connection_factory, transport, protocol
@ -158,7 +161,8 @@ def test_connection_errors_retry(hass, monkeypatch, mock_connection_factory):
)
monkeypatch.setattr(
"dsmr_parser.clients.protocol.create_dsmr_reader", first_fail_connection_factory
"homeassistant.components.dsmr.sensor.create_dsmr_reader",
first_fail_connection_factory,
)
yield from async_setup_component(hass, "sensor", {"sensor": config})