Use device_registry for HomeWizard device_info (#82921)
* Use global device and use didentifiers to get device_info * Remove unused config_entry
This commit is contained in:
parent
f0b979556c
commit
982966a3d9
|
@ -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
|
||||
|
|
|
@ -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."""
|
||||
|
||||
|
|
|
@ -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."""
|
||||
|
|
|
@ -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."""
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue