Make CoolMasterNet integration async (#38643)
* make CoolMasteNet integration async * Apply suggestions from code review Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Minor post-review tweaks * Apply suggestions from code review * Update homeassistant/components/coolmaster/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
This commit is contained in:
1 parent
597d1e2799
commit
96d48c309f
8 files changed
+144
-80
No files matched your search
@@ -1,13 +1,41 @@
|
||||
"""The Coolmaster integration."""
|
||||
import logging
|
||||
|
||||
from pycoolmasternet_async import CoolMasterNet
|
||||
|
||||
from homeassistant.components.climate import SCAN_INTERVAL
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import DATA_COORDINATOR, DATA_INFO, DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Set up Coolmaster components."""
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass, entry):
|
||||
"""Set up Coolmaster from a config entry."""
|
||||
host = entry.data[CONF_HOST]
|
||||
port = entry.data[CONF_PORT]
|
||||
coolmaster = CoolMasterNet(host, port)
|
||||
try:
|
||||
info = await coolmaster.info()
|
||||
if not info:
|
||||
raise ConfigEntryNotReady
|
||||
except (OSError, ConnectionRefusedError, TimeoutError) as error:
|
||||
raise ConfigEntryNotReady() from error
|
||||
coordinator = CoolmasterDataUpdateCoordinator(hass, coolmaster)
|
||||
await coordinator.async_refresh()
|
||||
hass.data[DOMAIN][entry.entry_id] = {
|
||||
DATA_INFO: info,
|
||||
DATA_COORDINATOR: coordinator,
|
||||
}
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(entry, "climate")
|
||||
)
|
||||
@@ -17,4 +45,28 @@ async def async_setup_entry(hass, entry):
|
||||
|
||||
async def async_unload_entry(hass, entry):
|
||||
"""Unload a Coolmaster config entry."""
|
||||
return await hass.config_entries.async_forward_entry_unload(entry, "climate")
|
||||
unload_ok = await hass.config_entries.async_forward_entry_unload(entry, "climate")
|
||||
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
|
||||
|
||||
class CoolmasterDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
"""Class to manage fetching Coolmaster data."""
|
||||
|
||||
def __init__(self, hass, coolmaster):
|
||||
"""Initialize global Coolmaster data updater."""
|
||||
self._coolmaster = coolmaster
|
||||
|
||||
super().__init__(
|
||||
hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL,
|
||||
)
|
||||
|
||||
async def _async_update_data(self):
|
||||
"""Fetch data from Coolmaster."""
|
||||
try:
|
||||
return await self._coolmaster.status()
|
||||
except (OSError, ConnectionRefusedError, TimeoutError) as error:
|
||||
raise UpdateFailed from error
|
||||
Reference in new issue
Block a user