mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Use runtime_data in coolmaster (#136405)
* Use runtime_data in coolmaster * Adjust test
This commit is contained in:
parent
6a1279611d
commit
8b08cb9bc1
@ -2,18 +2,17 @@
|
|||||||
|
|
||||||
from pycoolmasternet_async import CoolMasterNet
|
from pycoolmasternet_async import CoolMasterNet
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT, Platform
|
from homeassistant.const import CONF_HOST, CONF_PORT, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
|
|
||||||
from .const import CONF_SWING_SUPPORT, DATA_COORDINATOR, DATA_INFO, DOMAIN
|
from .const import CONF_SWING_SUPPORT
|
||||||
from .coordinator import CoolmasterDataUpdateCoordinator
|
from .coordinator import CoolmasterConfigEntry, CoolmasterDataUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.CLIMATE, Platform.SENSOR]
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.CLIMATE, Platform.SENSOR]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: CoolmasterConfigEntry) -> bool:
|
||||||
"""Set up Coolmaster from a config entry."""
|
"""Set up Coolmaster from a config entry."""
|
||||||
host = entry.data[CONF_HOST]
|
host = entry.data[CONF_HOST]
|
||||||
port = entry.data[CONF_PORT]
|
port = entry.data[CONF_PORT]
|
||||||
@ -38,21 +37,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
raise ConfigEntryNotReady
|
raise ConfigEntryNotReady
|
||||||
except OSError as error:
|
except OSError as error:
|
||||||
raise ConfigEntryNotReady from error
|
raise ConfigEntryNotReady from error
|
||||||
coordinator = CoolmasterDataUpdateCoordinator(hass, coolmaster)
|
coordinator = CoolmasterDataUpdateCoordinator(hass, entry, coolmaster, info)
|
||||||
hass.data.setdefault(DOMAIN, {})
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
hass.data[DOMAIN][entry.entry_id] = {
|
entry.runtime_data = coordinator
|
||||||
DATA_INFO: info,
|
|
||||||
DATA_COORDINATOR: coordinator,
|
|
||||||
}
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: CoolmasterConfigEntry) -> bool:
|
||||||
"""Unload a Coolmaster config entry."""
|
"""Unload a Coolmaster config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
return unload_ok
|
|
||||||
|
@ -7,26 +7,23 @@ from homeassistant.components.binary_sensor import (
|
|||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
BinarySensorEntityDescription,
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DATA_COORDINATOR, DATA_INFO, DOMAIN
|
from .coordinator import CoolmasterConfigEntry
|
||||||
from .entity import CoolmasterEntity
|
from .entity import CoolmasterEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: CoolmasterConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the CoolMasterNet binary_sensor platform."""
|
"""Set up the CoolMasterNet binary_sensor platform."""
|
||||||
info = hass.data[DOMAIN][config_entry.entry_id][DATA_INFO]
|
coordinator = config_entry.runtime_data
|
||||||
coordinator = hass.data[DOMAIN][config_entry.entry_id][DATA_COORDINATOR]
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
CoolmasterCleanFilter(coordinator, unit_id, info)
|
CoolmasterCleanFilter(coordinator, unit_id) for unit_id in coordinator.data
|
||||||
for unit_id in coordinator.data
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,26 +3,23 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DATA_COORDINATOR, DATA_INFO, DOMAIN
|
from .coordinator import CoolmasterConfigEntry
|
||||||
from .entity import CoolmasterEntity
|
from .entity import CoolmasterEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: CoolmasterConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the CoolMasterNet button platform."""
|
"""Set up the CoolMasterNet button platform."""
|
||||||
info = hass.data[DOMAIN][config_entry.entry_id][DATA_INFO]
|
coordinator = config_entry.runtime_data
|
||||||
coordinator = hass.data[DOMAIN][config_entry.entry_id][DATA_COORDINATOR]
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
CoolmasterResetFilter(coordinator, unit_id, info)
|
CoolmasterResetFilter(coordinator, unit_id) for unit_id in coordinator.data
|
||||||
for unit_id in coordinator.data
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,13 +12,13 @@ from homeassistant.components.climate import (
|
|||||||
ClimateEntityFeature,
|
ClimateEntityFeature,
|
||||||
HVACMode,
|
HVACMode,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
|
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import CONF_SUPPORTED_MODES, DATA_COORDINATOR, DATA_INFO, DOMAIN
|
from .const import CONF_SUPPORTED_MODES
|
||||||
|
from .coordinator import CoolmasterConfigEntry, CoolmasterDataUpdateCoordinator
|
||||||
from .entity import CoolmasterEntity
|
from .entity import CoolmasterEntity
|
||||||
|
|
||||||
CM_TO_HA_STATE = {
|
CM_TO_HA_STATE = {
|
||||||
@ -38,15 +38,16 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: CoolmasterConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the CoolMasterNet climate platform."""
|
"""Set up the CoolMasterNet climate platform."""
|
||||||
info = hass.data[DOMAIN][config_entry.entry_id][DATA_INFO]
|
coordinator = config_entry.runtime_data
|
||||||
coordinator = hass.data[DOMAIN][config_entry.entry_id][DATA_COORDINATOR]
|
supported_modes: list[str] = config_entry.data[CONF_SUPPORTED_MODES]
|
||||||
supported_modes = config_entry.data.get(CONF_SUPPORTED_MODES)
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
CoolmasterClimate(coordinator, unit_id, info, supported_modes)
|
CoolmasterClimate(
|
||||||
|
coordinator, unit_id, [HVACMode(mode) for mode in supported_modes]
|
||||||
|
)
|
||||||
for unit_id in coordinator.data
|
for unit_id in coordinator.data
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -56,9 +57,14 @@ class CoolmasterClimate(CoolmasterEntity, ClimateEntity):
|
|||||||
|
|
||||||
_attr_name = None
|
_attr_name = None
|
||||||
|
|
||||||
def __init__(self, coordinator, unit_id, info, supported_modes):
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: CoolmasterDataUpdateCoordinator,
|
||||||
|
unit_id: str,
|
||||||
|
supported_modes: list[HVACMode],
|
||||||
|
) -> None:
|
||||||
"""Initialize the climate device."""
|
"""Initialize the climate device."""
|
||||||
super().__init__(coordinator, unit_id, info)
|
super().__init__(coordinator, unit_id)
|
||||||
self._attr_hvac_modes = supported_modes
|
self._attr_hvac_modes = supported_modes
|
||||||
self._attr_unique_id = unit_id
|
self._attr_unique_id = unit_id
|
||||||
|
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
"""Constants for the Coolmaster integration."""
|
"""Constants for the Coolmaster integration."""
|
||||||
|
|
||||||
DATA_INFO = "info"
|
|
||||||
DATA_COORDINATOR = "coordinator"
|
|
||||||
|
|
||||||
DOMAIN = "coolmaster"
|
DOMAIN = "coolmaster"
|
||||||
|
|
||||||
DEFAULT_PORT = 10102
|
DEFAULT_PORT = 10102
|
||||||
|
@ -1,8 +1,15 @@
|
|||||||
"""DataUpdateCoordinator for coolmaster integration."""
|
"""DataUpdateCoordinator for coolmaster integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from pycoolmasternet_async import CoolMasterNet
|
||||||
|
from pycoolmasternet_async.coolmasternet import CoolMasterNetUnit
|
||||||
|
|
||||||
from homeassistant.components.climate import SCAN_INTERVAL
|
from homeassistant.components.climate import SCAN_INTERVAL
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
@ -10,21 +17,34 @@ from .const import DOMAIN
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class CoolmasterDataUpdateCoordinator(DataUpdateCoordinator):
|
type CoolmasterConfigEntry = ConfigEntry[CoolmasterDataUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
|
class CoolmasterDataUpdateCoordinator(
|
||||||
|
DataUpdateCoordinator[dict[str, CoolMasterNetUnit]]
|
||||||
|
):
|
||||||
"""Class to manage fetching Coolmaster data."""
|
"""Class to manage fetching Coolmaster data."""
|
||||||
|
|
||||||
def __init__(self, hass, coolmaster):
|
def __init__(
|
||||||
|
self,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entry: CoolmasterConfigEntry,
|
||||||
|
coolmaster: CoolMasterNet,
|
||||||
|
info: dict[str, str],
|
||||||
|
) -> None:
|
||||||
"""Initialize global Coolmaster data updater."""
|
"""Initialize global Coolmaster data updater."""
|
||||||
self._coolmaster = coolmaster
|
self._coolmaster = coolmaster
|
||||||
|
self.info = info
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
|
config_entry=entry,
|
||||||
name=DOMAIN,
|
name=DOMAIN,
|
||||||
update_interval=SCAN_INTERVAL,
|
update_interval=SCAN_INTERVAL,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _async_update_data(self):
|
async def _async_update_data(self) -> dict[str, CoolMasterNetUnit]:
|
||||||
"""Fetch data from Coolmaster."""
|
"""Fetch data from Coolmaster."""
|
||||||
try:
|
try:
|
||||||
return await self._coolmaster.status()
|
return await self._coolmaster.status()
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
"""Base entity for Coolmaster integration."""
|
"""Base entity for Coolmaster integration."""
|
||||||
|
|
||||||
from pycoolmasternet_async.coolmasternet import CoolMasterNetUnit
|
|
||||||
|
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
@ -19,18 +17,17 @@ class CoolmasterEntity(CoordinatorEntity[CoolmasterDataUpdateCoordinator]):
|
|||||||
self,
|
self,
|
||||||
coordinator: CoolmasterDataUpdateCoordinator,
|
coordinator: CoolmasterDataUpdateCoordinator,
|
||||||
unit_id: str,
|
unit_id: str,
|
||||||
info: dict[str, str],
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initiate CoolmasterEntity."""
|
"""Initiate CoolmasterEntity."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._unit_id: str = unit_id
|
self._unit_id: str = unit_id
|
||||||
self._unit: CoolMasterNetUnit = coordinator.data[self._unit_id]
|
self._unit = coordinator.data[self._unit_id]
|
||||||
self._attr_device_info: DeviceInfo = DeviceInfo(
|
self._attr_device_info: DeviceInfo = DeviceInfo(
|
||||||
identifiers={(DOMAIN, unit_id)},
|
identifiers={(DOMAIN, unit_id)},
|
||||||
manufacturer="CoolAutomation",
|
manufacturer="CoolAutomation",
|
||||||
model="CoolMasterNet",
|
model="CoolMasterNet",
|
||||||
name=unit_id,
|
name=unit_id,
|
||||||
sw_version=info["version"],
|
sw_version=coordinator.info["version"],
|
||||||
)
|
)
|
||||||
if hasattr(self, "entity_description"):
|
if hasattr(self, "entity_description"):
|
||||||
self._attr_unique_id: str = f"{unit_id}-{self.entity_description.key}"
|
self._attr_unique_id: str = f"{unit_id}-{self.entity_description.key}"
|
||||||
|
@ -3,26 +3,23 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DATA_COORDINATOR, DATA_INFO, DOMAIN
|
from .coordinator import CoolmasterConfigEntry
|
||||||
from .entity import CoolmasterEntity
|
from .entity import CoolmasterEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: CoolmasterConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the CoolMasterNet sensor platform."""
|
"""Set up the CoolMasterNet sensor platform."""
|
||||||
info = hass.data[DOMAIN][config_entry.entry_id][DATA_INFO]
|
coordinator = config_entry.runtime_data
|
||||||
coordinator = hass.data[DOMAIN][config_entry.entry_id][DATA_COORDINATOR]
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
CoolmasterCleanFilter(coordinator, unit_id, info)
|
CoolmasterCleanFilter(coordinator, unit_id) for unit_id in coordinator.data
|
||||||
for unit_id in coordinator.data
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
"""The test for the Coolmaster integration."""
|
"""The test for the Coolmaster integration."""
|
||||||
|
|
||||||
from homeassistant.components.coolmaster.const import DOMAIN
|
|
||||||
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
|
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
@ -20,8 +19,6 @@ async def test_unload_entry(
|
|||||||
load_int: ConfigEntry,
|
load_int: ConfigEntry,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test Coolmaster unloading an entry."""
|
"""Test Coolmaster unloading an entry."""
|
||||||
assert load_int.entry_id in hass.data.get(DOMAIN)
|
|
||||||
await hass.config_entries.async_unload(load_int.entry_id)
|
await hass.config_entries.async_unload(load_int.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert load_int.state is ConfigEntryState.NOT_LOADED
|
assert load_int.state is ConfigEntryState.NOT_LOADED
|
||||||
assert not hass.data.get(DOMAIN)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user