55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
"""Jackery Home Assistant Integration."""
|
|
import logging
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.components import mqtt
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
DOMAIN = "jackery"
|
|
PLATFORMS = [Platform.SENSOR, Platform.SWITCH, Platform.NUMBER]
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Set up one Jackery device from a config entry."""
|
|
if not await mqtt.async_wait_for_mqtt_client(hass):
|
|
_LOGGER.error("MQTT integration is not available")
|
|
return False
|
|
|
|
from .sensor import JackeryCoordinator
|
|
|
|
config = entry.data
|
|
coordinator = JackeryCoordinator(
|
|
hass=hass,
|
|
entry_id=entry.entry_id,
|
|
device_sn=config["device_sn"],
|
|
token=config.get("token", ""),
|
|
topic_prefix=config.get("topic_prefix", "hb"),
|
|
)
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
hass.data[DOMAIN][entry.entry_id] = coordinator
|
|
|
|
# Start MQTT subscriptions now; the poll loop waits 2 s so platform
|
|
# async_setup_entry callbacks are registered before the first poll fires.
|
|
await coordinator.async_start()
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Unload a config entry."""
|
|
coordinator = hass.data[DOMAIN].get(entry.entry_id)
|
|
if coordinator:
|
|
await coordinator.async_stop()
|
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
if unload_ok:
|
|
hass.data[DOMAIN].pop(entry.entry_id, None)
|
|
|
|
return unload_ok
|