Skip to content

Commit

Permalink
Add check for working connection in setup
Browse files Browse the repository at this point in the history
  • Loading branch information
MadOne committed Oct 22, 2024
1 parent cc103e7 commit d32c4dd
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion custom_components/weishaupt_modbus/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from pymodbus.client import AsyncModbusTcpClient

# from . import wp
from .const import CONST
Expand Down Expand Up @@ -33,7 +34,8 @@ async def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]:
# throw CannotConnect
# If the authentication is wrong:
# InvalidAuth

if not await validateModbusConnection(data["host"], data["port"]):
raise CannotConnect
# Return info that you want to store in the config entry.
# "Title" is what is displayed to the user for this hub device
# It is stored internally in HA as part of the device config.
Expand Down Expand Up @@ -72,9 +74,24 @@ async def async_step_user(self, user_input=None):
)


<<<<<<< HEAD
=======
async def validateModbusConnection(host, port):
"""Validate the host."""
client = AsyncModbusTcpClient(host=host, port=port)
await client.connect()
return client.connected


>>>>>>> d34be90 (Add check for working connection in setup)
class InvalidHost(exceptions.HomeAssistantError):
"""Error to indicate there is an invalid hostname."""


<<<<<<< HEAD
class ConnectionFailed(exceptions.HomeAssistantError):
"""Error to indicate there is an invalid hostname."""
=======
class CannotConnect(exceptions.HomeAssistantError):
"""Error to indicate that the connecton to the heatpump failed."""
>>>>>>> d34be90 (Add check for working connection in setup)

0 comments on commit d32c4dd

Please sign in to comment.