mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 07:07:28 +00:00
Migrate apcupsd to use runtime_data (#125539)
This commit is contained in:
parent
7209b3c7d3
commit
4d804649fc
@ -2,22 +2,22 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
|
||||||
from typing import Final
|
from typing import Final
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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 .const import DOMAIN
|
|
||||||
from .coordinator import APCUPSdCoordinator
|
from .coordinator import APCUPSdCoordinator
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
type APCUPSdConfigEntry = ConfigEntry[APCUPSdCoordinator]
|
||||||
|
|
||||||
PLATFORMS: Final = (Platform.BINARY_SENSOR, Platform.SENSOR)
|
PLATFORMS: Final = (Platform.BINARY_SENSOR, Platform.SENSOR)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, config_entry: APCUPSdConfigEntry
|
||||||
|
) -> bool:
|
||||||
"""Use config values to set up a function enabling status retrieval."""
|
"""Use config values to set up a function enabling status retrieval."""
|
||||||
host, port = config_entry.data[CONF_HOST], config_entry.data[CONF_PORT]
|
host, port = config_entry.data[CONF_HOST], config_entry.data[CONF_PORT]
|
||||||
coordinator = APCUPSdCoordinator(hass, host, port)
|
coordinator = APCUPSdCoordinator(hass, host, port)
|
||||||
@ -25,17 +25,13 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
# Store the coordinator for later uses.
|
# Store the coordinator for later uses.
|
||||||
hass.data.setdefault(DOMAIN, {})
|
config_entry.runtime_data = coordinator
|
||||||
hass.data[DOMAIN][config_entry.entry_id] = coordinator
|
|
||||||
|
|
||||||
# Forward the config entries to the supported platforms.
|
# Forward the config entries to the supported platforms.
|
||||||
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: APCUPSdConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a 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 and DOMAIN in hass.data:
|
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
return unload_ok
|
|
||||||
|
@ -2,24 +2,21 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
|
||||||
from typing import Final
|
from typing import Final
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
BinarySensorEntityDescription,
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
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 homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import APCUPSdConfigEntry
|
||||||
from .coordinator import APCUPSdCoordinator
|
from .coordinator import APCUPSdCoordinator
|
||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
_DESCRIPTION = BinarySensorEntityDescription(
|
_DESCRIPTION = BinarySensorEntityDescription(
|
||||||
key="statflag",
|
key="statflag",
|
||||||
translation_key="online_status",
|
translation_key="online_status",
|
||||||
@ -30,11 +27,11 @@ _VALUE_ONLINE_MASK: Final = 0b1000
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: APCUPSdConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up an APCUPSd Online Status binary sensor."""
|
"""Set up an APCUPSd Online Status binary sensor."""
|
||||||
coordinator: APCUPSdCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
coordinator = config_entry.runtime_data
|
||||||
|
|
||||||
# Do not create the binary sensor if APCUPSd does not provide STATFLAG field for us
|
# Do not create the binary sensor if APCUPSd does not provide STATFLAG field for us
|
||||||
# to determine the online status.
|
# to determine the online status.
|
||||||
|
@ -5,19 +5,17 @@ from __future__ import annotations
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.diagnostics import async_redact_data
|
from homeassistant.components.diagnostics import async_redact_data
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import APCUPSdConfigEntry
|
||||||
from .coordinator import APCUPSdCoordinator, APCUPSdData
|
|
||||||
|
|
||||||
TO_REDACT = {"SERIALNO", "HOSTNAME"}
|
TO_REDACT = {"SERIALNO", "HOSTNAME"}
|
||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, entry: ConfigEntry
|
hass: HomeAssistant, entry: APCUPSdConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
coordinator: APCUPSdCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator = entry.runtime_data
|
||||||
data: APCUPSdData = coordinator.data
|
data = coordinator.data
|
||||||
return async_redact_data(data, TO_REDACT)
|
return async_redact_data(data, TO_REDACT)
|
||||||
|
@ -10,7 +10,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
UnitOfApparentPower,
|
UnitOfApparentPower,
|
||||||
@ -25,7 +24,8 @@ from homeassistant.core import HomeAssistant, callback
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN, LAST_S_TEST
|
from . import APCUPSdConfigEntry
|
||||||
|
from .const import LAST_S_TEST
|
||||||
from .coordinator import APCUPSdCoordinator
|
from .coordinator import APCUPSdCoordinator
|
||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
@ -406,11 +406,11 @@ INFERRED_UNITS = {
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: APCUPSdConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the APCUPSd sensors from config entries."""
|
"""Set up the APCUPSd sensors from config entries."""
|
||||||
coordinator: APCUPSdCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
coordinator = config_entry.runtime_data
|
||||||
|
|
||||||
# The resource keys in the data dict collected in the coordinator is in upper-case
|
# The resource keys in the data dict collected in the coordinator is in upper-case
|
||||||
# by default, but we use lower cases throughout this integration.
|
# by default, but we use lower cases throughout this integration.
|
||||||
|
@ -4,7 +4,7 @@ from collections import OrderedDict
|
|||||||
from typing import Final
|
from typing import Final
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from homeassistant.components.apcupsd import DOMAIN
|
from homeassistant.components.apcupsd.const import DOMAIN
|
||||||
from homeassistant.config_entries import SOURCE_USER
|
from homeassistant.config_entries import SOURCE_USER
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
@ -5,7 +5,7 @@ from unittest.mock import patch
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.apcupsd import DOMAIN
|
from homeassistant.components.apcupsd.const import DOMAIN
|
||||||
from homeassistant.config_entries import SOURCE_USER
|
from homeassistant.config_entries import SOURCE_USER
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SOURCE
|
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SOURCE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
Loading…
x
Reference in New Issue
Block a user