mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Save mysensors gateway type in config entry (#46981)
This commit is contained in:
parent
eccdae60bf
commit
1c7c6163dd
@ -19,6 +19,7 @@ from homeassistant.components.mysensors import (
|
|||||||
is_persistence_file,
|
is_persistence_file,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import callback
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
from . import CONF_RETAIN, CONF_VERSION, DEFAULT_VERSION
|
from . import CONF_RETAIN, CONF_VERSION, DEFAULT_VERSION
|
||||||
@ -99,6 +100,10 @@ def _is_same_device(
|
|||||||
class MySensorsConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
class MySensorsConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow."""
|
"""Handle a config flow."""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
"""Set up config flow."""
|
||||||
|
self._gw_type: Optional[str] = None
|
||||||
|
|
||||||
async def async_step_import(self, user_input: Optional[Dict[str, str]] = None):
|
async def async_step_import(self, user_input: Optional[Dict[str, str]] = None):
|
||||||
"""Import a config entry.
|
"""Import a config entry.
|
||||||
|
|
||||||
@ -130,7 +135,7 @@ class MySensorsConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
schema = vol.Schema(schema)
|
schema = vol.Schema(schema)
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
gw_type = user_input[CONF_GATEWAY_TYPE]
|
gw_type = self._gw_type = user_input[CONF_GATEWAY_TYPE]
|
||||||
input_pass = user_input if CONF_DEVICE in user_input else None
|
input_pass = user_input if CONF_DEVICE in user_input else None
|
||||||
if gw_type == CONF_GATEWAY_TYPE_MQTT:
|
if gw_type == CONF_GATEWAY_TYPE_MQTT:
|
||||||
return await self.async_step_gw_mqtt(input_pass)
|
return await self.async_step_gw_mqtt(input_pass)
|
||||||
@ -149,9 +154,7 @@ class MySensorsConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
await self.validate_common(CONF_GATEWAY_TYPE_SERIAL, errors, user_input)
|
await self.validate_common(CONF_GATEWAY_TYPE_SERIAL, errors, user_input)
|
||||||
)
|
)
|
||||||
if not errors:
|
if not errors:
|
||||||
return self.async_create_entry(
|
return self._async_create_entry(user_input)
|
||||||
title=f"{user_input[CONF_DEVICE]}", data=user_input
|
|
||||||
)
|
|
||||||
|
|
||||||
schema = _get_schema_common()
|
schema = _get_schema_common()
|
||||||
schema[
|
schema[
|
||||||
@ -177,9 +180,7 @@ class MySensorsConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
await self.validate_common(CONF_GATEWAY_TYPE_TCP, errors, user_input)
|
await self.validate_common(CONF_GATEWAY_TYPE_TCP, errors, user_input)
|
||||||
)
|
)
|
||||||
if not errors:
|
if not errors:
|
||||||
return self.async_create_entry(
|
return self._async_create_entry(user_input)
|
||||||
title=f"{user_input[CONF_DEVICE]}", data=user_input
|
|
||||||
)
|
|
||||||
|
|
||||||
schema = _get_schema_common()
|
schema = _get_schema_common()
|
||||||
schema[vol.Required(CONF_DEVICE, default="127.0.0.1")] = str
|
schema[vol.Required(CONF_DEVICE, default="127.0.0.1")] = str
|
||||||
@ -228,9 +229,8 @@ class MySensorsConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
await self.validate_common(CONF_GATEWAY_TYPE_MQTT, errors, user_input)
|
await self.validate_common(CONF_GATEWAY_TYPE_MQTT, errors, user_input)
|
||||||
)
|
)
|
||||||
if not errors:
|
if not errors:
|
||||||
return self.async_create_entry(
|
return self._async_create_entry(user_input)
|
||||||
title=f"{user_input[CONF_DEVICE]}", data=user_input
|
|
||||||
)
|
|
||||||
schema = _get_schema_common()
|
schema = _get_schema_common()
|
||||||
schema[vol.Required(CONF_RETAIN, default=True)] = bool
|
schema[vol.Required(CONF_RETAIN, default=True)] = bool
|
||||||
schema[vol.Required(CONF_TOPIC_IN_PREFIX)] = str
|
schema[vol.Required(CONF_TOPIC_IN_PREFIX)] = str
|
||||||
@ -241,6 +241,16 @@ class MySensorsConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
step_id="gw_mqtt", data_schema=schema, errors=errors
|
step_id="gw_mqtt", data_schema=schema, errors=errors
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _async_create_entry(
|
||||||
|
self, user_input: Optional[Dict[str, str]] = None
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""Create the config entry."""
|
||||||
|
return self.async_create_entry(
|
||||||
|
title=f"{user_input[CONF_DEVICE]}",
|
||||||
|
data={**user_input, CONF_GATEWAY_TYPE: self._gw_type},
|
||||||
|
)
|
||||||
|
|
||||||
def _normalize_persistence_file(self, path: str) -> str:
|
def _normalize_persistence_file(self, path: str) -> str:
|
||||||
return os.path.realpath(os.path.normcase(self.hass.config.path(path)))
|
return os.path.realpath(os.path.normcase(self.hass.config.path(path)))
|
||||||
|
|
||||||
|
@ -81,6 +81,7 @@ async def test_config_mqtt(hass: HomeAssistantType):
|
|||||||
CONF_TOPIC_IN_PREFIX: "bla",
|
CONF_TOPIC_IN_PREFIX: "bla",
|
||||||
CONF_TOPIC_OUT_PREFIX: "blub",
|
CONF_TOPIC_OUT_PREFIX: "blub",
|
||||||
CONF_VERSION: "2.4",
|
CONF_VERSION: "2.4",
|
||||||
|
CONF_GATEWAY_TYPE: "MQTT",
|
||||||
}
|
}
|
||||||
assert len(mock_setup.mock_calls) == 1
|
assert len(mock_setup.mock_calls) == 1
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
@ -120,6 +121,7 @@ async def test_config_serial(hass: HomeAssistantType):
|
|||||||
CONF_DEVICE: "/dev/ttyACM0",
|
CONF_DEVICE: "/dev/ttyACM0",
|
||||||
CONF_BAUD_RATE: 115200,
|
CONF_BAUD_RATE: 115200,
|
||||||
CONF_VERSION: "2.4",
|
CONF_VERSION: "2.4",
|
||||||
|
CONF_GATEWAY_TYPE: "Serial",
|
||||||
}
|
}
|
||||||
assert len(mock_setup.mock_calls) == 1
|
assert len(mock_setup.mock_calls) == 1
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
@ -156,6 +158,7 @@ async def test_config_tcp(hass: HomeAssistantType):
|
|||||||
CONF_DEVICE: "127.0.0.1",
|
CONF_DEVICE: "127.0.0.1",
|
||||||
CONF_TCP_PORT: 5003,
|
CONF_TCP_PORT: 5003,
|
||||||
CONF_VERSION: "2.4",
|
CONF_VERSION: "2.4",
|
||||||
|
CONF_GATEWAY_TYPE: "TCP",
|
||||||
}
|
}
|
||||||
assert len(mock_setup.mock_calls) == 1
|
assert len(mock_setup.mock_calls) == 1
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user