Use name in ESPHome discovery title (#24100)

* Use name in ESPHome discovery title

* Add test

* Lint
This commit is contained in:
Otto Winter 2019-05-26 13:48:05 +02:00 committed by Pascal Vizeli
parent 0194905e97
commit 9438dd1cbd
3 changed files with 24 additions and 18 deletions

View File

@ -50,6 +50,11 @@ class EsphomeFlowHandler(config_entries.ConfigFlow):
if error is not None:
return await self.async_step_user(error=error)
self._name = device_info.name
# pylint: disable=unsupported-assignment-operation
self.context['title_placeholders'] = {
'name': self._name
}
# Only show authentication step if device uses password
if device_info.uses_password:
return await self.async_step_authenticate()

View File

@ -26,9 +26,10 @@
},
"discovery_confirm": {
"description": "Do you want to add the ESPHome node `{name}` to Home Assistant?",
"title": "Discovered ESPHome node"
"title": "Discovered ESPHome node"
}
},
"title": "ESPHome"
"title": "ESPHome",
"flow_title": "ESPHome: {name}"
}
}

View File

@ -45,10 +45,16 @@ def mock_api_connection_error():
yield mock_error
async def test_user_connection_works(hass, mock_client):
"""Test we can finish a config flow."""
def _setup_flow_handler(hass):
flow = config_flow.EsphomeFlowHandler()
flow.hass = hass
flow.context = {}
return flow
async def test_user_connection_works(hass, mock_client):
"""Test we can finish a config flow."""
flow = _setup_flow_handler(hass)
result = await flow.async_step_user(user_input=None)
assert result['type'] == 'form'
@ -78,8 +84,7 @@ async def test_user_connection_works(hass, mock_client):
async def test_user_resolve_error(hass, mock_api_connection_error,
mock_client):
"""Test user step with IP resolve error."""
flow = config_flow.EsphomeFlowHandler()
flow.hass = hass
flow = _setup_flow_handler(hass)
await flow.async_step_user(user_input=None)
class MockResolveError(mock_api_connection_error):
@ -111,8 +116,7 @@ async def test_user_resolve_error(hass, mock_api_connection_error,
async def test_user_connection_error(hass, mock_api_connection_error,
mock_client):
"""Test user step with connection error."""
flow = config_flow.EsphomeFlowHandler()
flow.hass = hass
flow = _setup_flow_handler(hass)
await flow.async_step_user(user_input=None)
mock_client.device_info.side_effect = mock_api_connection_error
@ -134,8 +138,7 @@ async def test_user_connection_error(hass, mock_api_connection_error,
async def test_user_with_password(hass, mock_client):
"""Test user step with password."""
flow = config_flow.EsphomeFlowHandler()
flow.hass = hass
flow = _setup_flow_handler(hass)
await flow.async_step_user(user_input=None)
mock_client.device_info.return_value = mock_coro(
@ -165,8 +168,7 @@ async def test_user_with_password(hass, mock_client):
async def test_user_invalid_password(hass, mock_api_connection_error,
mock_client):
"""Test user step with invalid password."""
flow = config_flow.EsphomeFlowHandler()
flow.hass = hass
flow = _setup_flow_handler(hass)
await flow.async_step_user(user_input=None)
mock_client.device_info.return_value = mock_coro(
@ -190,8 +192,7 @@ async def test_user_invalid_password(hass, mock_api_connection_error,
async def test_discovery_initiation(hass, mock_client):
"""Test discovery importing works."""
flow = config_flow.EsphomeFlowHandler()
flow.hass = hass
flow = _setup_flow_handler(hass)
service_info = {
'host': '192.168.43.183',
'port': 6053,
@ -206,6 +207,7 @@ async def test_discovery_initiation(hass, mock_client):
assert result['type'] == 'form'
assert result['step_id'] == 'discovery_confirm'
assert result['description_placeholders']['name'] == 'test8266'
assert flow.context['title_placeholders']['name'] == 'test8266'
result = await flow.async_step_discovery_confirm(user_input={})
assert result['type'] == 'create_entry'
@ -221,8 +223,7 @@ async def test_discovery_already_configured_hostname(hass, mock_client):
data={'host': 'test8266.local', 'port': 6053, 'password': ''}
).add_to_hass(hass)
flow = config_flow.EsphomeFlowHandler()
flow.hass = hass
flow = _setup_flow_handler(hass)
service_info = {
'host': '192.168.43.183',
'port': 6053,
@ -241,8 +242,7 @@ async def test_discovery_already_configured_ip(hass, mock_client):
data={'host': '192.168.43.183', 'port': 6053, 'password': ''}
).add_to_hass(hass)
flow = config_flow.EsphomeFlowHandler()
flow.hass = hass
flow = _setup_flow_handler(hass)
service_info = {
'host': '192.168.43.183',
'port': 6053,