Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Plugwise platform for Home Assistant Core."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import Platform
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers import device_registry as dr, entity_registry as er
11 
12 from .const import DOMAIN, LOGGER, PLATFORMS
13 from .coordinator import PlugwiseDataUpdateCoordinator
14 
15 type PlugwiseConfigEntry = ConfigEntry[PlugwiseDataUpdateCoordinator]
16 
17 
18 async def async_setup_entry(hass: HomeAssistant, entry: PlugwiseConfigEntry) -> bool:
19  """Set up Plugwise components from a config entry."""
20  await er.async_migrate_entries(hass, entry.entry_id, async_migrate_entity_entry)
21 
22  coordinator = PlugwiseDataUpdateCoordinator(hass)
23  await coordinator.async_config_entry_first_refresh()
24  migrate_sensor_entities(hass, coordinator)
25 
26  entry.runtime_data = coordinator
27 
28  device_registry = dr.async_get(hass)
29  device_registry.async_get_or_create(
30  config_entry_id=entry.entry_id,
31  identifiers={(DOMAIN, str(coordinator.api.gateway_id))},
32  manufacturer="Plugwise",
33  model=coordinator.api.smile_model,
34  model_id=coordinator.api.smile_model_id,
35  name=coordinator.api.smile_name,
36  sw_version=str(coordinator.api.smile_version),
37  ) # required for adding the entity-less P1 Gateway
38 
39  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
40 
41  return True
42 
43 
44 async def async_unload_entry(hass: HomeAssistant, entry: PlugwiseConfigEntry) -> bool:
45  """Unload the Plugwise components."""
46  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
47 
48 
49 @callback
50 def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None:
51  """Migrate Plugwise entity entries.
52 
53  - Migrates old unique ID's from old binary_sensors and switches to the new unique ID's
54  """
55  if entry.domain == Platform.BINARY_SENSOR and entry.unique_id.endswith(
56  "-slave_boiler_state"
57  ):
58  return {
59  "new_unique_id": entry.unique_id.replace(
60  "-slave_boiler_state", "-secondary_boiler_state"
61  )
62  }
63  if entry.domain == Platform.SENSOR and entry.unique_id.endswith(
64  "-relative_humidity"
65  ):
66  return {
67  "new_unique_id": entry.unique_id.replace("-relative_humidity", "-humidity")
68  }
69  if entry.domain == Platform.SWITCH and entry.unique_id.endswith("-plug"):
70  return {"new_unique_id": entry.unique_id.replace("-plug", "-relay")}
71 
72  # No migration needed
73  return None
74 
75 
77  hass: HomeAssistant,
78  coordinator: PlugwiseDataUpdateCoordinator,
79 ) -> None:
80  """Migrate Sensors if needed."""
81  ent_reg = er.async_get(hass)
82 
83  # Migrating opentherm_outdoor_temperature
84  # to opentherm_outdoor_air_temperature sensor
85  for device_id, device in coordinator.data.devices.items():
86  if device["dev_class"] != "heater_central":
87  continue
88 
89  old_unique_id = f"{device_id}-outdoor_temperature"
90  if entity_id := ent_reg.async_get_entity_id(
91  Platform.SENSOR, DOMAIN, old_unique_id
92  ):
93  new_unique_id = f"{device_id}-outdoor_air_temperature"
94  LOGGER.debug(
95  "Migrating entity %s from old unique ID '%s' to new unique ID '%s'",
96  entity_id,
97  old_unique_id,
98  new_unique_id,
99  )
100  ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id)
bool async_setup_entry(HomeAssistant hass, PlugwiseConfigEntry entry)
Definition: __init__.py:18
bool async_unload_entry(HomeAssistant hass, PlugwiseConfigEntry entry)
Definition: __init__.py:44
dict[str, Any]|None async_migrate_entity_entry(er.RegistryEntry entry)
Definition: __init__.py:50
None migrate_sensor_entities(HomeAssistant hass, PlugwiseDataUpdateCoordinator coordinator)
Definition: __init__.py:79