Allow hostname for nibe heatpump (#80793)

Allow hostname for nibe
This commit is contained in:
Joakim Plate 2022-10-24 14:17:53 +02:00 committed by GitHub
parent f7982a0db2
commit ebfb10c177
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 13 deletions

View File

@ -2,6 +2,7 @@
from __future__ import annotations
import errno
from socket import gaierror
from typing import Any
from nibe.connection.nibegw import NibeGW
@ -14,7 +15,6 @@ from homeassistant.const import CONF_IP_ADDRESS, CONF_MODEL
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import config_validation as cv
from homeassistant.util.network import is_ipv4_address
from .const import (
CONF_CONNECTION_TYPE,
@ -51,9 +51,6 @@ class FieldError(Exception):
async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
"""Validate the user input allows us to connect."""
if not is_ipv4_address(data[CONF_IP_ADDRESS]):
raise FieldError("Not a valid ipv4 address", CONF_IP_ADDRESS, "address")
heatpump = HeatPump(Model[data[CONF_MODEL]])
heatpump.initialize()
@ -79,6 +76,8 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
coil = await connection.read_coil(coil)
word_swap = coil.value == "ON"
coil = await connection.write_coil(coil)
except gaierror as exception:
raise FieldError(str(exception), "ip_address", "address") from exception
except CoilNotFoundException as exception:
raise FieldError(
"Model selected doesn't seem to support expected coils", "base", "model"

View File

@ -3,7 +3,7 @@
"step": {
"user": {
"data": {
"ip_address": "Remote IP address",
"ip_address": "Remote address",
"remote_read_port": "Remote read port",
"remote_write_port": "Remote write port",
"listening_port": "Local listening port"
@ -13,7 +13,7 @@
"error": {
"write": "Error on write request to pump. Verify your `Remote write port` or `Remote IP address`.",
"read": "Error on read request from pump. Verify your `Remote read port` or `Remote IP address`.",
"address": "Invalid remote IP address specified. Address must be a IPV4 address.",
"address": "Invalid remote address specified. Address must be an IP address or a resolvable hostname.",
"address_in_use": "The selected listening port is already in use on this system.",
"model": "The model selected doesn't seem to support modbus40",
"unknown": "[%key:common::config_flow::error::unknown%]"

View File

@ -1,10 +1,7 @@
{
"config": {
"abort": {
"already_configured": "Device is already configured"
},
"error": {
"address": "Invalid remote IP address specified. Address must be a IPV4 address.",
"address": "Invalid remote address specified. Address must be an IP address or a resolvable hostname.",
"address_in_use": "The selected listening port is already in use on this system.",
"model": "The model selected doesn't seem to support modbus40",
"read": "Error on read request from pump. Verify your `Remote read port` or `Remote IP address`.",
@ -14,7 +11,7 @@
"step": {
"user": {
"data": {
"ip_address": "Remote IP address",
"ip_address": "Remote address",
"listening_port": "Local listening port",
"remote_read_port": "Remote read port",
"remote_write_port": "Remote write port"

View File

@ -1,5 +1,6 @@
"""Test the Nibe Heat Pump config flow."""
import errno
from socket import gaierror
from unittest.mock import Mock, patch
from nibe.coil import Coil
@ -150,13 +151,13 @@ async def test_unexpected_exception(hass: HomeAssistant, mock_connection: Mock)
assert result2["errors"] == {"base": "unknown"}
async def test_invalid_ip(hass: HomeAssistant, mock_connection: Mock) -> None:
async def test_invalid_host(hass: HomeAssistant, mock_connection: Mock) -> None:
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
mock_connection.return_value.read_coil.side_effect = Exception()
mock_connection.return_value.read_coil.side_effect = gaierror()
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {**MOCK_FLOW_USERDATA, "ip_address": "abcd"}