8000 Use DeviceInfo in mqtt by epenet · Pull Request #58389 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use DeviceInfo in mqtt #58389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions homeassistant/components/mqtt/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
import voluptuous as vol

from homeassistant.const import (
ATTR_CONFIGURATION_URL,
ATTR_MANUFACTURER,
ATTR_MODEL,
ATTR_NAME,
ATTR_SUGGESTED_AREA,
ATTR_SW_VERSION,
ATTR_VIA_DEVICE,
CONF_DEVICE,
CONF_ENTITY_CATEGORY,
CONF_ICON,
Expand All @@ -21,7 +28,7 @@
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.entity import ENTITY_CATEGORIES_SCHEMA, Entity
from homeassistant.helpers.entity import ENTITY_CATEGORIES_SCHEMA, DeviceInfo, Entity
from homeassistant.helpers.typing import ConfigType

from . import DATA_MQTT, debug_info, publish, subscription
Expand Down Expand Up @@ -513,36 +520,36 @@ def _cleanup_discovery_on_remove(self) -> None:
self._remove_signal = None


def device_info_from_config(config):
def device_info_from_config(config) -> DeviceInfo | None:
"""Return a device description for device registry."""
if not config:
return None

info = {
"identifiers": {(DOMAIN, id_) for id_ in config[CONF_IDENTIFIERS]},
"connections": {tuple(x) for x in config[CONF_CONNECTIONS]},
}
info = DeviceInfo(
identifiers={(DOMAIN, id_) for id_ in config[CONF_IDENTIFIERS]},
connections={(conn_[0], conn_[1]) for conn_ in config[CONF_CONNECTIONS]},
)

if CONF_MANUFACTURER in config:
info["manufacturer"] = config[CONF_MANUFACTURER]
info[ATTR_MANUFACTURER] = config[CONF_MANUFACTURER]

if CONF_MODEL in config:
info["model"] = config[CONF_MODEL]
info[ATTR_MODEL] = config[CONF_MODEL]

if CONF_NAME in config:
info["name"] = config[CONF_NAME]
info[ATTR_NAME] = config[CONF_NAME]

if CONF_SW_VERSION in config:
info["sw_version"] = config[CONF_SW_VERSION]
info[ATTR_SW_VERSION] = config[CONF_SW_VERSION]

if CONF_VIA_DEVICE in config:
info["via_device"] = (DOMAIN, config[CONF_VIA_DEVICE])
info[ATTR_VIA_DEVICE] = (DOMAIN, config[CONF_VIA_DEVICE])

if CONF_SUGGESTED_AREA in config:
info["suggested_area"] = config[CONF_SUGGESTED_AREA]
info[ATTR_SUGGESTED_AREA] = config[CONF_SUGGESTED_AREA]

if CONF_CONFIGURATION_URL in config:
info["configuration_url"] = config[CONF_CONFIGURATION_URL]
info[ATTR_CONFIGURATION_URL] = config[CONF_CONFIGURATION_URL]

return info

Expand All @@ -563,11 +570,12 @@ async def device_info_discovery_update(self, config: dict):
device_info = self.device_info

if config_entry_id is not None and device_info is not None:
device_info["config_entry_id"] = config_entry_id
device_registry.async_get_or_create(**device_info)
device_registry.async_get_or_create(
config_entry_id=config_entry_id, **device_info
)

@property
def device_info(self):
def device_info(self) -> DeviceInfo | None:
"""Return a device description for device registry."""
return device_info_from_config(self._device_config)

Expand Down
0