Use DeviceInfo in huawei-lte (#58398)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-10-25 13:17:55 +02:00 committed by GitHub
parent 48c85fb839
commit cfe1bbcda0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,6 +31,8 @@ from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
ATTR_MODEL,
ATTR_SW_VERSION,
CONF_MAC,
CONF_NAME,
CONF_PASSWORD,
@ -371,10 +373,10 @@ async def async_setup_entry( # noqa: C901
await hass.async_add_executor_job(router.update)
# Check that we found required information
device_info = router.data.get(KEY_DEVICE_INFORMATION)
router_info = router.data.get(KEY_DEVICE_INFORMATION)
if not entry.unique_id:
# Transitional from < 2021.8: update None config entry and entity unique ids
if device_info and (serial_number := device_info.get("SerialNumber")):
if router_info and (serial_number := router_info.get("SerialNumber")):
hass.config_entries.async_update_entry(entry, unique_id=serial_number)
ent_reg = entity_registry.async_get(hass)
for entity_entry in entity_registry.async_entries_for_config_entry(
@ -419,35 +421,36 @@ async def async_setup_entry( # noqa: C901
except Exception: # pylint: disable=broad-except
# Assume not supported, or authentication required but in unauthenticated mode
wlan_settings = {}
macs = get_device_macs(device_info or {}, wlan_settings)
macs = get_device_macs(router_info or {}, wlan_settings)
# Be careful not to overwrite a previous, more complete set with a partial one
if macs and (not entry.data[CONF_MAC] or (device_info and wlan_settings)):
if macs and (not entry.data[CONF_MAC] or (router_info and wlan_settings)):
new_data = dict(entry.data)
new_data[CONF_MAC] = macs
hass.config_entries.async_update_entry(entry, data=new_data)
# Set up device registry
if router.device_identifiers or router.device_connections:
device_data = {}
device_info = DeviceInfo(
connections=router.device_connections,
identifiers=router.device_identifiers,
name=router.device_name,
manufacturer="Huawei",
)
sw_version = None
if device_info:
sw_version = device_info.get("SoftwareVersion")
if device_info.get("DeviceName"):
device_data["model"] = device_info["DeviceName"]
if router_info:
sw_version = router_info.get("SoftwareVersion")
if router_info.get("DeviceName"):
device_info[ATTR_MODEL] = router_info["DeviceName"]
if not sw_version and router.data.get(KEY_DEVICE_BASIC_INFORMATION):
sw_version = router.data[KEY_DEVICE_BASIC_INFORMATION].get(
"SoftwareVersion"
)
if sw_version:
device_data["sw_version"] = sw_version
device_info[ATTR_SW_VERSION] = sw_version
device_registry = await dr.async_get_registry(hass)
device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
connections=router.device_connections,
identifiers=router.device_identifiers,
name=router.device_name,
manufacturer="Huawei",
**device_data,
**device_info,
)
# Forward config entry setup to platforms