2017-03-17 02:22:10 -04:00
|
|
|
"""The tests for Efergy sensor platform."""
|
|
|
|
|
2021-10-09 05:23:40 -04:00
|
|
|
import asyncio
|
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
|
|
|
from homeassistant.const import STATE_UNAVAILABLE
|
2021-09-24 06:54:15 -04:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-10-19 12:09:39 +02:00
|
|
|
from homeassistant.setup import async_setup_component
|
2021-10-09 05:23:40 -04:00
|
|
|
import homeassistant.util.dt as dt_util
|
2017-03-17 02:22:10 -04:00
|
|
|
|
2021-10-09 05:23:40 -04:00
|
|
|
from tests.common import async_fire_time_changed, load_fixture
|
2021-09-24 06:54:15 -04:00
|
|
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
2017-03-17 02:22:10 -04:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
token = "9p6QGJ7dpZfO3fqPTBk1fyEmjV1cGoLT"
|
|
|
|
multi_sensor_token = "9r6QGF7dpZfO3fqPTBl1fyRmjV1cGoLT"
|
2017-03-17 02:22:10 -04:00
|
|
|
|
|
|
|
ONE_SENSOR_CONFIG = {
|
2019-07-31 12:25:30 -07:00
|
|
|
"platform": "efergy",
|
|
|
|
"app_token": token,
|
|
|
|
"utc_offset": "300",
|
|
|
|
"monitored_variables": [
|
|
|
|
{"type": "amount", "period": "day"},
|
|
|
|
{"type": "instant_readings"},
|
|
|
|
{"type": "budget"},
|
|
|
|
{"type": "cost", "period": "day", "currency": "$"},
|
|
|
|
{"type": "current_values"},
|
|
|
|
],
|
2017-03-17 02:22:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
MULTI_SENSOR_CONFIG = {
|
2019-07-31 12:25:30 -07:00
|
|
|
"platform": "efergy",
|
|
|
|
"app_token": multi_sensor_token,
|
|
|
|
"utc_offset": "300",
|
|
|
|
"monitored_variables": [{"type": "current_values"}],
|
2017-03-17 02:22:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-09 05:23:40 -04:00
|
|
|
def mock_responses(aioclient_mock: AiohttpClientMocker, error: bool = False):
|
2017-03-17 02:22:10 -04:00
|
|
|
"""Mock responses for Efergy."""
|
2019-07-31 12:25:30 -07:00
|
|
|
base_url = "https://engage.efergy.com/mobile_proxy/"
|
2021-10-09 05:23:40 -04:00
|
|
|
if error:
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"{base_url}getCurrentValuesSummary?token={token}", exc=asyncio.TimeoutError
|
|
|
|
)
|
|
|
|
return
|
2021-09-24 06:54:15 -04:00
|
|
|
aioclient_mock.get(
|
2020-08-27 13:56:20 +02:00
|
|
|
f"{base_url}getInstant?token={token}",
|
2021-09-24 06:54:15 -04:00
|
|
|
text=load_fixture("efergy/efergy_instant.json"),
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2021-09-24 06:54:15 -04:00
|
|
|
aioclient_mock.get(
|
2020-04-05 00:26:08 +02:00
|
|
|
f"{base_url}getEnergy?token={token}&offset=300&period=day",
|
2021-09-24 06:54:15 -04:00
|
|
|
text=load_fixture("efergy/efergy_energy.json"),
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2021-09-24 06:54:15 -04:00
|
|
|
aioclient_mock.get(
|
2020-08-27 13:56:20 +02:00
|
|
|
f"{base_url}getBudget?token={token}",
|
2021-09-24 06:54:15 -04:00
|
|
|
text=load_fixture("efergy/efergy_budget.json"),
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2021-09-24 06:54:15 -04:00
|
|
|
aioclient_mock.get(
|
2020-04-05 00:26:08 +02:00
|
|
|
f"{base_url}getCost?token={token}&offset=300&period=day",
|
2021-09-24 06:54:15 -04:00
|
|
|
text=load_fixture("efergy/efergy_cost.json"),
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2021-09-24 06:54:15 -04:00
|
|
|
aioclient_mock.get(
|
2020-04-05 00:26:08 +02:00
|
|
|
f"{base_url}getCurrentValuesSummary?token={token}",
|
2021-09-24 06:54:15 -04:00
|
|
|
text=load_fixture("efergy/efergy_current_values_single.json"),
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2021-09-24 06:54:15 -04:00
|
|
|
aioclient_mock.get(
|
2020-04-05 00:26:08 +02:00
|
|
|
f"{base_url}getCurrentValuesSummary?token={multi_sensor_token}",
|
2021-09-24 06:54:15 -04:00
|
|
|
text=load_fixture("efergy/efergy_current_values_multi.json"),
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2017-03-17 02:22:10 -04:00
|
|
|
|
|
|
|
|
2021-09-24 06:54:15 -04:00
|
|
|
async def test_single_sensor_readings(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
):
|
2020-10-19 12:09:39 +02:00
|
|
|
"""Test for successfully setting up the Efergy platform."""
|
2021-09-24 06:54:15 -04:00
|
|
|
mock_responses(aioclient_mock)
|
2021-10-09 05:23:40 -04:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass, SENSOR_DOMAIN, {SENSOR_DOMAIN: ONE_SENSOR_CONFIG}
|
|
|
|
)
|
2020-10-19 12:09:39 +02:00
|
|
|
await hass.async_block_till_done()
|
2017-03-17 02:22:10 -04:00
|
|
|
|
2021-03-20 13:55:10 +01:00
|
|
|
assert hass.states.get("sensor.energy_consumed").state == "38.21"
|
|
|
|
assert hass.states.get("sensor.energy_usage").state == "1580"
|
|
|
|
assert hass.states.get("sensor.energy_budget").state == "ok"
|
|
|
|
assert hass.states.get("sensor.energy_cost").state == "5.27"
|
|
|
|
assert hass.states.get("sensor.efergy_728386").state == "1628"
|
2018-07-09 14:39:28 +02:00
|
|
|
|
2017-03-17 02:22:10 -04:00
|
|
|
|
2021-09-24 06:54:15 -04:00
|
|
|
async def test_multi_sensor_readings(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
):
|
2020-10-19 12:09:39 +02:00
|
|
|
"""Test for multiple sensors in one household."""
|
2021-09-24 06:54:15 -04:00
|
|
|
mock_responses(aioclient_mock)
|
2021-10-09 05:23:40 -04:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass, SENSOR_DOMAIN, {SENSOR_DOMAIN: MULTI_SENSOR_CONFIG}
|
|
|
|
)
|
2020-10-19 12:09:39 +02:00
|
|
|
await hass.async_block_till_done()
|
2018-07-09 14:39:28 +02:00
|
|
|
|
2021-03-20 13:55:10 +01:00
|
|
|
assert hass.states.get("sensor.efergy_728386").state == "218"
|
|
|
|
assert hass.states.get("sensor.efergy_0").state == "1808"
|
|
|
|
assert hass.states.get("sensor.efergy_728387").state == "312"
|
2021-10-09 05:23:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def test_failed_getting_sids(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
):
|
|
|
|
"""Test failed gettings sids."""
|
|
|
|
mock_responses(aioclient_mock, error=True)
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, SENSOR_DOMAIN, {SENSOR_DOMAIN: ONE_SENSOR_CONFIG}
|
|
|
|
)
|
|
|
|
assert not hass.states.async_all("sensor")
|
|
|
|
|
|
|
|
|
|
|
|
async def test_failed_update_and_reconnection(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
):
|
|
|
|
"""Test failed update and reconnection."""
|
|
|
|
mock_responses(aioclient_mock)
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, SENSOR_DOMAIN, {SENSOR_DOMAIN: ONE_SENSOR_CONFIG}
|
|
|
|
)
|
|
|
|
aioclient_mock.clear_requests()
|
|
|
|
mock_responses(aioclient_mock, error=True)
|
|
|
|
next_update = dt_util.utcnow() + timedelta(seconds=3)
|
|
|
|
async_fire_time_changed(hass, next_update)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert hass.states.get("sensor.efergy_728386").state == STATE_UNAVAILABLE
|
|
|
|
aioclient_mock.clear_requests()
|
|
|
|
mock_responses(aioclient_mock)
|
|
|
|
next_update = dt_util.utcnow() + timedelta(seconds=30)
|
|
|
|
async_fire_time_changed(hass, next_update)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert hass.states.get("sensor.efergy_728386").state == "1628"
|