test_config_flow.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """Test the Solar of Things config flow."""
  2. from unittest.mock import patch
  3. import pytest
  4. from homeassistant import config_entries, data_entry_flow
  5. from homeassistant.core import HomeAssistant
  6. from custom_components.solar_of_things.const import DOMAIN
  7. async def test_form(hass: HomeAssistant) -> None:
  8. """Test we get the form."""
  9. result = await hass.config_entries.flow.async_init(
  10. DOMAIN, context={"source": config_entries.SOURCE_USER}
  11. )
  12. assert result["type"] == data_entry_flow.FlowResultType.FORM
  13. assert result["errors"] == {}
  14. async def test_form_cannot_connect(hass: HomeAssistant) -> None:
  15. """Test we handle cannot connect error."""
  16. with patch(
  17. "custom_components.solar_of_things.api.SolarOfThingsAPI.test_connection",
  18. return_value=False,
  19. ):
  20. result = await hass.config_entries.flow.async_init(
  21. DOMAIN,
  22. context={"source": config_entries.SOURCE_USER},
  23. data={
  24. "iot_token": "test_token",
  25. "device_id": "123456789012345678",
  26. },
  27. )
  28. assert result["type"] == data_entry_flow.FlowResultType.FORM
  29. assert result["errors"] == {"base": "cannot_connect"}
  30. async def test_form_success(hass: HomeAssistant) -> None:
  31. """Test successful configuration."""
  32. with patch(
  33. "custom_components.solar_of_things.api.SolarOfThingsAPI.test_connection",
  34. return_value=True,
  35. ):
  36. result = await hass.config_entries.flow.async_init(
  37. DOMAIN,
  38. context={"source": config_entries.SOURCE_USER},
  39. data={
  40. "iot_token": "test_token",
  41. "device_id": "123456789012345678",
  42. "station_id": "876543210987654321",
  43. },
  44. )
  45. assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
  46. assert result["title"] == "Solar Station 876543210987654321"
  47. assert result["data"] == {
  48. "iot_token": "test_token",
  49. "device_id": "123456789012345678",
  50. "station_id": "876543210987654321",
  51. }