Files
ha-core/homeassistant/components/madvr/__init__.py
ilan 12228d8a00 Add madvr envy integration (#120382)
* feat: Add madvr envy

* fix: await and pass entry directly

* fix: add attributes and unique id for sensors

* fix: reflect power state well, improve state detection

* fix: don't connect on init, add options, add reload on change, keep on during test

* fix: cancel tasks on unload

* fix: test connection via library

* fix: wait for boot time

* docs: add readme and license

* fix: broken pipe in lib

* fix: detect out of band power off

* fix: improve extra attributes

* fix: fix unloading, add config flow test, limit to one platform

* fix: use conf, refresh coordinator, other comments

* fix: remove event data

* fix: fix tests passing, remove wake on lan

* fix: dont allow to proceed unless connection works

* chore: update dep

* fix: update config flow, add constants

* fix: write state, use runtime data instead

* fix: remove await

* fix: move unloading and stuff to coordinator/init

* fix: pass in config entry with correct type

* fix: move queue and tasks to library

* fix: config flow error flow, tests, name, and update lib

* fix: update lib, leave connection open on setup

* fix: update lib

* fix: address comments, remove wol from lib

* fix: remove unneeded options

* fix: remove fields

* fix: simplify code, address comments

* fix: move error to lib

* fix: fix test

* fix: stronger types

* fix: update lib

* fix: missing text from options flow

* chore: remove options flow

* chore: remove import

* chore: update comments

* fix: get mac from device, persist

* fix: add mac stuff to test

* fix: startup import errors

* chore: stale comment

* fix: get mac from persisted config

* chore: update lib

* fix: persist mac in a better way

* feat: use mac as unique ID for entry

* fix: use unique ID from mac, add proper device

* fix: will not be set in init potentially

* fix: access mac

* fix: optimize, move error to lib

* feat: add coordinator test, use conf

* fix: use one mock, add init test

* fix: not async

* feat: add remote test

* fix: types

* fix: patch client, expand remote tests

* fix: use snapshot test

* fix: update branding

* fix: add description, fix type check

* fix: update tests

* fix: test

* fix: update test

* fix: camelcase

* Fix

* feat: strict typing

* fix: strict typing in lib

* fix: type will never be None

* fix: reference to mac, all tests passing

---------

Co-authored-by: Joostlek <joostlek@outlook.com>
2024-07-07 20:41:53 +02:00

71 lines
2.1 KiB
Python

"""The madvr-envy integration."""
from __future__ import annotations
import logging
from madvr.madvr import Madvr
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP, Platform
from homeassistant.core import Event, HomeAssistant, callback
from .coordinator import MadVRCoordinator
PLATFORMS: list[Platform] = [Platform.REMOTE]
type MadVRConfigEntry = ConfigEntry[MadVRCoordinator]
_LOGGER = logging.getLogger(__name__)
async def async_handle_unload(coordinator: MadVRCoordinator) -> None:
"""Handle unload."""
_LOGGER.debug("Integration unloading")
coordinator.client.stop()
await coordinator.client.async_cancel_tasks()
_LOGGER.debug("Integration closing connection")
await coordinator.client.close_connection()
_LOGGER.debug("Unloaded")
async def async_setup_entry(hass: HomeAssistant, entry: MadVRConfigEntry) -> bool:
"""Set up the integration from a config entry."""
assert entry.unique_id
madVRClient = Madvr(
host=entry.data[CONF_HOST],
logger=_LOGGER,
port=entry.data[CONF_PORT],
mac=entry.unique_id,
connect_timeout=10,
loop=hass.loop,
)
coordinator = MadVRCoordinator(hass, madVRClient)
entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
@callback
async def handle_unload(event: Event) -> None:
"""Handle unload."""
await async_handle_unload(coordinator=coordinator)
# listen for core stop event
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, handle_unload)
# handle loading operations
await coordinator.handle_coordinator_load()
return True
async def async_unload_entry(hass: HomeAssistant, entry: MadVRConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
coordinator: MadVRCoordinator = entry.runtime_data
await async_handle_unload(coordinator=coordinator)
return unload_ok