Add Glances sensors dynamically (#28639)

* Add temp_sensors to glances dynamically

* unsub from updates when sensor is disabled

* dynamicall add sensors

* change "device_name" to "mnt_point"

* remove unnecessary logging

* update sensor.py

* update test_config_flow.py

* remove commented code
This commit is contained in:
Rami Mosleh authored and GitHub committed 2020-02-10 15:02:14 -08:00
1 parent d55846c33a
commit c66106ee98
3 files changed
+152 -86

No files matched your search

+44 -25
View File
@@ -3,8 +3,8 @@ from unittest.mock import patch
from glances_api import Glances
from homeassistant.components.glances import config_flow
from homeassistant.components.glances.const import DOMAIN
from homeassistant import data_entry_flow
from homeassistant.components import glances
from homeassistant.const import CONF_SCAN_INTERVAL
from tests.common import MockConfigEntry, mock_coro
@@ -29,22 +29,22 @@ DEMO_USER_INPUT = {
}
def init_config_flow(hass):
"""Init a configuration flow."""
flow = config_flow.GlancesFlowHandler()
flow.hass = hass
return flow
async def test_form(hass):
"""Test config entry configured successfully."""
flow = init_config_flow(hass)
result = await hass.config_entries.flow.async_init(
glances.DOMAIN, context={"source": "user"}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
with patch("glances_api.Glances"), patch.object(
Glances, "get_data", return_value=mock_coro()
):
result = await flow.async_step_user(DEMO_USER_INPUT)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=DEMO_USER_INPUT
)
assert result["type"] == "create_entry"
assert result["title"] == NAME
@@ -53,10 +53,14 @@ async def test_form(hass):
async def test_form_cannot_connect(hass):
"""Test to return error if we cannot connect."""
flow = init_config_flow(hass)
with patch("glances_api.Glances"):
result = await flow.async_step_user(DEMO_USER_INPUT)
result = await hass.config_entries.flow.async_init(
glances.DOMAIN, context={"source": "user"}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=DEMO_USER_INPUT
)
assert result["type"] == "form"
assert result["errors"] == {"base": "cannot_connect"}
@@ -64,11 +68,15 @@ async def test_form_cannot_connect(hass):
async def test_form_wrong_version(hass):
"""Test to check if wrong version is entered."""
flow = init_config_flow(hass)
user_input = DEMO_USER_INPUT.copy()
user_input.update(version=1)
result = await flow.async_step_user(user_input)
result = await hass.config_entries.flow.async_init(
glances.DOMAIN, context={"source": "user"}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=user_input
)
assert result["type"] == "form"
assert result["errors"] == {"version": "wrong_version"}
@@ -77,13 +85,16 @@ async def test_form_wrong_version(hass):
async def test_form_already_configured(hass):
"""Test host is already configured."""
entry = MockConfigEntry(
domain=DOMAIN, data=DEMO_USER_INPUT, options={CONF_SCAN_INTERVAL: 60}
domain=glances.DOMAIN, data=DEMO_USER_INPUT, options={CONF_SCAN_INTERVAL: 60}
)
entry.add_to_hass(hass)
flow = init_config_flow(hass)
result = await flow.async_step_user(DEMO_USER_INPUT)
result = await hass.config_entries.flow.async_init(
glances.DOMAIN, context={"source": "user"}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=DEMO_USER_INPUT
)
assert result["type"] == "abort"
assert result["reason"] == "already_configured"
@@ -91,12 +102,20 @@ async def test_form_already_configured(hass):
async def test_options(hass):
"""Test options for Glances."""
entry = MockConfigEntry(
domain=DOMAIN, data=DEMO_USER_INPUT, options={CONF_SCAN_INTERVAL: 60}
domain=glances.DOMAIN, data=DEMO_USER_INPUT, options={CONF_SCAN_INTERVAL: 60}
)
entry.add_to_hass(hass)
flow = init_config_flow(hass)
options_flow = flow.async_get_options_flow(entry)
result = await options_flow.async_step_init({CONF_SCAN_INTERVAL: 10})
assert result["type"] == "create_entry"
assert result["data"][CONF_SCAN_INTERVAL] == 10
result = await hass.config_entries.options.async_init(entry.entry_id)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={glances.CONF_SCAN_INTERVAL: 10}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["data"] == {
glances.CONF_SCAN_INTERVAL: 10,
}