1
0
mirror of https://github.com/natekspencer/hacs-oasis_mini.git synced 2025-12-06 18:44:14 -05:00

Switch to using mqtt

This commit is contained in:
Nathan Spencer
2025-11-22 04:40:58 +00:00
parent 171a608314
commit 886d7598f3
34 changed files with 2036 additions and 1057 deletions

View File

@@ -1,4 +1,4 @@
"""Support for Oasis Mini."""
"""Support for Oasis devices."""
from __future__ import annotations
@@ -6,17 +6,16 @@ import logging
from typing import Any
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.const import CONF_EMAIL, Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
import homeassistant.helpers.device_registry as dr
from homeassistant.exceptions import ConfigEntryAuthFailed
import homeassistant.helpers.entity_registry as er
from .const import DOMAIN
from .coordinator import OasisMiniCoordinator
from .coordinator import OasisDeviceCoordinator
from .helpers import create_client
from .pyoasiscontrol import OasisMqttClient, UnauthenticatedError
type OasisMiniConfigEntry = ConfigEntry[OasisMiniCoordinator]
type OasisDeviceConfigEntry = ConfigEntry[OasisDeviceCoordinator]
_LOGGER = logging.getLogger(__name__)
@@ -29,90 +28,116 @@ PLATFORMS = [
Platform.NUMBER,
Platform.SELECT,
Platform.SENSOR,
# Platform.SWITCH,
Platform.UPDATE,
]
async def async_setup_entry(hass: HomeAssistant, entry: OasisMiniConfigEntry) -> bool:
"""Set up Oasis Mini from a config entry."""
client = create_client(entry.data | entry.options)
coordinator = OasisMiniCoordinator(hass, client)
async def async_setup_entry(hass: HomeAssistant, entry: OasisDeviceConfigEntry) -> bool:
"""Set up Oasis devices from a config entry."""
cloud_client = create_client(hass, entry.data)
try:
user = await cloud_client.async_get_user()
except UnauthenticatedError as err:
raise ConfigEntryAuthFailed(err) from err
mqtt_client = OasisMqttClient()
mqtt_client.start()
coordinator = OasisDeviceCoordinator(hass, cloud_client, mqtt_client)
try:
await coordinator.async_config_entry_first_refresh()
except Exception as ex:
_LOGGER.exception(ex)
if not entry.unique_id:
if not (serial_number := coordinator.device.serial_number):
dev_reg = dr.async_get(hass)
devices = dr.async_entries_for_config_entry(dev_reg, entry.entry_id)
serial_number = next(
(
identifier[1]
for identifier in devices[0].identifiers
if identifier[0] == DOMAIN
),
None,
)
hass.config_entries.async_update_entry(entry, unique_id=serial_number)
if entry.unique_id != (user_id := str(user["id"])):
hass.config_entries.async_update_entry(entry, unique_id=user_id)
if not coordinator.data:
await client.session.close()
raise ConfigEntryNotReady
if entry.unique_id != coordinator.device.serial_number:
await client.session.close()
raise ConfigEntryError("Serial number mismatch")
_LOGGER.warning("No devices associated with account")
entry.runtime_data = coordinator
def _on_oasis_update() -> None:
coordinator.async_update_listeners()
for device in coordinator.data:
device.add_update_listener(_on_oasis_update)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(update_listener))
return True
async def async_unload_entry(hass: HomeAssistant, entry: OasisMiniConfigEntry) -> bool:
async def async_unload_entry(
hass: HomeAssistant, entry: OasisDeviceConfigEntry
) -> bool:
"""Unload a config entry."""
await entry.runtime_data.device.session.close()
mqtt_client = entry.runtime_data.mqtt_client
await mqtt_client.async_close()
cloud_client = entry.runtime_data.cloud_client
await cloud_client.async_close()
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def async_remove_entry(hass: HomeAssistant, entry: OasisMiniConfigEntry) -> None:
async def async_remove_entry(
hass: HomeAssistant, entry: OasisDeviceConfigEntry
) -> None:
"""Handle removal of an entry."""
if entry.options:
client = create_client(entry.data | entry.options)
await client.async_cloud_logout()
await client.session.close()
cloud_client = create_client(hass, entry.data)
try:
await cloud_client.async_logout()
except Exception as ex:
_LOGGER.exception(ex)
await cloud_client.async_close()
async def update_listener(hass: HomeAssistant, entry: OasisMiniConfigEntry) -> None:
"""Handle options update."""
await hass.config_entries.async_reload(entry.entry_id)
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry):
async def async_migrate_entry(hass: HomeAssistant, entry: OasisDeviceConfigEntry):
"""Migrate old entry."""
_LOGGER.debug(
"Migrating configuration from version %s.%s", entry.version, entry.minor_version
)
if entry.version == 1 and entry.minor_version == 1:
# Need to update previous playlist select entity to queue
@callback
def migrate_unique_id(entity_entry: er.RegistryEntry) -> dict[str, Any] | None:
"""Migrate the playlist unique ID to queue."""
if entity_entry.domain == "select" and entity_entry.unique_id.endswith(
"-playlist"
):
unique_id = entity_entry.unique_id.replace("-playlist", "-queue")
return {"new_unique_id": unique_id}
return None
if entry.version > 1:
# This means the user has downgraded from a future version
return False
await er.async_migrate_entries(hass, entry.entry_id, migrate_unique_id)
hass.config_entries.async_update_entry(entry, minor_version=2, version=1)
if entry.version == 1:
new_data = {**entry.data}
new_options = {**entry.options}
if entry.minor_version < 2:
# Need to update previous playlist select entity to queue
@callback
def migrate_unique_id(
entity_entry: er.RegistryEntry,
) -> dict[str, Any] | None:
"""Migrate the playlist unique ID to queue."""
if entity_entry.domain == "select" and entity_entry.unique_id.endswith(
"-playlist"
):
unique_id = entity_entry.unique_id.replace("-playlist", "-queue")
return {"new_unique_id": unique_id}
return None
await er.async_migrate_entries(hass, entry.entry_id, migrate_unique_id)
if entry.minor_version < 3:
# Auth is now required, host is dropped
new_data = {**entry.options}
new_options = {}
hass.config_entries.async_update_entry(
entry,
data=new_data,
options=new_options,
minor_version=3,
title=new_data.get(CONF_EMAIL, "Oasis Control"),
unique_id=None,
version=1,
)
_LOGGER.debug(
"Migration to configuration version %s.%s successful",