diff --git a/homeassistant/components/homewizard/__init__.py b/homeassistant/components/homewizard/__init__.py index ec43cdfdd2e..a236c392c07 100644 --- a/homeassistant/components/homewizard/__init__.py +++ b/homeassistant/components/homewizard/__init__.py @@ -5,7 +5,7 @@ from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.const import CONF_IP_ADDRESS from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady -from homeassistant.helpers import entity_registry as er +from homeassistant.helpers import device_registry as dr, entity_registry as er from .const import DOMAIN, PLATFORMS from .coordinator import HWEnergyDeviceUpdateCoordinator as Coordinator @@ -71,6 +71,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await coordinator.api.close() raise + # Register device + device_registry = dr.async_get(hass) + device_registry.async_get_or_create( + config_entry_id=entry.entry_id, + name=entry.title, + manufacturer="HomeWizard", + sw_version=coordinator.data["device"].firmware_version, + model=coordinator.data["device"].product_type, + identifiers={(DOMAIN, coordinator.data["device"].serial)}, + ) + # Finalize hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = coordinator diff --git a/homeassistant/components/homewizard/coordinator.py b/homeassistant/components/homewizard/coordinator.py index 4397d77a3ff..dc441836d9a 100644 --- a/homeassistant/components/homewizard/coordinator.py +++ b/homeassistant/components/homewizard/coordinator.py @@ -8,6 +8,7 @@ from homewizard_energy.errors import DisabledError, RequestError from homeassistant.core import HomeAssistant from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .const import DOMAIN, UPDATE_INTERVAL, DeviceResponseEntry @@ -30,6 +31,13 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry] super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL) self.api = HomeWizardEnergy(host, clientsession=async_get_clientsession(hass)) + @property + def device_info(self) -> DeviceInfo: + """Return device_info.""" + return DeviceInfo( + identifiers={(DOMAIN, self.data["device"].serial)}, + ) + async def _async_update_data(self) -> DeviceResponseEntry: """Fetch all device and sensor data from api.""" diff --git a/homeassistant/components/homewizard/number.py b/homeassistant/components/homewizard/number.py index 6acbbcfa37b..783841168ed 100644 --- a/homeassistant/components/homewizard/number.py +++ b/homeassistant/components/homewizard/number.py @@ -50,9 +50,7 @@ class HWEnergyNumberEntity( self._attr_name = "Status light brightness" self._attr_native_unit_of_measurement = PERCENTAGE self._attr_icon = "mdi:lightbulb-on" - self._attr_device_info = { - "identifiers": {(DOMAIN, coordinator.data["device"].serial)}, - } + self._attr_device_info = coordinator.device_info async def async_set_native_value(self, value: float) -> None: """Set a new value.""" diff --git a/homeassistant/components/homewizard/sensor.py b/homeassistant/components/homewizard/sensor.py index 4baaff8835d..493b5f1c0e3 100644 --- a/homeassistant/components/homewizard/sensor.py +++ b/homeassistant/components/homewizard/sensor.py @@ -18,7 +18,7 @@ from homeassistant.const import ( VOLUME_CUBIC_METERS, ) from homeassistant.core import HomeAssistant -from homeassistant.helpers.entity import DeviceInfo, EntityCategory +from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -171,6 +171,7 @@ class HWEnergySensor(CoordinatorEntity[HWEnergyDeviceUpdateCoordinator], SensorE # Config attributes. self.data_type = description.key self._attr_unique_id = f"{entry.unique_id}_{description.key}" + self._attr_device_info = coordinator.device_info # Special case for export, not everyone has solarpanels # The chance that 'export' is non-zero when you have solar panels is nil @@ -181,17 +182,6 @@ class HWEnergySensor(CoordinatorEntity[HWEnergyDeviceUpdateCoordinator], SensorE if self.native_value == 0: self._attr_entity_registry_enabled_default = False - @property - def device_info(self) -> DeviceInfo: - """Return device information.""" - return { - "name": self.entry.title, - "manufacturer": "HomeWizard", - "sw_version": self.data["device"].firmware_version, - "model": self.data["device"].product_type, - "identifiers": {(DOMAIN, self.data["device"].serial)}, - } - @property def data(self) -> DeviceResponseEntry: """Return data object from DataUpdateCoordinator.""" diff --git a/homeassistant/components/homewizard/switch.py b/homeassistant/components/homewizard/switch.py index 7bf7d9a741e..8a40087403e 100644 --- a/homeassistant/components/homewizard/switch.py +++ b/homeassistant/components/homewizard/switch.py @@ -54,13 +54,7 @@ class HWEnergySwitchEntity( """Initialize the switch.""" super().__init__(coordinator) self._attr_unique_id = f"{entry.unique_id}_{key}" - self._attr_device_info = { - "name": entry.title, - "manufacturer": "HomeWizard", - "sw_version": coordinator.data["device"].firmware_version, - "model": coordinator.data["device"].product_type, - "identifiers": {(DOMAIN, coordinator.data["device"].serial)}, - } + self._attr_device_info = coordinator.device_info class HWEnergyMainSwitchEntity(HWEnergySwitchEntity):