1
mirror of https://github.com/home-assistant/core synced 2024-09-09 12:51:22 +02:00

Remove resources selection from Nut config flow (#59450)

* Remove resources selection from Nut config flow

* Code clean-up

* Requested changes

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
ollo69 2021-11-10 13:49:05 +01:00 committed by GitHub
parent c03fdd5da6
commit 6cba03aa4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 149 additions and 210 deletions

View File

@ -37,13 +37,11 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Network UPS Tools (NUT) from a config entry."""
# strip out the stale options CONF_RESOURCES
if CONF_RESOURCES in entry.options:
new_data = {**entry.data, CONF_RESOURCES: entry.options[CONF_RESOURCES]}
new_options = {k: v for k, v in entry.options.items() if k != CONF_RESOURCES}
hass.config_entries.async_update_entry(
entry, data=new_data, options=new_options
)
# strip out the stale setting CONF_RESOURCES from data & options
if CONF_RESOURCES in entry.data:
new_data = {k: v for k, v in entry.data.items() if k != CONF_RESOURCES}
new_opts = {k: v for k, v in entry.options.items() if k != CONF_RESOURCES}
hass.config_entries.async_update_entry(entry, data=new_data, options=new_opts)
config = entry.data
host = config[CONF_HOST]
@ -79,7 +77,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_LOGGER.debug("NUT Sensors Available: %s", status)
undo_listener = entry.add_update_listener(_async_update_listener)
unique_id = _unique_id_from_status(status)
if unique_id is None:
unique_id = entry.entry_id
@ -91,7 +88,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
PYNUT_UNIQUE_ID: unique_id,
UNDO_UPDATE_LISTENER: undo_listener,
}
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
return True

View File

@ -10,23 +10,13 @@ from homeassistant.const import (
CONF_HOST,
CONF_PASSWORD,
CONF_PORT,
CONF_RESOURCES,
CONF_SCAN_INTERVAL,
CONF_USERNAME,
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from . import PyNUTData
from .const import (
DEFAULT_HOST,
DEFAULT_PORT,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
KEY_STATUS,
KEY_STATUS_DISPLAY,
SENSOR_TYPES,
)
from .const import DEFAULT_HOST, DEFAULT_PORT, DEFAULT_SCAN_INTERVAL, DOMAIN
_LOGGER = logging.getLogger(__name__)
@ -48,27 +38,6 @@ def _base_schema(discovery_info):
return vol.Schema(base_schema)
def _resource_schema_base(available_resources, selected_resources):
"""Resource selection schema."""
known_available_resources = {
sensor_id: sensor_desc.name
for sensor_id, sensor_desc in SENSOR_TYPES.items()
if sensor_id in available_resources
}
if KEY_STATUS in known_available_resources:
known_available_resources[KEY_STATUS_DISPLAY] = SENSOR_TYPES[
KEY_STATUS_DISPLAY
].name
return {
vol.Required(CONF_RESOURCES, default=selected_resources): cv.multi_select(
known_available_resources
)
}
def _ups_schema(ups_list):
"""UPS selection schema."""
return vol.Schema({vol.Required(CONF_ALIAS): vol.In(ups_list)})
@ -112,7 +81,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
def __init__(self):
"""Initialize the nut config flow."""
self.nut_config = {}
self.available_resources = {}
self.discovery_info = {}
self.ups_list = None
self.title = None
@ -148,8 +116,8 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
if self._host_port_alias_already_configured(self.nut_config):
return self.async_abort(reason="already_configured")
self.available_resources.update(info["available_resources"])
return await self.async_step_resources()
title = _format_host_port_alias(self.nut_config)
return self.async_create_entry(title=title, data=self.nut_config)
return self.async_show_form(
step_id="user", data_schema=_base_schema(self.discovery_info), errors=errors
@ -163,10 +131,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self.nut_config.update(user_input)
if self._host_port_alias_already_configured(self.nut_config):
return self.async_abort(reason="already_configured")
info, errors = await self._async_validate_or_error(self.nut_config)
_, errors = await self._async_validate_or_error(self.nut_config)
if not errors:
self.available_resources.update(info["available_resources"])
return await self.async_step_resources()
title = _format_host_port_alias(self.nut_config)
return self.async_create_entry(title=title, data=self.nut_config)
return self.async_show_form(
step_id="ups",
@ -174,20 +142,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
errors=errors,
)
async def async_step_resources(self, user_input=None):
"""Handle the picking the resources."""
if user_input is None:
return self.async_show_form(
step_id="resources",
data_schema=vol.Schema(
_resource_schema_base(self.available_resources, [])
),
)
self.nut_config.update(user_input)
title = _format_host_port_alias(self.nut_config)
return self.async_create_entry(title=title, data=self.nut_config)
def _host_port_alias_already_configured(self, user_input):
"""See if we already have a nut entry matching user input configured."""
existing_host_port_aliases = {

View File

@ -68,6 +68,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.load": SensorEntityDescription(
key="ups.load",
@ -82,12 +83,14 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=PERCENTAGE,
icon="mdi:gauge",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.id": SensorEntityDescription(
key="ups.id",
name="System identifier",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.delay.start": SensorEntityDescription(
key="ups.delay.start",
@ -95,6 +98,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=TIME_SECONDS,
icon="mdi:timer-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.delay.reboot": SensorEntityDescription(
key="ups.delay.reboot",
@ -102,6 +106,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=TIME_SECONDS,
icon="mdi:timer-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.delay.shutdown": SensorEntityDescription(
key="ups.delay.shutdown",
@ -109,6 +114,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=TIME_SECONDS,
icon="mdi:timer-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.timer.start": SensorEntityDescription(
key="ups.timer.start",
@ -116,6 +122,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=TIME_SECONDS,
icon="mdi:timer-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.timer.reboot": SensorEntityDescription(
key="ups.timer.reboot",
@ -123,6 +130,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=TIME_SECONDS,
icon="mdi:timer-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.timer.shutdown": SensorEntityDescription(
key="ups.timer.shutdown",
@ -130,6 +138,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=TIME_SECONDS,
icon="mdi:timer-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.test.interval": SensorEntityDescription(
key="ups.test.interval",
@ -137,30 +146,35 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=TIME_SECONDS,
icon="mdi:timer-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.test.result": SensorEntityDescription(
key="ups.test.result",
name="Self-Test Result",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.test.date": SensorEntityDescription(
key="ups.test.date",
name="Self-Test Date",
icon="mdi:calendar",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.display.language": SensorEntityDescription(
key="ups.display.language",
name="Language",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.contacts": SensorEntityDescription(
key="ups.contacts",
name="External Contacts",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.efficiency": SensorEntityDescription(
key="ups.efficiency",
@ -169,6 +183,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
icon="mdi:gauge",
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.power": SensorEntityDescription(
key="ups.power",
@ -177,6 +192,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
icon="mdi:flash",
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.power.nominal": SensorEntityDescription(
key="ups.power.nominal",
@ -184,6 +200,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=POWER_VOLT_AMPERE,
icon="mdi:flash",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.realpower": SensorEntityDescription(
key="ups.realpower",
@ -192,6 +209,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.realpower.nominal": SensorEntityDescription(
key="ups.realpower.nominal",
@ -199,48 +217,56 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=POWER_WATT,
device_class=DEVICE_CLASS_POWER,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.beeper.status": SensorEntityDescription(
key="ups.beeper.status",
name="Beeper Status",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.type": SensorEntityDescription(
key="ups.type",
name="UPS Type",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_SYSTEM,
entity_registry_enabled_default=False,
),
"ups.watchdog.status": SensorEntityDescription(
key="ups.watchdog.status",
name="Watchdog Status",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.start.auto": SensorEntityDescription(
key="ups.start.auto",
name="Start on AC",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.start.battery": SensorEntityDescription(
key="ups.start.battery",
name="Start on Battery",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.start.reboot": SensorEntityDescription(
key="ups.start.reboot",
name="Reboot on Battery",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ups.shutdown": SensorEntityDescription(
key="ups.shutdown",
name="Shutdown Ability",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.charge": SensorEntityDescription(
key="battery.charge",
@ -255,6 +281,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=PERCENTAGE,
icon="mdi:gauge",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.charge.restart": SensorEntityDescription(
key="battery.charge.restart",
@ -262,6 +289,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=PERCENTAGE,
icon="mdi:gauge",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.charge.warning": SensorEntityDescription(
key="battery.charge.warning",
@ -269,6 +297,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=PERCENTAGE,
icon="mdi:gauge",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.charger.status": SensorEntityDescription(
key="battery.charger.status",
@ -282,6 +311,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
device_class=DEVICE_CLASS_VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.voltage.nominal": SensorEntityDescription(
key="battery.voltage.nominal",
@ -289,6 +319,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
device_class=DEVICE_CLASS_VOLTAGE,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.voltage.low": SensorEntityDescription(
key="battery.voltage.low",
@ -296,6 +327,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
device_class=DEVICE_CLASS_VOLTAGE,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.voltage.high": SensorEntityDescription(
key="battery.voltage.high",
@ -303,6 +335,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
device_class=DEVICE_CLASS_VOLTAGE,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.capacity": SensorEntityDescription(
key="battery.capacity",
@ -310,6 +343,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement="Ah",
icon="mdi:flash",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.current": SensorEntityDescription(
key="battery.current",
@ -318,6 +352,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
icon="mdi:flash",
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.current.total": SensorEntityDescription(
key="battery.current.total",
@ -325,6 +360,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
icon="mdi:flash",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.temperature": SensorEntityDescription(
key="battery.temperature",
@ -333,6 +369,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.runtime": SensorEntityDescription(
key="battery.runtime",
@ -340,6 +377,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=TIME_SECONDS,
icon="mdi:timer-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.runtime.low": SensorEntityDescription(
key="battery.runtime.low",
@ -347,6 +385,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=TIME_SECONDS,
icon="mdi:timer-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.runtime.restart": SensorEntityDescription(
key="battery.runtime.restart",
@ -354,48 +393,56 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=TIME_SECONDS,
icon="mdi:timer-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.alarm.threshold": SensorEntityDescription(
key="battery.alarm.threshold",
name="Battery Alarm Threshold",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.date": SensorEntityDescription(
key="battery.date",
name="Battery Date",
icon="mdi:calendar",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.mfr.date": SensorEntityDescription(
key="battery.mfr.date",
name="Battery Manuf. Date",
icon="mdi:calendar",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.packs": SensorEntityDescription(
key="battery.packs",
name="Number of Batteries",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.packs.bad": SensorEntityDescription(
key="battery.packs.bad",
name="Number of Bad Batteries",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"battery.type": SensorEntityDescription(
key="battery.type",
name="Battery Chemistry",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"input.sensitivity": SensorEntityDescription(
key="input.sensitivity",
name="Input Power Sensitivity",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"input.transfer.low": SensorEntityDescription(
key="input.transfer.low",
@ -403,6 +450,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
device_class=DEVICE_CLASS_VOLTAGE,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"input.transfer.high": SensorEntityDescription(
key="input.transfer.high",
@ -410,12 +458,14 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
device_class=DEVICE_CLASS_VOLTAGE,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"input.transfer.reason": SensorEntityDescription(
key="input.transfer.reason",
name="Voltage Transfer Reason",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"input.voltage": SensorEntityDescription(
key="input.voltage",
@ -430,6 +480,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
device_class=DEVICE_CLASS_VOLTAGE,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"input.frequency": SensorEntityDescription(
key="input.frequency",
@ -438,6 +489,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
icon="mdi:flash",
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"input.frequency.nominal": SensorEntityDescription(
key="input.frequency.nominal",
@ -445,12 +497,14 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=FREQUENCY_HERTZ,
icon="mdi:flash",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"input.frequency.status": SensorEntityDescription(
key="input.frequency.status",
name="Input Frequency Status",
icon="mdi:information-outline",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"output.current": SensorEntityDescription(
key="output.current",
@ -459,6 +513,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
icon="mdi:flash",
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"output.current.nominal": SensorEntityDescription(
key="output.current.nominal",
@ -466,6 +521,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
icon="mdi:flash",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"output.voltage": SensorEntityDescription(
key="output.voltage",
@ -480,6 +536,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
device_class=DEVICE_CLASS_VOLTAGE,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"output.frequency": SensorEntityDescription(
key="output.frequency",
@ -488,6 +545,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
icon="mdi:flash",
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"output.frequency.nominal": SensorEntityDescription(
key="output.frequency.nominal",
@ -495,6 +553,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
native_unit_of_measurement=FREQUENCY_HERTZ,
icon="mdi:flash",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ambient.humidity": SensorEntityDescription(
key="ambient.humidity",
@ -503,6 +562,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
device_class=DEVICE_CLASS_HUMIDITY,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"ambient.temperature": SensorEntityDescription(
key="ambient.temperature",
@ -511,6 +571,7 @@ SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"watts": SensorEntityDescription(
key="watts",

View File

@ -5,7 +5,7 @@ import logging
from homeassistant.components.nut import PyNUTData
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.const import CONF_RESOURCES, STATE_UNKNOWN
from homeassistant.const import STATE_UNKNOWN
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
@ -35,9 +35,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
unique_id = pynut_data[PYNUT_UNIQUE_ID]
status = coordinator.data
enabled_resources = [
resource.lower() for resource in config_entry.data[CONF_RESOURCES]
]
resources = [sensor_id for sensor_id in SENSOR_TYPES if sensor_id in status]
# Display status is a special case that falls back to the status value
# of the UPS instead.
@ -50,7 +47,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
SENSOR_TYPES[sensor_type],
data,
unique_id,
sensor_type in enabled_resources,
)
for sensor_type in resources
]
@ -67,14 +63,12 @@ class NUTSensor(CoordinatorEntity, SensorEntity):
sensor_description: SensorEntityDescription,
data: PyNUTData,
unique_id: str,
enabled_default: bool,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self.entity_description = sensor_description
device_name = data.name.title()
self._attr_entity_registry_enabled_default = enabled_default
self._attr_name = f"{device_name} {sensor_description.name}"
self._attr_unique_id = f"{unique_id}_{sensor_description.key}"
self._attr_device_info = DeviceInfo(

View File

@ -16,12 +16,6 @@
"alias": "Alias",
"resources": "Resources"
}
},
"resources": {
"title": "Choose the Resources to Monitor",
"data": {
"resources": "Resources"
}
}
},
"error": {

View File

@ -1,19 +1,14 @@
{
"config": {
"abort": {
"already_configured": "Device is already configured"
"already_configured": "Device is already configured",
"resources_not_available": "No known resources found"
},
"error": {
"cannot_connect": "Failed to connect",
"unknown": "Unexpected error"
},
"step": {
"resources": {
"data": {
"resources": "Resources"
},
"title": "Choose the Resources to Monitor"
},
"ups": {
"data": {
"alias": "Alias",
@ -33,17 +28,11 @@
}
},
"options": {
"error": {
"cannot_connect": "Failed to connect",
"unknown": "Unexpected error"
},
"step": {
"init": {
"data": {
"resources": "Resources",
"scan_interval": "Scan Interval (seconds)"
},
"description": "Choose Sensor Resources."
}
}
}
}

View File

@ -44,18 +44,6 @@ async def test_form_zeroconf(hass):
list_vars={"battery.voltage": "voltage", "ups.status": "OL"}, list_ups=["ups1"]
)
with patch(
"homeassistant.components.nut.PyNUTClient",
return_value=mock_pynut,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_USERNAME: "test-username", CONF_PASSWORD: "test-password"},
)
assert result2["step_id"] == "resources"
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
with patch(
"homeassistant.components.nut.PyNUTClient",
return_value=mock_pynut,
@ -63,22 +51,21 @@ async def test_form_zeroconf(hass):
"homeassistant.components.nut.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{CONF_RESOURCES: ["battery.voltage", "ups.status", "ups.status.display"]},
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_USERNAME: "test-username", CONF_PASSWORD: "test-password"},
)
await hass.async_block_till_done()
assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result3["title"] == "192.168.1.5:1234"
assert result3["data"] == {
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result2["title"] == "192.168.1.5:1234"
assert result2["data"] == {
CONF_HOST: "192.168.1.5",
CONF_PASSWORD: "test-password",
CONF_PORT: 1234,
CONF_RESOURCES: ["battery.voltage", "ups.status", "ups.status.display"],
CONF_USERNAME: "test-username",
}
assert result3["result"].unique_id is None
assert result2["result"].unique_id is None
assert len(mock_setup_entry.mock_calls) == 1
@ -98,7 +85,10 @@ async def test_form_user_one_ups(hass):
with patch(
"homeassistant.components.nut.PyNUTClient",
return_value=mock_pynut,
):
), patch(
"homeassistant.components.nut.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
@ -108,30 +98,14 @@ async def test_form_user_one_ups(hass):
CONF_PORT: 2222,
},
)
assert result2["step_id"] == "resources"
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
with patch(
"homeassistant.components.nut.PyNUTClient",
return_value=mock_pynut,
), patch(
"homeassistant.components.nut.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{CONF_RESOURCES: ["battery.voltage", "ups.status", "ups.status.display"]},
)
await hass.async_block_till_done()
assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result3["title"] == "1.1.1.1:2222"
assert result3["data"] == {
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result2["title"] == "1.1.1.1:2222"
assert result2["data"] == {
CONF_HOST: "1.1.1.1",
CONF_PASSWORD: "test-password",
CONF_PORT: 2222,
CONF_RESOURCES: ["battery.voltage", "ups.status", "ups.status.display"],
CONF_USERNAME: "test-username",
}
assert len(mock_setup_entry.mock_calls) == 1
@ -175,18 +149,6 @@ async def test_form_user_multiple_ups(hass):
assert result2["step_id"] == "ups"
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
with patch(
"homeassistant.components.nut.PyNUTClient",
return_value=mock_pynut,
):
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{CONF_ALIAS: "ups2"},
)
assert result3["step_id"] == "resources"
assert result3["type"] == data_entry_flow.RESULT_TYPE_FORM
with patch(
"homeassistant.components.nut.PyNUTClient",
return_value=mock_pynut,
@ -194,20 +156,19 @@ async def test_form_user_multiple_ups(hass):
"homeassistant.components.nut.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"],
{CONF_RESOURCES: ["battery.voltage"]},
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{CONF_ALIAS: "ups2"},
)
await hass.async_block_till_done()
assert result4["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result4["title"] == "ups2@1.1.1.1:2222"
assert result4["data"] == {
assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result3["title"] == "ups2@1.1.1.1:2222"
assert result3["data"] == {
CONF_HOST: "1.1.1.1",
CONF_PASSWORD: "test-password",
CONF_ALIAS: "ups2",
CONF_PORT: 2222,
CONF_RESOURCES: ["battery.voltage"],
CONF_USERNAME: "test-username",
}
assert len(mock_setup_entry.mock_calls) == 2
@ -234,7 +195,10 @@ async def test_form_user_one_ups_with_ignored_entry(hass):
with patch(
"homeassistant.components.nut.PyNUTClient",
return_value=mock_pynut,
):
), patch(
"homeassistant.components.nut.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
@ -244,30 +208,14 @@ async def test_form_user_one_ups_with_ignored_entry(hass):
CONF_PORT: 2222,
},
)
assert result2["step_id"] == "resources"
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
with patch(
"homeassistant.components.nut.PyNUTClient",
return_value=mock_pynut,
), patch(
"homeassistant.components.nut.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{CONF_RESOURCES: ["battery.voltage", "ups.status", "ups.status.display"]},
)
await hass.async_block_till_done()
assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result3["title"] == "1.1.1.1:2222"
assert result3["data"] == {
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result2["title"] == "1.1.1.1:2222"
assert result2["data"] == {
CONF_HOST: "1.1.1.1",
CONF_PASSWORD: "test-password",
CONF_PORT: 2222,
CONF_RESOURCES: ["battery.voltage", "ups.status", "ups.status.display"],
CONF_USERNAME: "test-username",
}
assert len(mock_setup_entry.mock_calls) == 1

View File

@ -3,7 +3,7 @@ from unittest.mock import patch
from homeassistant.components.nut.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_RESOURCES, STATE_UNAVAILABLE
from homeassistant.const import CONF_HOST, CONF_PORT, STATE_UNAVAILABLE
from .util import _get_mock_pynutclient
@ -14,11 +14,7 @@ async def test_async_setup_entry(hass):
"""Test a successful setup entry."""
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_HOST: "mock",
CONF_PORT: "mock",
CONF_RESOURCES: ["ups.status"],
},
data={CONF_HOST: "mock", CONF_PORT: "mock"},
)
entry.add_to_hass(hass)
@ -52,11 +48,7 @@ async def test_config_not_ready(hass):
"""Test for setup failure if connection to broker is missing."""
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_HOST: "mock",
CONF_PORT: "mock",
CONF_RESOURCES: ["ups.status"],
},
data={CONF_HOST: "mock", CONF_PORT: "mock"},
)
entry.add_to_hass(hass)

View File

@ -20,7 +20,7 @@ from tests.common import MockConfigEntry
async def test_pr3000rt2u(hass):
"""Test creation of PR3000RT2U sensors."""
await async_init_integration(hass, "PR3000RT2U", ["battery.charge"])
await async_init_integration(hass, "PR3000RT2U")
registry = er.async_get(hass)
entry = registry.async_get("sensor.ups1_battery_charge")
assert entry
@ -44,7 +44,7 @@ async def test_pr3000rt2u(hass):
async def test_cp1350c(hass):
"""Test creation of CP1350C sensors."""
config_entry = await async_init_integration(hass, "CP1350C", ["battery.charge"])
config_entry = await async_init_integration(hass, "CP1350C")
registry = er.async_get(hass)
entry = registry.async_get("sensor.ups1_battery_charge")
@ -69,7 +69,7 @@ async def test_cp1350c(hass):
async def test_5e850i(hass):
"""Test creation of 5E850I sensors."""
config_entry = await async_init_integration(hass, "5E850I", ["battery.charge"])
config_entry = await async_init_integration(hass, "5E850I")
registry = er.async_get(hass)
entry = registry.async_get("sensor.ups1_battery_charge")
assert entry
@ -93,7 +93,7 @@ async def test_5e850i(hass):
async def test_5e650i(hass):
"""Test creation of 5E650I sensors."""
config_entry = await async_init_integration(hass, "5E650I", ["battery.charge"])
config_entry = await async_init_integration(hass, "5E650I")
registry = er.async_get(hass)
entry = registry.async_get("sensor.ups1_battery_charge")
assert entry
@ -117,7 +117,7 @@ async def test_5e650i(hass):
async def test_backupsses600m1(hass):
"""Test creation of BACKUPSES600M1 sensors."""
await async_init_integration(hass, "BACKUPSES600M1", ["battery.charge"])
await async_init_integration(hass, "BACKUPSES600M1")
registry = er.async_get(hass)
entry = registry.async_get("sensor.ups1_battery_charge")
assert entry
@ -144,9 +144,7 @@ async def test_backupsses600m1(hass):
async def test_cp1500pfclcd(hass):
"""Test creation of CP1500PFCLCD sensors."""
config_entry = await async_init_integration(
hass, "CP1500PFCLCD", ["battery.charge"]
)
config_entry = await async_init_integration(hass, "CP1500PFCLCD")
registry = er.async_get(hass)
entry = registry.async_get("sensor.ups1_battery_charge")
assert entry
@ -170,7 +168,7 @@ async def test_cp1500pfclcd(hass):
async def test_dl650elcd(hass):
"""Test creation of DL650ELCD sensors."""
config_entry = await async_init_integration(hass, "DL650ELCD", ["battery.charge"])
config_entry = await async_init_integration(hass, "DL650ELCD")
registry = er.async_get(hass)
entry = registry.async_get("sensor.ups1_battery_charge")
assert entry
@ -194,7 +192,7 @@ async def test_dl650elcd(hass):
async def test_blazer_usb(hass):
"""Test creation of blazer_usb sensors."""
config_entry = await async_init_integration(hass, "blazer_usb", ["battery.charge"])
config_entry = await async_init_integration(hass, "blazer_usb")
registry = er.async_get(hass)
entry = registry.async_get("sensor.ups1_battery_charge")
assert entry
@ -219,11 +217,7 @@ async def test_state_sensors(hass):
"""Test creation of status display sensors."""
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_HOST: "mock",
CONF_PORT: "mock",
CONF_RESOURCES: ["ups.status", "ups.status.display"],
},
data={CONF_HOST: "mock", CONF_PORT: "mock"},
)
entry.add_to_hass(hass)
@ -248,11 +242,7 @@ async def test_unknown_state_sensors(hass):
"""Test creation of unknown status display sensors."""
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_HOST: "mock",
CONF_PORT: "mock",
CONF_RESOURCES: ["ups.status", "ups.status.display"],
},
data={CONF_HOST: "mock", CONF_PORT: "mock"},
)
entry.add_to_hass(hass)
@ -275,12 +265,34 @@ async def test_unknown_state_sensors(hass):
async def test_stale_options(hass):
"""Test creation of sensors with stale options to remove."""
config_entry = await async_init_integration(
hass, "blazer_usb", ["battery.charge"], True
config_entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_HOST: "mock",
CONF_PORT: "mock",
CONF_RESOURCES: ["battery.charge"],
},
options={CONF_RESOURCES: ["battery.charge"]},
)
registry = er.async_get(hass)
entry = registry.async_get("sensor.ups1_battery_charge")
assert entry
assert entry.unique_id == f"{config_entry.entry_id}_battery.charge"
assert config_entry.options == {}
config_entry.add_to_hass(hass)
mock_pynut = _get_mock_pynutclient(
list_ups={"ups1": "UPS 1"}, list_vars={"battery.charge": "10"}
)
with patch(
"homeassistant.components.nut.PyNUTClient",
return_value=mock_pynut,
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
registry = er.async_get(hass)
entry = registry.async_get("sensor.ups1_battery_charge")
assert entry
assert entry.unique_id == f"{config_entry.entry_id}_battery.charge"
assert CONF_RESOURCES not in config_entry.data
assert config_entry.options == {}
state = hass.states.get("sensor.ups1_battery_charge")
assert state.state == "10"

View File

@ -4,7 +4,7 @@ import json
from unittest.mock import MagicMock, patch
from homeassistant.components.nut.const import DOMAIN
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_RESOURCES
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, load_fixture
@ -18,7 +18,7 @@ def _get_mock_pynutclient(list_vars=None, list_ups=None):
async def async_init_integration(
hass: HomeAssistant, ups_fixture: str, resources: list, add_options: bool = False
hass: HomeAssistant, ups_fixture: str
) -> MockConfigEntry:
"""Set up the nexia integration in Home Assistant."""
@ -33,8 +33,7 @@ async def async_init_integration(
):
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_HOST: "mock", CONF_PORT: "mock", CONF_RESOURCES: resources},
options={CONF_RESOURCES: resources} if add_options else {},
data={CONF_HOST: "mock", CONF_PORT: "mock"},
)
entry.add_to_hass(hass)