Skip to content

Commit

Permalink
Use device_registry for HomeWizard device_info (home-assistant#82921)
Browse files Browse the repository at this point in the history
* Use global device and use didentifiers to get device_info

* Remove unused config_entry
  • Loading branch information
DCSBL authored Nov 29, 2022
1 parent f0b9795 commit 982966a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
13 changes: 12 additions & 1 deletion homeassistant/components/homewizard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions homeassistant/components/homewizard/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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
Expand All @@ -30,6 +31,13 @@ def __init__(
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."""

Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/homewizard/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ def __init__(
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."""
Expand Down
14 changes: 2 additions & 12 deletions homeassistant/components/homewizard/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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
Expand Down Expand Up @@ -171,6 +171,7 @@ def __init__(
# 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
Expand All @@ -181,17 +182,6 @@ def __init__(
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."""
Expand Down
8 changes: 1 addition & 7 deletions homeassistant/components/homewizard/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ def __init__(
"""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):
Expand Down

0 comments on commit 982966a

Please sign in to comment.