Skip to content

Commit

Permalink
Refactor config flow
Browse files Browse the repository at this point in the history
  • Loading branch information
briis committed Jan 25, 2024
1 parent c9c130e commit b495c4e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
9 changes: 7 additions & 2 deletions custom_components/meteobridgesql/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
)
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from pymeteobridgesql import MeteobridgeSQL, MeteobridgeSQLDatabaseConnectionError
from pymeteobridgesql import MeteobridgeSQL, MeteobridgeSQLDatabaseConnectionError, MeteobridgeSQLDataError, StationData
from .const import (CONF_DATABASE, DEFAULT_PORT, DOMAIN)

_LOGGER = logging.getLogger(__name__)
Expand All @@ -41,16 +41,21 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None) -> Flo
try:
meteobridge = MeteobridgeSQL(host=user_input[CONF_HOST],user=user_input[CONF_USERNAME],password=user_input[CONF_PASSWORD], database=user_input[CONF_DATABASE])
await meteobridge.async_init()
station_data: StationData = await meteobridge.async_get_station_data(user_input[CONF_MAC])
except MeteobridgeSQLDatabaseConnectionError as error:
_LOGGER.error("Error connecting to MySQL Database: %s", error)
errors["base"] = "cannot_connect"
return await self._show_setup_form(errors)
except MeteobridgeSQLDataError as error:
_LOGGER.error("Failed to lookup data in the database: %s", error)
errors["base"] = "no_data"
return await self._show_setup_form(errors)

await self.async_set_unique_id(user_input[CONF_MAC])
self._abort_if_unique_id_configured

return self.async_create_entry(
title=f"Meteobride SQL ({user_input[CONF_MAC]})",
title=f"Meteobride SQL ({station_data.mb_stationname})",
data={
CONF_MAC: user_input[CONF_MAC],
CONF_HOST: user_input[CONF_HOST],
Expand Down
2 changes: 1 addition & 1 deletion custom_components/meteobridgesql/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def __init__(
entry_type=DeviceEntryType.SERVICE,
manufacturer=MANUFACTURER,
model=_get_hw_platform(self.coordinator.data.sensor_data.mb_platform),
name=f"{self.coordinator.data.sensor_data.mb_stationname} Sensors",
name=f"{self.coordinator.data.sensor_data.mb_stationname} Sensor",
configuration_url=f"http://{self.coordinator.data.sensor_data.mb_ip}",
hw_version=f"{self.coordinator.data.sensor_data.mb_platform}",
sw_version=f"{self.coordinator.data.sensor_data.mb_swversion}-{self.coordinator.data.sensor_data.mb_buildnum}",
Expand Down
3 changes: 2 additions & 1 deletion custom_components/meteobridgesql/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"unique_id": "This Server has already been setup."
},
"error": {
"cannot_connect": "Cannot connect to the MySQL Database. Please check your input and try again"
"cannot_connect": "Cannot connect to the MySQL Database. Please check your input and try again.",
"no_data": "Cannot retrieve data from the database. Does the table exist?"
},
"step": {
"user": {
Expand Down

0 comments on commit b495c4e

Please sign in to comment.