Add possibility to synchronize automatically all available feeds in emoncms (#128122)

* Add checkbox in options to sync all feeds once

* Add sync mode selector in async_step_user
Remove checkbox in options

* Correct use of SYNC_MODE & SYNC_MODE_AUTO in tests

* Use dropdown for mode selection

* rmv_unused_const

* Add separate tests + use SelectSelector
This commit is contained in:
Alexandre CUER 2025-06-30 10:05:07 +02:00 committed by GitHub
parent c9a6b1fd45
commit 97c1e21a69
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 97 additions and 20 deletions

View File

@ -16,7 +16,12 @@ from homeassistant.config_entries import (
from homeassistant.const import CONF_API_KEY, CONF_URL
from homeassistant.core import callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.selector import selector
from homeassistant.helpers.selector import (
SelectSelector,
SelectSelectorConfig,
SelectSelectorMode,
selector,
)
from .const import (
CONF_MESSAGE,
@ -26,6 +31,9 @@ from .const import (
FEED_ID,
FEED_NAME,
FEED_TAG,
SYNC_MODE,
SYNC_MODE_AUTO,
SYNC_MODE_MANUAL,
)
@ -102,6 +110,17 @@ class EmoncmsConfigFlow(ConfigFlow, domain=DOMAIN):
"mode": "dropdown",
"multiple": True,
}
if user_input.get(SYNC_MODE) == SYNC_MODE_AUTO:
return self.async_create_entry(
title=sensor_name(self.url),
data={
CONF_URL: self.url,
CONF_API_KEY: self.api_key,
CONF_ONLY_INCLUDE_FEEDID: [
feed[FEED_ID] for feed in result[CONF_MESSAGE]
],
},
)
return await self.async_step_choose_feeds()
return self.async_show_form(
step_id="user",
@ -110,6 +129,15 @@ class EmoncmsConfigFlow(ConfigFlow, domain=DOMAIN):
{
vol.Required(CONF_URL): str,
vol.Required(CONF_API_KEY): str,
vol.Required(
SYNC_MODE, default=SYNC_MODE_MANUAL
): SelectSelector(
SelectSelectorConfig(
options=[SYNC_MODE_MANUAL, SYNC_MODE_AUTO],
mode=SelectSelectorMode.DROPDOWN,
translation_key=SYNC_MODE,
)
),
}
),
user_input,

View File

@ -14,6 +14,9 @@ EMONCMS_UUID_DOC_URL = (
FEED_ID = "id"
FEED_NAME = "name"
FEED_TAG = "tag"
SYNC_MODE = "sync_mode"
SYNC_MODE_AUTO = "auto"
SYNC_MODE_MANUAL = "manual"
LOGGER = logging.getLogger(__package__)

View File

@ -7,7 +7,8 @@
"user": {
"data": {
"url": "[%key:common::config_flow::data::url%]",
"api_key": "[%key:common::config_flow::data::api_key%]"
"api_key": "[%key:common::config_flow::data::api_key%]",
"sync_mode": "Synchronization mode"
},
"data_description": {
"url": "Server URL starting with the protocol (http or https)",
@ -24,6 +25,14 @@
"already_configured": "This server is already configured"
}
},
"selector": {
"sync_mode": {
"options": {
"auto": "Synchronize all available Feeds",
"manual": "Select which Feeds to synchronize"
}
}
},
"entity": {
"sensor": {
"energy": {

View File

@ -2,14 +2,20 @@
from unittest.mock import AsyncMock
from homeassistant.components.emoncms.const import CONF_ONLY_INCLUDE_FEEDID, DOMAIN
from homeassistant.components.emoncms.const import (
CONF_ONLY_INCLUDE_FEEDID,
DOMAIN,
SYNC_MODE,
SYNC_MODE_AUTO,
SYNC_MODE_MANUAL,
)
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_API_KEY, CONF_URL
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from . import setup_integration
from .conftest import EMONCMS_FAILURE, SENSOR_NAME
from .conftest import EMONCMS_FAILURE, FLOW_RESULT, SENSOR_NAME
from tests.common import MockConfigEntry
@ -19,12 +25,29 @@ USER_INPUT = {
}
async def test_user_flow(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
emoncms_client: AsyncMock,
async def test_user_flow_failure(
hass: HomeAssistant, emoncms_client: AsyncMock
) -> None:
"""Test we get the user form."""
"""Test emoncms failure when adding a new entry."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
emoncms_client.async_request.return_value = EMONCMS_FAILURE
assert result["type"] is FlowResultType.FORM
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
assert result["errors"]["base"] == "api_error"
assert result["description_placeholders"]["details"] == "failure"
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
async def test_user_flow_manual_mode(
hass: HomeAssistant, mock_setup_entry: AsyncMock, emoncms_client: AsyncMock
) -> None:
"""Test we get the user forms and the entry in manual mode."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
@ -33,11 +56,10 @@ async def test_user_flow(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
{**USER_INPUT, SYNC_MODE: SYNC_MODE_MANUAL},
)
assert result["type"] is FlowResultType.FORM
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_ONLY_INCLUDE_FEEDID: ["1"]},
@ -46,16 +68,32 @@ async def test_user_flow(
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == SENSOR_NAME
assert result["data"] == {**USER_INPUT, CONF_ONLY_INCLUDE_FEEDID: ["1"]}
# assert len(mock_setup_entry.mock_calls) == 1
async def test_user_flow_auto_mode(
hass: HomeAssistant, mock_setup_entry: AsyncMock, emoncms_client: AsyncMock
) -> None:
"""Test we get the user form and the entry in automatic mode."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{**USER_INPUT, SYNC_MODE: SYNC_MODE_AUTO},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == SENSOR_NAME
assert result["data"] == {
**USER_INPUT,
CONF_ONLY_INCLUDE_FEEDID: FLOW_RESULT[CONF_ONLY_INCLUDE_FEEDID],
}
assert len(mock_setup_entry.mock_calls) == 1
CONFIG_ENTRY = {
CONF_API_KEY: "my_api_key",
CONF_ONLY_INCLUDE_FEEDID: ["1"],
CONF_URL: "http://1.1.1.1",
}
async def test_options_flow(
hass: HomeAssistant,
emoncms_client: AsyncMock,
@ -80,13 +118,12 @@ async def test_options_flow(
async def test_options_flow_failure(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
emoncms_client: AsyncMock,
config_entry: MockConfigEntry,
) -> None:
"""Options flow - test failure."""
emoncms_client.async_request.return_value = EMONCMS_FAILURE
await setup_integration(hass, config_entry)
emoncms_client.async_request.return_value = EMONCMS_FAILURE
result = await hass.config_entries.options.async_init(config_entry.entry_id)
await hass.async_block_till_done()
assert result["errors"]["base"] == "api_error"