Add support for Flo by Moen water shutoff devices (#38171)

This commit is contained in:
David F. Mulcahey authored and GitHub committed 2020-08-10 14:19:38 +02:00
1 parent 07de9deab6
commit f1fd8aa51f
27 files changed
+1650 -1

No files matched your search

+1
View File
@@ -0,0 +1 @@
"""Tests for the flo integration."""
+12
View File
@@ -0,0 +1,12 @@
"""Define common test utilities."""
TEST_ACCOUNT_ID = "aabbccdd"
TEST_DEVICE_ID = "98765"
TEST_EMAIL_ADDRESS = "email@address.com"
TEST_FIRST_NAME = "Tom"
TEST_LAST_NAME = "Jones"
TEST_LOCATION_ID = "mmnnoopp"
TEST_MAC_ADDRESS = "12:34:56:ab:cd:ef"
TEST_PASSWORD = "password"
TEST_PHONE_NUMBER = "+1 123-456-7890"
TEST_TOKEN = "123abc"
TEST_USER_ID = "12345abcde"
+83
View File
@@ -0,0 +1,83 @@
"""Define fixtures available for all tests."""
import json
import time
import pytest
from homeassistant.components.flo.const import DOMAIN as FLO_DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from .common import TEST_EMAIL_ADDRESS, TEST_PASSWORD, TEST_TOKEN, TEST_USER_ID
from tests.common import MockConfigEntry, load_fixture
@pytest.fixture
def config_entry(hass):
"""Config entry version 1 fixture."""
return MockConfigEntry(
domain=FLO_DOMAIN,
data={CONF_USERNAME: TEST_USER_ID, CONF_PASSWORD: TEST_PASSWORD},
version=1,
)
@pytest.fixture
def aioclient_mock_fixture(aioclient_mock):
"""Fixture to provide a aioclient mocker."""
now = round(time.time())
# Mocks the login response for flo.
aioclient_mock.post(
"https://api.meetflo.com/api/v1/users/auth",
text=json.dumps(
{
"token": TEST_TOKEN,
"tokenPayload": {
"user": {"user_id": TEST_USER_ID, "email": TEST_EMAIL_ADDRESS},
"timestamp": now,
},
"tokenExpiration": 86400,
"timeNow": now,
}
),
headers={"Content-Type": "application/json"},
status=200,
)
# Mocks the device for flo.
aioclient_mock.get(
"https://api-gw.meetflo.com/api/v2/devices/98765",
text=load_fixture("flo/device_info_response.json"),
status=200,
headers={"Content-Type": "application/json"},
)
# Mocks the water consumption for flo.
aioclient_mock.get(
"https://api-gw.meetflo.com/api/v2/water/consumption",
text=load_fixture("flo/water_consumption_info_response.json"),
status=200,
headers={"Content-Type": "application/json"},
)
# Mocks the location info for flo.
aioclient_mock.get(
"https://api-gw.meetflo.com/api/v2/locations/mmnnoopp",
text=load_fixture("flo/location_info_expand_devices_response.json"),
status=200,
headers={"Content-Type": "application/json"},
)
# Mocks the user info for flo.
aioclient_mock.get(
"https://api-gw.meetflo.com/api/v2/users/12345abcde",
text=load_fixture("flo/user_info_expand_locations_response.json"),
status=200,
headers={"Content-Type": "application/json"},
params={"expand": "locations"},
)
# Mocks the user info for flo.
aioclient_mock.get(
"https://api-gw.meetflo.com/api/v2/users/12345abcde",
text=load_fixture("flo/user_info_expand_locations_response.json"),
status=200,
headers={"Content-Type": "application/json"},
)
+46
View File
@@ -0,0 +1,46 @@
"""Test the flo config flow."""
from homeassistant import config_entries, setup
from homeassistant.components.flo.const import DOMAIN
from tests.async_mock import patch
async def test_form(hass, aioclient_mock_fixture):
"""Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["errors"] == {}
with patch(
"homeassistant.components.flo.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.flo.async_setup_entry", return_value=True
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"username": "test-username", "password": "test-password"},
)
assert result2["type"] == "create_entry"
assert result2["title"] == "Home"
assert result2["data"] == {"username": "test-username", "password": "test-password"}
await hass.async_block_till_done()
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_cannot_connect(hass, aioclient_mock):
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {"username": "test-username", "password": "test-password"}
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"}
+50
View File
@@ -0,0 +1,50 @@
"""Define tests for device-related endpoints."""
from datetime import timedelta
from homeassistant.components.flo.const import DOMAIN as FLO_DOMAIN
from homeassistant.components.flo.device import FloDeviceDataUpdateCoordinator
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.setup import async_setup_component
from homeassistant.util import dt
from .common import TEST_PASSWORD, TEST_USER_ID
from tests.common import async_fire_time_changed
async def test_device(hass, config_entry, aioclient_mock_fixture, aioclient_mock):
"""Test Flo by Moen device."""
config_entry.add_to_hass(hass)
assert await async_setup_component(
hass, FLO_DOMAIN, {CONF_USERNAME: TEST_USER_ID, CONF_PASSWORD: TEST_PASSWORD}
)
await hass.async_block_till_done()
assert len(hass.data[FLO_DOMAIN]["devices"]) == 1
device: FloDeviceDataUpdateCoordinator = hass.data[FLO_DOMAIN]["devices"][0]
assert device.api_client is not None
assert device.available
assert device.consumption_today == 3.674
assert device.current_flow_rate == 0
assert device.current_psi == 54.20000076293945
assert device.current_system_mode == "home"
assert device.target_system_mode == "home"
assert device.firmware_version == "6.1.1"
assert device.device_type == "flo_device_v2"
assert device.id == "98765"
assert device.last_heard_from_time == "2020-07-24T12:45:00Z"
assert device.location_id == "mmnnoopp"
assert device.hass is not None
assert device.temperature == 70
assert device.mac_address == "111111111111"
assert device.model == "flo_device_075_v2"
assert device.manufacturer == "Flo by Moen"
assert device.device_name == "Flo by Moen flo_device_075_v2"
assert device.rssi == -47
call_count = aioclient_mock.call_count
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=90))
await hass.async_block_till_done()
assert aioclient_mock.call_count == call_count + 2
+16
View File
@@ -0,0 +1,16 @@
"""Test init."""
from homeassistant.components.flo.const import DOMAIN as FLO_DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.setup import async_setup_component
from .common import TEST_PASSWORD, TEST_USER_ID
async def test_setup_entry(hass, config_entry, aioclient_mock_fixture):
"""Test migration of config entry from v1."""
config_entry.add_to_hass(hass)
assert await async_setup_component(
hass, FLO_DOMAIN, {CONF_USERNAME: TEST_USER_ID, CONF_PASSWORD: TEST_PASSWORD}
)
await hass.async_block_till_done()
assert len(hass.data[FLO_DOMAIN]["devices"]) == 1
+24
View File
@@ -0,0 +1,24 @@
"""Test Flo by Moen sensor entities."""
from homeassistant.components.flo.const import DOMAIN as FLO_DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.setup import async_setup_component
from .common import TEST_PASSWORD, TEST_USER_ID
async def test_sensors(hass, config_entry, aioclient_mock_fixture):
"""Test Flo by Moen sensors."""
config_entry.add_to_hass(hass)
assert await async_setup_component(
hass, FLO_DOMAIN, {CONF_USERNAME: TEST_USER_ID, CONF_PASSWORD: TEST_PASSWORD}
)
await hass.async_block_till_done()
assert len(hass.data[FLO_DOMAIN]["devices"]) == 1
# we should have 5 entities for the device
assert hass.states.get("sensor.current_system_mode").state == "home"
assert hass.states.get("sensor.today_s_water_usage").state == "3.7"
assert hass.states.get("sensor.water_flow_rate").state == "0"
assert hass.states.get("sensor.water_pressure").state == "54.2"
assert hass.states.get("sensor.water_temperature").state == "21.1"