test_config_flow.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "station_id": "876543210987654321",
  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. "station_id": "876543210987654321",
  42. "device_id": "123456789012345678",
  43. "time_zone": "Asia/Manila",
  44. },
  45. )
  46. assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
  47. assert result["title"] == "Solar Station 876543210987654321"
  48. assert result["data"] == {
  49. "iot_token": "test_token",
  50. "station_id": "876543210987654321",
  51. "device_id": "123456789012345678",
  52. "time_zone": "Asia/Manila",
  53. }