Convert nut to a multi step config flow (#33803)

* Convert nut to a multi step config flow

* Users can now choose the ups they want in
step 2 (or skipped if only one)

* Users can now select the resources they want
to monitor based on what is actually available
on the device

* CONF_NAME has been removed as we now get the
name from NUT

* Device classes have been added which allows
the battery charge state to be seen in the
devices UI

* Remove update_interval as its for a followup PR

* explict

* reduce

* fix bug, add tests for options flow

* Test for dupe import

* Test for dupe import

* Call step directly

* Update homeassistant/components/nut/config_flow.py

Co-Authored-By: Martin Hjelmare <marhje52@gmail.com>

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
J. Nick Koston 2020-04-08 11:45:45 -05:00 committed by GitHub
parent db9b6aba31
commit 7383e81609
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 482 additions and 160 deletions

View file

@ -1,37 +1,46 @@
{ {
"config": { "config": {
"abort": { "title": "Network UPS Tools (NUT)",
"already_configured": "Device is already configured" "step": {
}, "user": {
"error": { "title": "Connect to the NUT server",
"cannot_connect": "Failed to connect, please try again", "data": {
"unknown": "Unexpected error" "host": "Host",
}, "port": "Port",
"step": { "username": "Username",
"user": { "password": "Password"
"data": {
"alias": "Alias",
"host": "Host",
"name": "Name",
"password": "Password",
"port": "Port",
"resources": "Resources",
"username": "Username"
},
"description": "If there are multiple UPSs attached to the NUT server, enter the name UPS to query in the 'Alias' field.",
"title": "Connect to the NUT server"
}
},
"title": "Network UPS Tools (NUT)"
},
"options": {
"step": {
"init": {
"data": {
"resources": "Resources"
},
"description": "Choose Sensor Resources"
}
} }
},
"ups": {
"title": "Choose the UPS to Monitor",
"data": {
"alias": "Alias",
"resources": "Resources"
}
},
"resources": {
"title": "Choose the Resources to Monitor",
"data": {
"resources": "Resources"
}
}
},
"error": {
"cannot_connect": "Failed to connect, please try again",
"unknown": "Unexpected error"
},
"abort": {
"already_configured": "Device is already configured"
} }
} },
"options": {
"step": {
"init": {
"description": "Choose Sensor Resources.",
"data": {
"resources": "Resources"
}
}
}
}
}

View file

@ -23,6 +23,7 @@ from .const import (
PYNUT_FIRMWARE, PYNUT_FIRMWARE,
PYNUT_MANUFACTURER, PYNUT_MANUFACTURER,
PYNUT_MODEL, PYNUT_MODEL,
PYNUT_NAME,
PYNUT_STATUS, PYNUT_STATUS,
PYNUT_UNIQUE_ID, PYNUT_UNIQUE_ID,
) )
@ -65,6 +66,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
PYNUT_MANUFACTURER: _manufacturer_from_status(status), PYNUT_MANUFACTURER: _manufacturer_from_status(status),
PYNUT_MODEL: _model_from_status(status), PYNUT_MODEL: _model_from_status(status),
PYNUT_FIRMWARE: _firmware_from_status(status), PYNUT_FIRMWARE: _firmware_from_status(status),
PYNUT_NAME: data.name,
} }
entry.add_update_listener(_async_update_listener) entry.add_update_listener(_async_update_listener)
@ -186,10 +188,19 @@ class PyNUTData:
self.update() self.update()
return self._status return self._status
@property
def name(self):
"""Return the name of the ups."""
return self._alias
def list_ups(self):
"""List UPSes connected to the NUT server."""
return self._client.list_ups()
def _get_alias(self): def _get_alias(self):
"""Get the ups alias from NUT.""" """Get the ups alias from NUT."""
try: try:
return next(iter(self._client.list_ups())) return next(iter(self.list_ups()))
except PyNUTError as err: except PyNUTError as err:
_LOGGER.error("Failure getting NUT ups alias, %s", err) _LOGGER.error("Failure getting NUT ups alias, %s", err)
return None return None

View file

@ -7,7 +7,6 @@ from homeassistant import config_entries, core, exceptions
from homeassistant.const import ( from homeassistant.const import (
CONF_ALIAS, CONF_ALIAS,
CONF_HOST, CONF_HOST,
CONF_NAME,
CONF_PASSWORD, CONF_PASSWORD,
CONF_PORT, CONF_PORT,
CONF_RESOURCES, CONF_RESOURCES,
@ -17,7 +16,7 @@ from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from . import PyNUTData, find_resources_in_config_entry, pynutdata_status from . import PyNUTData, find_resources_in_config_entry, pynutdata_status
from .const import DEFAULT_HOST, DEFAULT_NAME, DEFAULT_PORT, SENSOR_TYPES from .const import DEFAULT_HOST, DEFAULT_PORT, SENSOR_TYPES
from .const import DOMAIN # pylint:disable=unused-import from .const import DOMAIN # pylint:disable=unused-import
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -27,17 +26,39 @@ SENSOR_DICT = {sensor_id: SENSOR_TYPES[sensor_id][0] for sensor_id in SENSOR_TYP
DATA_SCHEMA = vol.Schema( DATA_SCHEMA = vol.Schema(
{ {
vol.Optional(CONF_NAME, default=DEFAULT_NAME): str,
vol.Required(CONF_RESOURCES): cv.multi_select(SENSOR_DICT),
vol.Optional(CONF_HOST, default=DEFAULT_HOST): str, vol.Optional(CONF_HOST, default=DEFAULT_HOST): str,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): int, vol.Optional(CONF_PORT, default=DEFAULT_PORT): int,
vol.Optional(CONF_ALIAS): str,
vol.Optional(CONF_USERNAME): str, vol.Optional(CONF_USERNAME): str,
vol.Optional(CONF_PASSWORD): str, vol.Optional(CONF_PASSWORD): str,
} }
) )
def _resource_schema(available_resources, selected_resources):
"""Resource selection schema."""
known_available_resources = {
sensor_id: sensor[0]
for sensor_id, sensor in SENSOR_TYPES.items()
if sensor_id in available_resources
}
return vol.Schema(
{
vol.Required(CONF_RESOURCES, default=selected_resources): cv.multi_select(
known_available_resources
)
}
)
def _ups_schema(ups_list):
"""UPS selection schema."""
ups_map = {ups: ups for ups in ups_list}
return vol.Schema({vol.Required(CONF_ALIAS): vol.In(ups_map)})
async def validate_input(hass: core.HomeAssistant, data): async def validate_input(hass: core.HomeAssistant, data):
"""Validate the user input allows us to connect. """Validate the user input allows us to connect.
@ -52,16 +73,22 @@ async def validate_input(hass: core.HomeAssistant, data):
data = PyNUTData(host, port, alias, username, password) data = PyNUTData(host, port, alias, username, password)
status = await hass.async_add_executor_job(pynutdata_status, data) ups_list = await hass.async_add_executor_job(data.list_ups)
if not ups_list:
raise CannotConnect
status = await hass.async_add_executor_job(pynutdata_status, data)
if not status: if not status:
raise CannotConnect raise CannotConnect
return {"title": _format_host_port_alias(host, port, alias)} return {"ups_list": ups_list, "available_resources": status}
def _format_host_port_alias(host, port, alias): def _format_host_port_alias(user_input):
"""Format a host, port, and alias so it can be used for comparison or display.""" """Format a host, port, and alias so it can be used for comparison or display."""
host = user_input[CONF_HOST]
port = user_input[CONF_PORT]
alias = user_input.get(CONF_ALIAS)
if alias: if alias:
return f"{alias}@{host}:{port}" return f"{alias}@{host}:{port}"
return f"{host}:{port}" return f"{host}:{port}"
@ -73,40 +100,96 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1 VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
async def async_step_user(self, user_input=None): def __init__(self):
"""Handle the initial step.""" """Initialize the nut config flow."""
self.nut_config = {}
self.available_resources = {}
self.ups_list = None
self.title = None
async def async_step_import(self, user_input=None):
"""Handle the import."""
errors = {} errors = {}
if user_input is not None: if user_input is not None:
if self._host_port_alias_already_configured( if self._host_port_alias_already_configured(user_input):
user_input[CONF_HOST], user_input[CONF_PORT], user_input.get(CONF_ALIAS)
):
return self.async_abort(reason="already_configured") return self.async_abort(reason="already_configured")
try: _, errors = await self._async_validate_or_error(user_input)
info = await validate_input(self.hass, user_input)
except CannotConnect:
errors["base"] = "cannot_connect"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
if "base" not in errors: if not errors:
return self.async_create_entry(title=info["title"], data=user_input) title = _format_host_port_alias(user_input)
return self.async_create_entry(title=title, data=user_input)
return self.async_show_form( return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors step_id="user", data_schema=DATA_SCHEMA, errors=errors
) )
def _host_port_alias_already_configured(self, host, port, alias): async def async_step_user(self, user_input=None):
"""Handle the user input."""
errors = {}
if user_input is not None:
info, errors = await self._async_validate_or_error(user_input)
if not errors:
self.nut_config.update(user_input)
if len(info["ups_list"]) > 1:
self.ups_list = info["ups_list"]
return await self.async_step_ups()
self.available_resources.update(info["available_resources"])
return await self.async_step_resources()
return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
)
async def async_step_ups(self, user_input=None):
"""Handle the picking the ups."""
errors = {}
if user_input is not None:
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)
if not errors:
self.available_resources.update(info["available_resources"])
return await self.async_step_resources()
return self.async_show_form(
step_id="ups", data_schema=_ups_schema(self.ups_list), 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=_resource_schema(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.""" """See if we already have a nut entry matching user input configured."""
existing_host_port_aliases = { existing_host_port_aliases = {
_format_host_port_alias(host, port, alias) _format_host_port_alias(entry.data)
for entry in self._async_current_entries() for entry in self._async_current_entries()
} }
return _format_host_port_alias(host, port, alias) in existing_host_port_aliases return _format_host_port_alias(user_input) in existing_host_port_aliases
async def async_step_import(self, user_input): async def _async_validate_or_error(self, config):
"""Handle import.""" errors = {}
return await self.async_step_user(user_input) info = {}
try:
info = await validate_input(self.hass, config)
except CannotConnect:
errors["base"] = "cannot_connect"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
return info, errors
@staticmethod @staticmethod
@callback @callback
@ -129,14 +212,12 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
resources = find_resources_in_config_entry(self.config_entry) resources = find_resources_in_config_entry(self.config_entry)
data_schema = vol.Schema( info = await validate_input(self.hass, self.config_entry.data)
{
vol.Required(CONF_RESOURCES, default=resources): cv.multi_select( return self.async_show_form(
SENSOR_DICT step_id="init",
), data_schema=_resource_schema(info["available_resources"], resources),
}
) )
return self.async_show_form(step_id="init", data_schema=data_schema)
class CannotConnect(exceptions.HomeAssistantError): class CannotConnect(exceptions.HomeAssistantError):

View file

@ -1,4 +1,9 @@
"""The nut component.""" """The nut component."""
from homeassistant.components.sensor import (
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_POWER,
DEVICE_CLASS_TEMPERATURE,
)
from homeassistant.const import POWER_WATT, TEMP_CELSIUS, TIME_SECONDS, UNIT_PERCENTAGE from homeassistant.const import POWER_WATT, TEMP_CELSIUS, TIME_SECONDS, UNIT_PERCENTAGE
DOMAIN = "nut" DOMAIN = "nut"
@ -19,91 +24,146 @@ PYNUT_UNIQUE_ID = "unique_id"
PYNUT_MANUFACTURER = "manufacturer" PYNUT_MANUFACTURER = "manufacturer"
PYNUT_MODEL = "model" PYNUT_MODEL = "model"
PYNUT_FIRMWARE = "firmware" PYNUT_FIRMWARE = "firmware"
PYNUT_NAME = "name"
SENSOR_TYPES = { SENSOR_TYPES = {
"ups.status.display": ["Status", "", "mdi:information-outline"], "ups.status.display": ["Status", "", "mdi:information-outline", None],
"ups.status": ["Status Data", "", "mdi:information-outline"], "ups.status": ["Status Data", "", "mdi:information-outline", None],
"ups.alarm": ["Alarms", "", "mdi:alarm"], "ups.alarm": ["Alarms", "", "mdi:alarm", None],
"ups.temperature": ["UPS Temperature", TEMP_CELSIUS, "mdi:thermometer"], "ups.temperature": [
"ups.load": ["Load", UNIT_PERCENTAGE, "mdi:gauge"], "UPS Temperature",
"ups.load.high": ["Overload Setting", UNIT_PERCENTAGE, "mdi:gauge"], TEMP_CELSIUS,
"ups.id": ["System identifier", "", "mdi:information-outline"], "mdi:thermometer",
"ups.delay.start": ["Load Restart Delay", TIME_SECONDS, "mdi:timer"], DEVICE_CLASS_TEMPERATURE,
"ups.delay.reboot": ["UPS Reboot Delay", TIME_SECONDS, "mdi:timer"], ],
"ups.delay.shutdown": ["UPS Shutdown Delay", TIME_SECONDS, "mdi:timer"], "ups.load": ["Load", UNIT_PERCENTAGE, "mdi:gauge", None],
"ups.timer.start": ["Load Start Timer", TIME_SECONDS, "mdi:timer"], "ups.load.high": ["Overload Setting", UNIT_PERCENTAGE, "mdi:gauge", None],
"ups.timer.reboot": ["Load Reboot Timer", TIME_SECONDS, "mdi:timer"], "ups.id": ["System identifier", "", "mdi:information-outline", None],
"ups.timer.shutdown": ["Load Shutdown Timer", TIME_SECONDS, "mdi:timer"], "ups.delay.start": ["Load Restart Delay", TIME_SECONDS, "mdi:timer", None],
"ups.test.interval": ["Self-Test Interval", TIME_SECONDS, "mdi:timer"], "ups.delay.reboot": ["UPS Reboot Delay", TIME_SECONDS, "mdi:timer", None],
"ups.test.result": ["Self-Test Result", "", "mdi:information-outline"], "ups.delay.shutdown": ["UPS Shutdown Delay", TIME_SECONDS, "mdi:timer", None],
"ups.test.date": ["Self-Test Date", "", "mdi:calendar"], "ups.timer.start": ["Load Start Timer", TIME_SECONDS, "mdi:timer", None],
"ups.display.language": ["Language", "", "mdi:information-outline"], "ups.timer.reboot": ["Load Reboot Timer", TIME_SECONDS, "mdi:timer", None],
"ups.contacts": ["External Contacts", "", "mdi:information-outline"], "ups.timer.shutdown": ["Load Shutdown Timer", TIME_SECONDS, "mdi:timer", None],
"ups.efficiency": ["Efficiency", UNIT_PERCENTAGE, "mdi:gauge"], "ups.test.interval": ["Self-Test Interval", TIME_SECONDS, "mdi:timer", None],
"ups.power": ["Current Apparent Power", "VA", "mdi:flash"], "ups.test.result": ["Self-Test Result", "", "mdi:information-outline", None],
"ups.power.nominal": ["Nominal Power", "VA", "mdi:flash"], "ups.test.date": ["Self-Test Date", "", "mdi:calendar", None],
"ups.realpower": ["Current Real Power", POWER_WATT, "mdi:flash"], "ups.display.language": ["Language", "", "mdi:information-outline", None],
"ups.realpower.nominal": ["Nominal Real Power", POWER_WATT, "mdi:flash"], "ups.contacts": ["External Contacts", "", "mdi:information-outline", None],
"ups.beeper.status": ["Beeper Status", "", "mdi:information-outline"], "ups.efficiency": ["Efficiency", UNIT_PERCENTAGE, "mdi:gauge", None],
"ups.type": ["UPS Type", "", "mdi:information-outline"], "ups.power": ["Current Apparent Power", "VA", "mdi:flash", None],
"ups.watchdog.status": ["Watchdog Status", "", "mdi:information-outline"], "ups.power.nominal": ["Nominal Power", "VA", "mdi:flash", None],
"ups.start.auto": ["Start on AC", "", "mdi:information-outline"], "ups.realpower": [
"ups.start.battery": ["Start on Battery", "", "mdi:information-outline"], "Current Real Power",
"ups.start.reboot": ["Reboot on Battery", "", "mdi:information-outline"], POWER_WATT,
"ups.shutdown": ["Shutdown Ability", "", "mdi:information-outline"], "mdi:flash",
"battery.charge": ["Battery Charge", UNIT_PERCENTAGE, "mdi:gauge"], DEVICE_CLASS_POWER,
"battery.charge.low": ["Low Battery Setpoint", UNIT_PERCENTAGE, "mdi:gauge"], ],
"ups.realpower.nominal": [
"Nominal Real Power",
POWER_WATT,
"mdi:flash",
DEVICE_CLASS_POWER,
],
"ups.beeper.status": ["Beeper Status", "", "mdi:information-outline", None],
"ups.type": ["UPS Type", "", "mdi:information-outline", None],
"ups.watchdog.status": ["Watchdog Status", "", "mdi:information-outline", None],
"ups.start.auto": ["Start on AC", "", "mdi:information-outline", None],
"ups.start.battery": ["Start on Battery", "", "mdi:information-outline", None],
"ups.start.reboot": ["Reboot on Battery", "", "mdi:information-outline", None],
"ups.shutdown": ["Shutdown Ability", "", "mdi:information-outline", None],
"battery.charge": [
"Battery Charge",
UNIT_PERCENTAGE,
"mdi:gauge",
DEVICE_CLASS_BATTERY,
],
"battery.charge.low": ["Low Battery Setpoint", UNIT_PERCENTAGE, "mdi:gauge", None],
"battery.charge.restart": [ "battery.charge.restart": [
"Minimum Battery to Start", "Minimum Battery to Start",
UNIT_PERCENTAGE, UNIT_PERCENTAGE,
"mdi:gauge", "mdi:gauge",
None,
], ],
"battery.charge.warning": [ "battery.charge.warning": [
"Warning Battery Setpoint", "Warning Battery Setpoint",
UNIT_PERCENTAGE, UNIT_PERCENTAGE,
"mdi:gauge", "mdi:gauge",
None,
], ],
"battery.charger.status": ["Charging Status", "", "mdi:information-outline"], "battery.charger.status": ["Charging Status", "", "mdi:information-outline", None],
"battery.voltage": ["Battery Voltage", "V", "mdi:flash"], "battery.voltage": ["Battery Voltage", "V", "mdi:flash", None],
"battery.voltage.nominal": ["Nominal Battery Voltage", "V", "mdi:flash"], "battery.voltage.nominal": ["Nominal Battery Voltage", "V", "mdi:flash", None],
"battery.voltage.low": ["Low Battery Voltage", "V", "mdi:flash"], "battery.voltage.low": ["Low Battery Voltage", "V", "mdi:flash", None],
"battery.voltage.high": ["High Battery Voltage", "V", "mdi:flash"], "battery.voltage.high": ["High Battery Voltage", "V", "mdi:flash", None],
"battery.capacity": ["Battery Capacity", "Ah", "mdi:flash"], "battery.capacity": ["Battery Capacity", "Ah", "mdi:flash", None],
"battery.current": ["Battery Current", "A", "mdi:flash"], "battery.current": ["Battery Current", "A", "mdi:flash", None],
"battery.current.total": ["Total Battery Current", "A", "mdi:flash"], "battery.current.total": ["Total Battery Current", "A", "mdi:flash", None],
"battery.temperature": ["Battery Temperature", TEMP_CELSIUS, "mdi:thermometer"], "battery.temperature": [
"battery.runtime": ["Battery Runtime", TIME_SECONDS, "mdi:timer"], "Battery Temperature",
"battery.runtime.low": ["Low Battery Runtime", TIME_SECONDS, "mdi:timer"], TEMP_CELSIUS,
"mdi:thermometer",
DEVICE_CLASS_TEMPERATURE,
],
"battery.runtime": ["Battery Runtime", TIME_SECONDS, "mdi:timer", None],
"battery.runtime.low": ["Low Battery Runtime", TIME_SECONDS, "mdi:timer", None],
"battery.runtime.restart": [ "battery.runtime.restart": [
"Minimum Battery Runtime to Start", "Minimum Battery Runtime to Start",
TIME_SECONDS, TIME_SECONDS,
"mdi:timer", "mdi:timer",
None,
], ],
"battery.alarm.threshold": [ "battery.alarm.threshold": [
"Battery Alarm Threshold", "Battery Alarm Threshold",
"", "",
"mdi:information-outline", "mdi:information-outline",
None,
], ],
"battery.date": ["Battery Date", "", "mdi:calendar"], "battery.date": ["Battery Date", "", "mdi:calendar", None],
"battery.mfr.date": ["Battery Manuf. Date", "", "mdi:calendar"], "battery.mfr.date": ["Battery Manuf. Date", "", "mdi:calendar", None],
"battery.packs": ["Number of Batteries", "", "mdi:information-outline"], "battery.packs": ["Number of Batteries", "", "mdi:information-outline", None],
"battery.packs.bad": ["Number of Bad Batteries", "", "mdi:information-outline"], "battery.packs.bad": [
"battery.type": ["Battery Chemistry", "", "mdi:information-outline"], "Number of Bad Batteries",
"input.sensitivity": ["Input Power Sensitivity", "", "mdi:information-outline"], "",
"input.transfer.low": ["Low Voltage Transfer", "V", "mdi:flash"], "mdi:information-outline",
"input.transfer.high": ["High Voltage Transfer", "V", "mdi:flash"], None,
"input.transfer.reason": ["Voltage Transfer Reason", "", "mdi:information-outline"], ],
"input.voltage": ["Input Voltage", "V", "mdi:flash"], "battery.type": ["Battery Chemistry", "", "mdi:information-outline", None],
"input.voltage.nominal": ["Nominal Input Voltage", "V", "mdi:flash"], "input.sensitivity": [
"input.frequency": ["Input Line Frequency", "hz", "mdi:flash"], "Input Power Sensitivity",
"input.frequency.nominal": ["Nominal Input Line Frequency", "hz", "mdi:flash"], "",
"input.frequency.status": ["Input Frequency Status", "", "mdi:information-outline"], "mdi:information-outline",
"output.current": ["Output Current", "A", "mdi:flash"], None,
"output.current.nominal": ["Nominal Output Current", "A", "mdi:flash"], ],
"output.voltage": ["Output Voltage", "V", "mdi:flash"], "input.transfer.low": ["Low Voltage Transfer", "V", "mdi:flash", None],
"output.voltage.nominal": ["Nominal Output Voltage", "V", "mdi:flash"], "input.transfer.high": ["High Voltage Transfer", "V", "mdi:flash", None],
"output.frequency": ["Output Frequency", "hz", "mdi:flash"], "input.transfer.reason": [
"output.frequency.nominal": ["Nominal Output Frequency", "hz", "mdi:flash"], "Voltage Transfer Reason",
"",
"mdi:information-outline",
None,
],
"input.voltage": ["Input Voltage", "V", "mdi:flash", None],
"input.voltage.nominal": ["Nominal Input Voltage", "V", "mdi:flash", None],
"input.frequency": ["Input Line Frequency", "hz", "mdi:flash", None],
"input.frequency.nominal": [
"Nominal Input Line Frequency",
"hz",
"mdi:flash",
None,
],
"input.frequency.status": [
"Input Frequency Status",
"",
"mdi:information-outline",
None,
],
"output.current": ["Output Current", "A", "mdi:flash", None],
"output.current.nominal": ["Nominal Output Current", "A", "mdi:flash", None],
"output.voltage": ["Output Voltage", "V", "mdi:flash", None],
"output.voltage.nominal": ["Nominal Output Voltage", "V", "mdi:flash", None],
"output.frequency": ["Output Frequency", "hz", "mdi:flash", None],
"output.frequency.nominal": ["Nominal Output Frequency", "hz", "mdi:flash", None],
} }
STATE_TYPES = { STATE_TYPES = {
@ -123,3 +183,8 @@ STATE_TYPES = {
"FSD": "Forced Shutdown", "FSD": "Forced Shutdown",
"ALARM": "Alarm", "ALARM": "Alarm",
} }
SENSOR_NAME = 0
SENSOR_UNIT = 1
SENSOR_ICON = 2
SENSOR_DEVICE_CLASS = 3

View file

@ -31,9 +31,14 @@ from .const import (
PYNUT_FIRMWARE, PYNUT_FIRMWARE,
PYNUT_MANUFACTURER, PYNUT_MANUFACTURER,
PYNUT_MODEL, PYNUT_MODEL,
PYNUT_NAME,
PYNUT_STATUS, PYNUT_STATUS,
PYNUT_UNIQUE_ID, PYNUT_UNIQUE_ID,
SENSOR_DEVICE_CLASS,
SENSOR_ICON,
SENSOR_NAME,
SENSOR_TYPES, SENSOR_TYPES,
SENSOR_UNIT,
STATE_TYPES, STATE_TYPES,
) )
@ -69,7 +74,6 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the NUT sensors.""" """Set up the NUT sensors."""
config = config_entry.data
pynut_data = hass.data[DOMAIN][config_entry.entry_id] pynut_data = hass.data[DOMAIN][config_entry.entry_id]
data = pynut_data[PYNUT_DATA] data = pynut_data[PYNUT_DATA]
status = pynut_data[PYNUT_STATUS] status = pynut_data[PYNUT_STATUS]
@ -77,10 +81,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
manufacturer = pynut_data[PYNUT_MANUFACTURER] manufacturer = pynut_data[PYNUT_MANUFACTURER]
model = pynut_data[PYNUT_MODEL] model = pynut_data[PYNUT_MODEL]
firmware = pynut_data[PYNUT_FIRMWARE] firmware = pynut_data[PYNUT_FIRMWARE]
name = pynut_data[PYNUT_NAME]
entities = [] entities = []
name = config[CONF_NAME]
if CONF_RESOURCES in config_entry.options: if CONF_RESOURCES in config_entry.options:
resources = config_entry.options[CONF_RESOURCES] resources = config_entry.options[CONF_RESOURCES]
else: else:
@ -96,7 +100,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
): ):
entities.append( entities.append(
NUTSensor( NUTSensor(
name, data, sensor_type, unique_id, manufacturer, model, firmware name.title(),
data,
sensor_type,
unique_id,
manufacturer,
model,
firmware,
) )
) )
else: else:
@ -122,8 +132,8 @@ class NUTSensor(Entity):
self._firmware = firmware self._firmware = firmware
self._model = model self._model = model
self._device_name = name self._device_name = name
self._name = f"{name} {SENSOR_TYPES[sensor_type][0]}" self._name = f"{name} {SENSOR_TYPES[sensor_type][SENSOR_NAME]}"
self._unit = SENSOR_TYPES[sensor_type][1] self._unit = SENSOR_TYPES[sensor_type][SENSOR_UNIT]
self._state = None self._state = None
self._unique_id = unique_id self._unique_id = unique_id
self._display_state = None self._display_state = None
@ -161,7 +171,16 @@ class NUTSensor(Entity):
@property @property
def icon(self): def icon(self):
"""Icon to use in the frontend, if any.""" """Icon to use in the frontend, if any."""
return SENSOR_TYPES[self._type][2] if SENSOR_TYPES[self._type][SENSOR_DEVICE_CLASS]:
# The UI will assign an icon
# if it has a class
return None
return SENSOR_TYPES[self._type][SENSOR_ICON]
@property
def device_class(self):
"""Device class of the sensor."""
return SENSOR_TYPES[self._type][SENSOR_DEVICE_CLASS]
@property @property
def state(self): def state(self):

View file

@ -4,14 +4,23 @@
"step": { "step": {
"user": { "user": {
"title": "Connect to the NUT server", "title": "Connect to the NUT server",
"description": "If there are multiple UPSs attached to the NUT server, enter the name UPS to query in the 'Alias' field.",
"data": { "data": {
"name": "Name",
"host": "Host", "host": "Host",
"port": "Port", "port": "Port",
"alias": "Alias",
"username": "Username", "username": "Username",
"password": "Password", "password": "Password"
}
},
"ups": {
"title": "Choose the UPS to Monitor",
"data": {
"alias": "Alias",
"resources": "Resources"
}
},
"resources": {
"title": "Choose the Resources to Monitor",
"data": {
"resources": "Resources" "resources": "Resources"
} }
} }
@ -27,7 +36,7 @@
"options": { "options": {
"step": { "step": {
"init": { "init": {
"description": "Choose Sensor Resources", "description": "Choose Sensor Resources.",
"data": { "data": {
"resources": "Resources" "resources": "Resources"
} }

View file

@ -1,18 +1,28 @@
"""Test the Network UPS Tools (NUT) config flow.""" """Test the Network UPS Tools (NUT) config flow."""
from asynctest import MagicMock, patch from asynctest import MagicMock, patch
from homeassistant import config_entries, setup from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.components.nut.const import DOMAIN from homeassistant.components.nut.const import DOMAIN
from homeassistant.const import CONF_RESOURCES
from tests.common import MockConfigEntry
VALID_CONFIG = {
"host": "localhost",
"port": 123,
"name": "name",
"resources": ["battery.charge"],
}
def _get_mock_pynutclient(list_vars=None): def _get_mock_pynutclient(list_vars=None, list_ups=None):
pynutclient = MagicMock() pynutclient = MagicMock()
type(pynutclient).list_ups = MagicMock(return_value=["ups1"]) type(pynutclient).list_ups = MagicMock(return_value=list_ups)
type(pynutclient).list_vars = MagicMock(return_value=list_vars) type(pynutclient).list_vars = MagicMock(return_value=list_vars)
return pynutclient return pynutclient
async def test_form(hass): async def test_form_user_one_ups(hass):
"""Test we get the form.""" """Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {}) await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
@ -21,7 +31,25 @@ async def test_form(hass):
assert result["type"] == "form" assert result["type"] == "form"
assert result["errors"] == {} assert result["errors"] == {}
mock_pynut = _get_mock_pynutclient(list_vars={"battery.voltage": "voltage"}) mock_pynut = _get_mock_pynutclient(
list_vars={"battery.voltage": "voltage"}, list_ups=["ups1"]
)
with patch(
"homeassistant.components.nut.PyNUTClient", return_value=mock_pynut,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"username": "test-username",
"password": "test-password",
"port": 2222,
},
)
assert result2["step_id"] == "resources"
assert result2["type"] == "form"
with patch( with patch(
"homeassistant.components.nut.PyNUTClient", return_value=mock_pynut, "homeassistant.components.nut.PyNUTClient", return_value=mock_pynut,
@ -30,6 +58,40 @@ async def test_form(hass):
) as mock_setup, patch( ) as mock_setup, patch(
"homeassistant.components.nut.async_setup_entry", return_value=True, "homeassistant.components.nut.async_setup_entry", return_value=True,
) as mock_setup_entry: ) as mock_setup_entry:
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], {"resources": ["battery.voltage"]},
)
assert result3["type"] == "create_entry"
assert result3["title"] == "1.1.1.1:2222"
assert result3["data"] == {
"host": "1.1.1.1",
"password": "test-password",
"port": 2222,
"resources": ["battery.voltage"],
"username": "test-username",
}
await hass.async_block_till_done()
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_user_multiple_ups(hass):
"""Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["errors"] == {}
mock_pynut = _get_mock_pynutclient(
list_vars={"battery.voltage": "voltage"}, list_ups=["ups1", "ups2"]
)
with patch(
"homeassistant.components.nut.PyNUTClient", return_value=mock_pynut,
):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{ {
@ -37,20 +99,41 @@ async def test_form(hass):
"username": "test-username", "username": "test-username",
"password": "test-password", "password": "test-password",
"port": 2222, "port": 2222,
"alias": "ups1",
"resources": ["battery.charge"],
}, },
) )
assert result2["type"] == "create_entry" assert result2["step_id"] == "ups"
assert result2["title"] == "ups1@1.1.1.1:2222" assert result2["type"] == "form"
assert result2["data"] == {
"alias": "ups1", with patch(
"homeassistant.components.nut.PyNUTClient", return_value=mock_pynut,
):
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], {"alias": "ups2"},
)
assert result3["step_id"] == "resources"
assert result3["type"] == "form"
with patch(
"homeassistant.components.nut.PyNUTClient", return_value=mock_pynut,
), patch(
"homeassistant.components.nut.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.nut.async_setup_entry", return_value=True,
) as mock_setup_entry:
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"], {"resources": ["battery.voltage"]},
)
assert result4["type"] == "create_entry"
assert result4["title"] == "ups2@1.1.1.1:2222"
assert result4["data"] == {
"host": "1.1.1.1", "host": "1.1.1.1",
"name": "NUT UPS",
"password": "test-password", "password": "test-password",
"alias": "ups2",
"port": 2222, "port": 2222,
"resources": ["battery.charge"], "resources": ["battery.voltage"],
"username": "test-username", "username": "test-username",
} }
await hass.async_block_till_done() await hass.async_block_till_done()
@ -62,7 +145,9 @@ async def test_form_import(hass):
"""Test we get the form with import source.""" """Test we get the form with import source."""
await setup.async_setup_component(hass, "persistent_notification", {}) await setup.async_setup_component(hass, "persistent_notification", {})
mock_pynut = _get_mock_pynutclient(list_vars={"battery.voltage": "serial"}) mock_pynut = _get_mock_pynutclient(
list_vars={"battery.voltage": "serial"}, list_ups=["ups1"]
)
with patch( with patch(
"homeassistant.components.nut.PyNUTClient", return_value=mock_pynut, "homeassistant.components.nut.PyNUTClient", return_value=mock_pynut,
@ -95,6 +180,20 @@ async def test_form_import(hass):
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
async def test_form_import_dupe(hass):
"""Test we get abort on duplicate import."""
await setup.async_setup_component(hass, "persistent_notification", {})
entry = MockConfigEntry(domain=DOMAIN, data=VALID_CONFIG)
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "import"}, data=VALID_CONFIG
)
assert result["type"] == "abort"
assert result["reason"] == "already_configured"
async def test_form_cannot_connect(hass): async def test_form_cannot_connect(hass):
"""Test we handle cannot connect error.""" """Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
@ -113,10 +212,39 @@ async def test_form_cannot_connect(hass):
"username": "test-username", "username": "test-username",
"password": "test-password", "password": "test-password",
"port": 2222, "port": 2222,
"alias": "ups1",
"resources": ["battery.charge"],
}, },
) )
assert result2["type"] == "form" assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"} assert result2["errors"] == {"base": "cannot_connect"}
async def test_options_flow(hass):
"""Test config flow options."""
config_entry = MockConfigEntry(
domain=DOMAIN,
unique_id="abcde12345",
data=VALID_CONFIG,
options={CONF_RESOURCES: ["battery.charge"]},
)
config_entry.add_to_hass(hass)
mock_pynut = _get_mock_pynutclient(
list_vars={"battery.voltage": "voltage"}, list_ups=["ups1"]
)
with patch(
"homeassistant.components.nut.PyNUTClient", return_value=mock_pynut,
), patch("homeassistant.components.nut.async_setup_entry", return_value=True):
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={CONF_RESOURCES: ["battery.voltage"]}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert config_entry.options == {CONF_RESOURCES: ["battery.voltage"]}