Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Nibe Heat Pump integration."""
2 
3 from __future__ import annotations
4 
5 from nibe.connection import Connection
6 from nibe.connection.modbus import Modbus
7 from nibe.connection.nibegw import NibeGW, ProductInfo
8 from nibe.heatpump import HeatPump, Model
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import (
12  CONF_IP_ADDRESS,
13  CONF_MODEL,
14  EVENT_HOMEASSISTANT_STOP,
15  Platform,
16 )
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import HomeAssistantError
19 from homeassistant.helpers import device_registry as dr
20 
21 from .const import (
22  CONF_CONNECTION_TYPE,
23  CONF_CONNECTION_TYPE_MODBUS,
24  CONF_CONNECTION_TYPE_NIBEGW,
25  CONF_LISTENING_PORT,
26  CONF_MODBUS_UNIT,
27  CONF_MODBUS_URL,
28  CONF_REMOTE_READ_PORT,
29  CONF_REMOTE_WRITE_PORT,
30  CONF_WORD_SWAP,
31  DOMAIN,
32 )
33 from .coordinator import CoilCoordinator
34 
35 PLATFORMS: list[Platform] = [
36  Platform.BINARY_SENSOR,
37  Platform.BUTTON,
38  Platform.CLIMATE,
39  Platform.NUMBER,
40  Platform.SELECT,
41  Platform.SENSOR,
42  Platform.SWITCH,
43  Platform.WATER_HEATER,
44 ]
45 COIL_READ_RETRIES = 5
46 
47 
48 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
49  """Set up Nibe Heat Pump from a config entry."""
50 
51  heatpump = HeatPump(Model[entry.data[CONF_MODEL]])
52  heatpump.word_swap = entry.data.get(CONF_WORD_SWAP, True)
53  await heatpump.initialize()
54 
55  connection: Connection
56  connection_type = entry.data[CONF_CONNECTION_TYPE]
57 
58  if connection_type == CONF_CONNECTION_TYPE_NIBEGW:
59  connection = NibeGW(
60  heatpump,
61  entry.data[CONF_IP_ADDRESS],
62  entry.data[CONF_REMOTE_READ_PORT],
63  entry.data[CONF_REMOTE_WRITE_PORT],
64  listening_port=entry.data[CONF_LISTENING_PORT],
65  )
66  elif connection_type == CONF_CONNECTION_TYPE_MODBUS:
67  connection = Modbus(
68  heatpump, entry.data[CONF_MODBUS_URL], entry.data[CONF_MODBUS_UNIT]
69  )
70  else:
71  raise HomeAssistantError(f"Connection type {connection_type} is not supported.")
72 
73  await connection.start()
74 
75  assert heatpump.model
76 
77  async def _async_stop(_):
78  await connection.stop()
79 
80  entry.async_on_unload(
81  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_stop)
82  )
83 
84  coordinator = CoilCoordinator(hass, heatpump, connection)
85 
86  data = hass.data.setdefault(DOMAIN, {})
87  data[entry.entry_id] = coordinator
88 
89  reg = dr.async_get(hass)
90  device_entry = reg.async_get_or_create(
91  config_entry_id=entry.entry_id,
92  identifiers={(DOMAIN, entry.unique_id or entry.entry_id)},
93  manufacturer="NIBE Energy Systems",
94  name=heatpump.model.name,
95  )
96 
97  def _on_product_info(product_info: ProductInfo):
98  reg.async_update_device(
99  device_id=device_entry.id,
100  model=product_info.model,
101  sw_version=str(product_info.firmware_version),
102  )
103 
104  if hasattr(connection, "PRODUCT_INFO_EVENT") and hasattr(connection, "subscribe"):
105  connection.subscribe(connection.PRODUCT_INFO_EVENT, _on_product_info)
106  else:
107  reg.async_update_device(device_id=device_entry.id, model=heatpump.model.name)
108 
109  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
110 
111  # Trigger a refresh again now that all platforms have registered
112  hass.async_create_task(coordinator.async_refresh())
113  return True
114 
115 
116 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
117  """Unload a config entry."""
118  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
119  hass.data[DOMAIN].pop(entry.entry_id)
120 
121  return unload_ok
None _async_stop(HomeAssistant hass, bool restart)
Definition: __init__.py:392
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:48
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:116