Use ZeroconfServiceInfo in smappee (#60096)

This commit is contained in:
epenet 2021-11-21 22:50:29 +01:00 committed by GitHub
parent b465131170
commit 1c15544d7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 70 deletions

View file

@ -4,12 +4,13 @@ import logging
from pysmappee import helper, mqtt
import voluptuous as vol
from homeassistant.components import zeroconf
from homeassistant.const import CONF_HOST, CONF_IP_ADDRESS
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import config_entry_oauth2_flow
from . import api
from .const import (
CONF_HOSTNAME,
CONF_SERIALNUMBER,
DOMAIN,
ENV_CLOUD,
@ -36,14 +37,20 @@ class SmappeeFlowHandler(
"""Return logger."""
return logging.getLogger(__name__)
async def async_step_zeroconf(self, discovery_info):
async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult:
"""Handle zeroconf discovery."""
if not discovery_info[CONF_HOSTNAME].startswith(SUPPORTED_LOCAL_DEVICES):
if not discovery_info[zeroconf.ATTR_HOSTNAME].startswith(
SUPPORTED_LOCAL_DEVICES
):
return self.async_abort(reason="invalid_mdns")
serial_number = (
discovery_info[CONF_HOSTNAME].replace(".local.", "").replace("Smappee", "")
discovery_info[zeroconf.ATTR_HOSTNAME]
.replace(".local.", "")
.replace("Smappee", "")
)
# Check if already configured (local)
@ -56,7 +63,7 @@ class SmappeeFlowHandler(
self.context.update(
{
CONF_IP_ADDRESS: discovery_info["host"],
CONF_IP_ADDRESS: discovery_info[zeroconf.ATTR_HOST],
CONF_SERIALNUMBER: serial_number,
"title_placeholders": {"name": serial_number},
}

View file

@ -3,8 +3,8 @@ from http import HTTPStatus
from unittest.mock import patch
from homeassistant import data_entry_flow, setup
from homeassistant.components import zeroconf
from homeassistant.components.smappee.const import (
CONF_HOSTNAME,
CONF_SERIALNUMBER,
DOMAIN,
ENV_CLOUD,
@ -55,14 +55,14 @@ async def test_show_zeroconf_connection_error_form(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data={
"host": "1.2.3.4",
"port": 22,
CONF_HOSTNAME: "Smappee1006000212.local.",
"type": "_ssh._tcp.local.",
"name": "Smappee1006000212._ssh._tcp.local.",
"properties": {"_raw": {}},
},
data=zeroconf.ZeroconfServiceInfo(
host="1.2.3.4",
port=22,
hostname="Smappee1006000212.local.",
type="_ssh._tcp.local.",
name="Smappee1006000212._ssh._tcp.local.",
properties={"_raw": {}},
),
)
assert result["description_placeholders"] == {CONF_SERIALNUMBER: "1006000212"}
@ -84,14 +84,14 @@ async def test_show_zeroconf_connection_error_form_next_generation(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data={
"host": "1.2.3.4",
"port": 22,
CONF_HOSTNAME: "Smappee5001000212.local.",
"type": "_ssh._tcp.local.",
"name": "Smappee5001000212._ssh._tcp.local.",
"properties": {"_raw": {}},
},
data=zeroconf.ZeroconfServiceInfo(
host="1.2.3.4",
port=22,
hostname="Smappee5001000212.local.",
type="_ssh._tcp.local.",
name="Smappee5001000212._ssh._tcp.local.",
properties={"_raw": {}},
),
)
assert result["description_placeholders"] == {CONF_SERIALNUMBER: "5001000212"}
@ -166,14 +166,14 @@ async def test_zeroconf_wrong_mdns(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data={
"host": "1.2.3.4",
"port": 22,
CONF_HOSTNAME: "example.local.",
"type": "_ssh._tcp.local.",
"name": "example._ssh._tcp.local.",
"properties": {"_raw": {}},
},
data=zeroconf.ZeroconfServiceInfo(
host="1.2.3.4",
port=22,
hostname="example.local.",
type="_ssh._tcp.local.",
name="example._ssh._tcp.local.",
properties={"_raw": {}},
),
)
assert result["reason"] == "invalid_mdns"
@ -276,14 +276,14 @@ async def test_zeroconf_device_exists_abort(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data={
"host": "1.2.3.4",
"port": 22,
CONF_HOSTNAME: "Smappee1006000212.local.",
"type": "_ssh._tcp.local.",
"name": "Smappee1006000212._ssh._tcp.local.",
"properties": {"_raw": {}},
},
data=zeroconf.ZeroconfServiceInfo(
host="1.2.3.4",
port=22,
hostname="Smappee1006000212.local.",
type="_ssh._tcp.local.",
name="Smappee1006000212._ssh._tcp.local.",
properties={"_raw": {}},
),
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
@ -325,14 +325,14 @@ async def test_zeroconf_abort_if_cloud_device_exists(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data={
"host": "1.2.3.4",
"port": 22,
CONF_HOSTNAME: "Smappee1006000212.local.",
"type": "_ssh._tcp.local.",
"name": "Smappee1006000212._ssh._tcp.local.",
"properties": {"_raw": {}},
},
data=zeroconf.ZeroconfServiceInfo(
host="1.2.3.4",
port=22,
hostname="Smappee1006000212.local.",
type="_ssh._tcp.local.",
name="Smappee1006000212._ssh._tcp.local.",
properties={"_raw": {}},
),
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured_device"
@ -344,14 +344,14 @@ async def test_zeroconf_confirm_abort_if_cloud_device_exists(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data={
"host": "1.2.3.4",
"port": 22,
CONF_HOSTNAME: "Smappee1006000212.local.",
"type": "_ssh._tcp.local.",
"name": "Smappee1006000212._ssh._tcp.local.",
"properties": {"_raw": {}},
},
data=zeroconf.ZeroconfServiceInfo(
host="1.2.3.4",
port=22,
hostname="Smappee1006000212.local.",
type="_ssh._tcp.local.",
name="Smappee1006000212._ssh._tcp.local.",
properties={"_raw": {}},
),
)
config_entry = MockConfigEntry(
@ -463,14 +463,14 @@ async def test_full_zeroconf_flow(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data={
"host": "1.2.3.4",
"port": 22,
CONF_HOSTNAME: "Smappee1006000212.local.",
"type": "_ssh._tcp.local.",
"name": "Smappee1006000212._ssh._tcp.local.",
"properties": {"_raw": {}},
},
data=zeroconf.ZeroconfServiceInfo(
host="1.2.3.4",
port=22,
hostname="Smappee1006000212.local.",
type="_ssh._tcp.local.",
name="Smappee1006000212._ssh._tcp.local.",
properties={"_raw": {}},
),
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "zeroconf_confirm"
@ -538,14 +538,14 @@ async def test_full_zeroconf_flow_next_generation(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data={
"host": "1.2.3.4",
"port": 22,
CONF_HOSTNAME: "Smappee5001000212.local.",
"type": "_ssh._tcp.local.",
"name": "Smappee5001000212._ssh._tcp.local.",
"properties": {"_raw": {}},
},
data=zeroconf.ZeroconfServiceInfo(
host="1.2.3.4",
port=22,
hostname="Smappee5001000212.local.",
type="_ssh._tcp.local.",
name="Smappee5001000212._ssh._tcp.local.",
properties={"_raw": {}},
),
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "zeroconf_confirm"