Use dataclass properties in yeelight discovery (#60640)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-12-01 09:28:19 +01:00 committed by GitHub
parent 8240b8c72e
commit 73a4dba2ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 16 deletions

View File

@ -8,7 +8,7 @@ from yeelight.aio import AsyncBulb
from yeelight.main import get_known_models from yeelight.main import get_known_models
from homeassistant import config_entries, exceptions from homeassistant import config_entries, exceptions
from homeassistant.components import dhcp, zeroconf from homeassistant.components import dhcp, ssdp, zeroconf
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_DEVICE, CONF_HOST, CONF_ID, CONF_NAME from homeassistant.const import CONF_DEVICE, CONF_HOST, CONF_ID, CONF_NAME
from homeassistant.core import callback from homeassistant.core import callback
@ -60,28 +60,28 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self, discovery_info: zeroconf.ZeroconfServiceInfo self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult: ) -> FlowResult:
"""Handle discovery from homekit.""" """Handle discovery from homekit."""
self._discovered_ip = discovery_info[zeroconf.ATTR_HOST] self._discovered_ip = discovery_info.host
return await self._async_handle_discovery() return await self._async_handle_discovery()
async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult: async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Handle discovery from dhcp.""" """Handle discovery from dhcp."""
self._discovered_ip = discovery_info[dhcp.IP_ADDRESS] self._discovered_ip = discovery_info.ip
return await self._async_handle_discovery() return await self._async_handle_discovery()
async def async_step_zeroconf( async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult: ) -> FlowResult:
"""Handle discovery from zeroconf.""" """Handle discovery from zeroconf."""
self._discovered_ip = discovery_info[zeroconf.ATTR_HOST] self._discovered_ip = discovery_info.host
await self.async_set_unique_id( await self.async_set_unique_id(
"{0:#0{1}x}".format(int(discovery_info[zeroconf.ATTR_NAME][-26:-18]), 18) "{0:#0{1}x}".format(int(discovery_info.name[-26:-18]), 18)
) )
return await self._async_handle_discovery_with_unique_id() return await self._async_handle_discovery_with_unique_id()
async def async_step_ssdp(self, discovery_info): async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult:
"""Handle discovery from ssdp.""" """Handle discovery from ssdp."""
self._discovered_ip = urlparse(discovery_info["location"]).hostname self._discovered_ip = urlparse(discovery_info.ssdp_headers["location"]).hostname
await self.async_set_unique_id(discovery_info["id"]) await self.async_set_unique_id(discovery_info.ssdp_headers["id"])
return await self._async_handle_discovery_with_unique_id() return await self._async_handle_discovery_with_unique_id()
async def _async_handle_discovery_with_unique_id(self): async def _async_handle_discovery_with_unique_id(self):

View File

@ -16,5 +16,6 @@
"zeroconf": [{ "type": "_miio._udp.local.", "name": "yeelink-*" }], "zeroconf": [{ "type": "_miio._udp.local.", "name": "yeelink-*" }],
"homekit": { "homekit": {
"models": ["YL*"] "models": ["YL*"]
} },
"after_dependencies": ["ssdp"]
} }

View File

@ -8,7 +8,7 @@ from async_upnp_client.search import SsdpSearchListener
from yeelight import BulbException, BulbType from yeelight import BulbException, BulbType
from yeelight.main import _MODEL_SPECS from yeelight.main import _MODEL_SPECS
from homeassistant.components import zeroconf from homeassistant.components import ssdp, zeroconf
from homeassistant.components.yeelight import ( from homeassistant.components.yeelight import (
CONF_MODE_MUSIC, CONF_MODE_MUSIC,
CONF_NIGHTLIGHT_SWITCH_TYPE, CONF_NIGHTLIGHT_SWITCH_TYPE,
@ -179,11 +179,15 @@ def _patch_discovery(no_device=False, capabilities=None):
YeelightScanner._scanner = None # Clear class scanner to reset hass YeelightScanner._scanner = None # Clear class scanner to reset hass
def _generate_fake_ssdp_listener(*args, **kwargs): def _generate_fake_ssdp_listener(*args, **kwargs):
return _patched_ssdp_listener( info = None
None if no_device else capabilities or CAPABILITIES, if not no_device:
*args, info = ssdp.SsdpServiceInfo(
**kwargs, ssdp_usn="mock_usn",
) ssdp_st="mock_st",
upnp={},
ssdp_headers=capabilities or CAPABILITIES,
)
return _patched_ssdp_listener(info, *args, **kwargs)
return patch( return patch(
"homeassistant.components.yeelight.scanner.SsdpSearchListener", "homeassistant.components.yeelight.scanner.SsdpSearchListener",

View File

@ -55,7 +55,8 @@ DEFAULT_CONFIG = {
SSDP_INFO = ssdp.SsdpServiceInfo( SSDP_INFO = ssdp.SsdpServiceInfo(
ssdp_usn="mock_usn", ssdp_usn="mock_usn",
ssdp_st="mock_st", ssdp_st="mock_st",
upnp=CAPABILITIES, upnp={},
ssdp_headers=CAPABILITIES,
) )