Izone component (#24550)
* iZone component * Rename constants to const. * Changes as per code review. * Stop listening if discovery times out. * Unload properly * Changes as per code review * Climate 1.0 * Use dispatcher instead of listener * Free air settings * Test case for config flow. * Changes as per code review * Fix error on shutdown * Changes as per code review * Lint fix * Black formatting * Black on test * Fix test * Lint fix * Formatting * Updated requirements * Remaining patches * Per code r/v
This commit is contained in:
1 parent
c8fb7ce98b
commit
b68b8430a4
16 files changed
+894
No files matched your search
@@ -0,0 +1,87 @@
|
||||
"""Internal discovery service for iZone AC."""
|
||||
|
||||
import logging
|
||||
import pizone
|
||||
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.helpers import aiohttp_client
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
|
||||
from .const import (
|
||||
DATA_DISCOVERY_SERVICE,
|
||||
DISPATCH_CONTROLLER_DISCOVERED,
|
||||
DISPATCH_CONTROLLER_DISCONNECTED,
|
||||
DISPATCH_CONTROLLER_RECONNECTED,
|
||||
DISPATCH_CONTROLLER_UPDATE,
|
||||
DISPATCH_ZONE_UPDATE,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DiscoveryService(pizone.Listener):
|
||||
"""Discovery data and interfacing with pizone library."""
|
||||
|
||||
def __init__(self, hass):
|
||||
"""Initialise discovery service."""
|
||||
super().__init__()
|
||||
self.hass = hass
|
||||
self.pi_disco = None
|
||||
|
||||
# Listener interface
|
||||
def controller_discovered(self, ctrl: pizone.Controller) -> None:
|
||||
"""Handle new controller discoverery."""
|
||||
async_dispatcher_send(self.hass, DISPATCH_CONTROLLER_DISCOVERED, ctrl)
|
||||
|
||||
def controller_disconnected(self, ctrl: pizone.Controller, ex: Exception) -> None:
|
||||
"""On disconnect from controller."""
|
||||
async_dispatcher_send(self.hass, DISPATCH_CONTROLLER_DISCONNECTED, ctrl, ex)
|
||||
|
||||
def controller_reconnected(self, ctrl: pizone.Controller) -> None:
|
||||
"""On reconnect to controller."""
|
||||
async_dispatcher_send(self.hass, DISPATCH_CONTROLLER_RECONNECTED, ctrl)
|
||||
|
||||
def controller_update(self, ctrl: pizone.Controller) -> None:
|
||||
"""System update message is recieved from the controller."""
|
||||
async_dispatcher_send(self.hass, DISPATCH_CONTROLLER_UPDATE, ctrl)
|
||||
|
||||
def zone_update(self, ctrl: pizone.Controller, zone: pizone.Zone) -> None:
|
||||
"""Zone update message is recieved from the controller."""
|
||||
async_dispatcher_send(self.hass, DISPATCH_ZONE_UPDATE, ctrl, zone)
|
||||
|
||||
|
||||
async def async_start_discovery_service(hass: HomeAssistantType):
|
||||
"""Set up the pizone internal discovery."""
|
||||
disco = hass.data.get(DATA_DISCOVERY_SERVICE)
|
||||
if disco:
|
||||
# Already started
|
||||
return disco
|
||||
|
||||
# discovery local services
|
||||
disco = DiscoveryService(hass)
|
||||
hass.data[DATA_DISCOVERY_SERVICE] = disco
|
||||
|
||||
# Start the pizone discovery service, disco is the listener
|
||||
session = aiohttp_client.async_get_clientsession(hass)
|
||||
loop = hass.loop
|
||||
|
||||
disco.pi_disco = pizone.discovery(disco, loop=loop, session=session)
|
||||
await disco.pi_disco.start_discovery()
|
||||
|
||||
async def shutdown_event(event):
|
||||
await async_stop_discovery_service(hass)
|
||||
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown_event)
|
||||
|
||||
return disco
|
||||
|
||||
|
||||
async def async_stop_discovery_service(hass: HomeAssistantType):
|
||||
"""Stop the discovery service."""
|
||||
disco = hass.data.get(DATA_DISCOVERY_SERVICE)
|
||||
if not disco:
|
||||
return
|
||||
|
||||
await disco.pi_disco.close()
|
||||
del hass.data[DATA_DISCOVERY_SERVICE]
|
||||
Reference in new issue
Block a user