Home Assistant Unofficial Reference 2024.12.1
discovery.py
Go to the documentation of this file.
1 """Internal discovery service for iZone AC."""
2 
3 import logging
4 
5 import pizone
6 
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers import aiohttp_client
9 from homeassistant.helpers.dispatcher import async_dispatcher_send
10 
11 from .const import (
12  DATA_DISCOVERY_SERVICE,
13  DISPATCH_CONTROLLER_DISCONNECTED,
14  DISPATCH_CONTROLLER_DISCOVERED,
15  DISPATCH_CONTROLLER_RECONNECTED,
16  DISPATCH_CONTROLLER_UPDATE,
17  DISPATCH_ZONE_UPDATE,
18 )
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 
23 class DiscoveryService(pizone.Listener):
24  """Discovery data and interfacing with pizone library."""
25 
26  def __init__(self, hass: HomeAssistant) -> None:
27  """Initialise discovery service."""
28  super().__init__()
29  self.hasshass = hass
30  self.pi_disco: pizone.DiscoveryService | None = None
31 
32  # Listener interface
33  def controller_discovered(self, ctrl: pizone.Controller) -> None:
34  """Handle new controller discoverery."""
35  async_dispatcher_send(self.hasshass, DISPATCH_CONTROLLER_DISCOVERED, ctrl)
36 
37  def controller_disconnected(self, ctrl: pizone.Controller, ex: Exception) -> None:
38  """On disconnect from controller."""
39  async_dispatcher_send(self.hasshass, DISPATCH_CONTROLLER_DISCONNECTED, ctrl, ex)
40 
41  def controller_reconnected(self, ctrl: pizone.Controller) -> None:
42  """On reconnect to controller."""
43  async_dispatcher_send(self.hasshass, DISPATCH_CONTROLLER_RECONNECTED, ctrl)
44 
45  def controller_update(self, ctrl: pizone.Controller) -> None:
46  """System update message is received from the controller."""
47  async_dispatcher_send(self.hasshass, DISPATCH_CONTROLLER_UPDATE, ctrl)
48 
49  def zone_update(self, ctrl: pizone.Controller, zone: pizone.Zone) -> None:
50  """Zone update message is received from the controller."""
51  async_dispatcher_send(self.hasshass, DISPATCH_ZONE_UPDATE, ctrl, zone)
52 
53 
54 async def async_start_discovery_service(hass: HomeAssistant):
55  """Set up the pizone internal discovery."""
56  if disco := hass.data.get(DATA_DISCOVERY_SERVICE):
57  # Already started
58  return disco
59  _LOGGER.debug("Starting iZone Discovery Service")
60 
61  # discovery local services
62  disco = DiscoveryService(hass)
63  hass.data[DATA_DISCOVERY_SERVICE] = disco
64 
65  # Start the pizone discovery service, disco is the listener
66  session = aiohttp_client.async_get_clientsession(hass)
67  disco.pi_disco = pizone.discovery(disco, session=session)
68  await disco.pi_disco.start_discovery()
69 
70  return disco
71 
72 
73 async def async_stop_discovery_service(hass: HomeAssistant):
74  """Stop the discovery service."""
75  if not (disco := hass.data.get(DATA_DISCOVERY_SERVICE)):
76  return
77 
78  await disco.pi_disco.close()
79  del hass.data[DATA_DISCOVERY_SERVICE]
80 
81  _LOGGER.debug("Stopped iZone Discovery Service")
None controller_update(self, pizone.Controller ctrl)
Definition: discovery.py:45
None zone_update(self, pizone.Controller ctrl, pizone.Zone zone)
Definition: discovery.py:49
None controller_reconnected(self, pizone.Controller ctrl)
Definition: discovery.py:41
None controller_discovered(self, pizone.Controller ctrl)
Definition: discovery.py:33
None controller_disconnected(self, pizone.Controller ctrl, Exception ex)
Definition: discovery.py:37
def async_start_discovery_service(HomeAssistant hass)
Definition: discovery.py:54
def async_stop_discovery_service(HomeAssistant hass)
Definition: discovery.py:73
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193