Fix for DTE Energy Bridge returning the wrong units from time to time (#9246)
The DTE Energy Bridge seems to return the current energy usage randomly in either W or kW. The only way to tell the difference is if there is a decimal or not in the result. Also added some tests.
This commit is contained in:
parent
04bed51277
commit
7b0628421d
2 changed files with 74 additions and 1 deletions
|
@ -91,4 +91,9 @@ class DteEnergyBridgeSensor(Entity):
|
||||||
response.text, self._name)
|
response.text, self._name)
|
||||||
return
|
return
|
||||||
|
|
||||||
self._state = float(response_split[0])
|
val = float(response_split[0])
|
||||||
|
|
||||||
|
# A workaround for a bug in the DTE energy bridge.
|
||||||
|
# The returned value can randomly be in W or kW. Checking for a
|
||||||
|
# a decimal seems to be a reliable way to determine the units.
|
||||||
|
self._state = val if '.' in response_split[0] else val / 1000
|
||||||
|
|
68
tests/components/sensor/test_dte_energy_bridge.py
Normal file
68
tests/components/sensor/test_dte_energy_bridge.py
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
"""The tests for the DTE Energy Bridge."""
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import requests_mock
|
||||||
|
|
||||||
|
from homeassistant.setup import setup_component
|
||||||
|
|
||||||
|
from tests.common import get_test_home_assistant
|
||||||
|
|
||||||
|
DTE_ENERGY_BRIDGE_CONFIG = {
|
||||||
|
'platform': 'dte_energy_bridge',
|
||||||
|
'ip': '192.168.1.1',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class TestDteEnergyBridgeSetup(unittest.TestCase):
|
||||||
|
"""Test the DTE Energy Bridge platform."""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
"""Initialize values for this testcase class."""
|
||||||
|
self.hass = get_test_home_assistant()
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
"""Stop everything that was started."""
|
||||||
|
self.hass.stop()
|
||||||
|
|
||||||
|
def test_setup_with_config(self):
|
||||||
|
"""Test the platform setup with configuration."""
|
||||||
|
self.assertTrue(
|
||||||
|
setup_component(self.hass, 'sensor',
|
||||||
|
{'dte_energy_bridge': DTE_ENERGY_BRIDGE_CONFIG}))
|
||||||
|
|
||||||
|
@requests_mock.Mocker()
|
||||||
|
def test_setup_correct_reading(self, mock_req):
|
||||||
|
"""Test DTE Energy bridge returns a correct value."""
|
||||||
|
mock_req.get("http://{}/instantaneousdemand"
|
||||||
|
.format(DTE_ENERGY_BRIDGE_CONFIG['ip']),
|
||||||
|
text='.411 kW')
|
||||||
|
assert setup_component(self.hass, 'sensor', {
|
||||||
|
'sensor': DTE_ENERGY_BRIDGE_CONFIG})
|
||||||
|
self.assertEqual('0.411',
|
||||||
|
self.hass.states
|
||||||
|
.get('sensor.current_energy_usage').state)
|
||||||
|
|
||||||
|
@requests_mock.Mocker()
|
||||||
|
def test_setup_incorrect_units_reading(self, mock_req):
|
||||||
|
"""Test DTE Energy bridge handles a value with incorrect units."""
|
||||||
|
mock_req.get("http://{}/instantaneousdemand"
|
||||||
|
.format(DTE_ENERGY_BRIDGE_CONFIG['ip']),
|
||||||
|
text='411 kW')
|
||||||
|
assert setup_component(self.hass, 'sensor', {
|
||||||
|
'sensor': DTE_ENERGY_BRIDGE_CONFIG})
|
||||||
|
self.assertEqual('0.411',
|
||||||
|
self.hass.states
|
||||||
|
.get('sensor.current_energy_usage').state)
|
||||||
|
|
||||||
|
@requests_mock.Mocker()
|
||||||
|
def test_setup_bad_format_reading(self, mock_req):
|
||||||
|
"""Test DTE Energy bridge handles an invalid value."""
|
||||||
|
mock_req.get("http://{}/instantaneousdemand"
|
||||||
|
.format(DTE_ENERGY_BRIDGE_CONFIG['ip']),
|
||||||
|
text='411')
|
||||||
|
assert setup_component(self.hass, 'sensor', {
|
||||||
|
'sensor': DTE_ENERGY_BRIDGE_CONFIG})
|
||||||
|
self.assertEqual('unknown',
|
||||||
|
self.hass.states
|
||||||
|
.get('sensor.current_energy_usage').state)
|
Loading…
Add table
Reference in a new issue