Handle all zero serial numbers in NUT (#34045)

* Handle all zero serial numbers in NUT

* Add additional nut tests

* Update homeassistant/components/nut/__init__.py

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>

* remove re

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
pull/34123/head
J. Nick Koston 2020-04-12 17:29:35 -05:00 committed by GitHub
parent c75d3ce8c7
commit 173d7fa060
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 157 additions and 5 deletions

View File

@ -134,7 +134,7 @@ def _firmware_from_status(status):
def _serial_from_status(status):
"""Find the best serialvalue from the status."""
serial = status.get("device.serial") or status.get("ups.serial")
if serial and serial == "unknown":
if serial and (serial.lower() == "unknown" or serial.count("0") == len(serial)):
return None
return serial

View File

@ -9,6 +9,10 @@ async def test_pr3000rt2u(hass):
"""Test creation of PR3000RT2U sensors."""
await async_init_integration(hass, "PR3000RT2U", ["battery.charge"])
registry = await hass.helpers.entity_registry.async_get_registry()
entry = registry.async_get("sensor.ups1_battery_charge")
assert entry
assert entry.unique_id == "CPS_PR3000RT2U_PYVJO2000034_battery.charge"
state = hass.states.get("sensor.ups1_battery_charge")
assert state.state == "100"
@ -30,6 +34,10 @@ async def test_cp1350c(hass):
"""Test creation of CP1350C sensors."""
await async_init_integration(hass, "CP1350C", ["battery.charge"])
registry = await hass.helpers.entity_registry.async_get_registry()
entry = registry.async_get("sensor.ups1_battery_charge")
# No unique id, no registry entry
assert not entry
state = hass.states.get("sensor.ups1_battery_charge")
assert state.state == "100"
@ -51,6 +59,64 @@ async def test_5e850i(hass):
"""Test creation of 5E850I sensors."""
await async_init_integration(hass, "5E850I", ["battery.charge"])
registry = await hass.helpers.entity_registry.async_get_registry()
entry = registry.async_get("sensor.ups1_battery_charge")
# No unique id, no registry entry
assert not entry
state = hass.states.get("sensor.ups1_battery_charge")
assert state.state == "100"
expected_attributes = {
"device_class": "battery",
"friendly_name": "Ups1 Battery Charge",
"state": "Online",
"unit_of_measurement": "%",
}
# Only test for a subset of attributes in case
# HA changes the implementation and a new one appears
assert all(
state.attributes[key] == expected_attributes[key] for key in expected_attributes
)
async def test_backupsses600m1(hass):
"""Test creation of BACKUPSES600M1 sensors."""
await async_init_integration(hass, "BACKUPSES600M1", ["battery.charge"])
registry = await hass.helpers.entity_registry.async_get_registry()
entry = registry.async_get("sensor.ups1_battery_charge")
# No unique id, no registry entry
assert entry
assert (
entry.unique_id
== "American Power Conversion_Back-UPS ES 600M1_4B1713P32195 _battery.charge"
)
state = hass.states.get("sensor.ups1_battery_charge")
assert state.state == "100"
expected_attributes = {
"device_class": "battery",
"friendly_name": "Ups1 Battery Charge",
"state": "Online",
"unit_of_measurement": "%",
}
# Only test for a subset of attributes in case
# HA changes the implementation and a new one appears
assert all(
state.attributes[key] == expected_attributes[key] for key in expected_attributes
)
async def test_cp1500pfclcd(hass):
"""Test creation of CP1500PFCLCD sensors."""
await async_init_integration(hass, "CP1500PFCLCD", ["battery.charge"])
registry = await hass.helpers.entity_registry.async_get_registry()
entry = registry.async_get("sensor.ups1_battery_charge")
# No unique id, no registry entry
assert not entry
state = hass.states.get("sensor.ups1_battery_charge")
assert state.state == "100"

View File

@ -26,10 +26,6 @@ async def async_init_integration(
ups_fixture = f"nut/{ups_fixture}.json"
list_vars = json.loads(load_fixture(ups_fixture))
import pprint
pprint.pprint(list_vars)
mock_pynut = _get_mock_pynutclient(list_ups={"ups1": "UPS 1"}, list_vars=list_vars)
with patch(

47
tests/fixtures/nut/BACKUPSES600M1.json vendored Normal file
View File

@ -0,0 +1,47 @@
{
"ups.realpower.nominal" : "330",
"input.voltage" : "123.0",
"ups.mfr" : "American Power Conversion",
"driver.version" : "2.7.4",
"ups.test.result" : "No test initiated",
"input.voltage.nominal" : "120",
"input.transfer.low" : "92",
"driver.parameter.pollinterval" : "15",
"driver.version.data" : "APC HID 0.96",
"driver.parameter.pollfreq" : "30",
"battery.mfr.date" : "2017/04/01",
"ups.beeper.status" : "enabled",
"battery.date" : "2001/09/25",
"driver.name" : "usbhid-ups",
"battery.charge" : "100",
"ups.status" : "OL",
"ups.model" : "Back-UPS ES 600M1",
"battery.runtime.low" : "120",
"ups.firmware" : "928.a5 .D",
"ups.delay.shutdown" : "20",
"device.model" : "Back-UPS ES 600M1",
"device.serial" : "4B1713P32195 ",
"input.sensitivity" : "medium",
"ups.firmware.aux" : "a5 ",
"input.transfer.reason" : "input voltage out of range",
"ups.timer.reboot" : "0",
"battery.voltage.nominal" : "12.0",
"ups.vendorid" : "051d",
"input.transfer.high" : "139",
"battery.voltage" : "13.7",
"battery.charge.low" : "10",
"battery.type" : "PbAc",
"ups.mfr.date" : "2017/04/01",
"ups.timer.shutdown" : "-1",
"device.mfr" : "American Power Conversion",
"driver.parameter.port" : "auto",
"battery.charge.warning" : "50",
"device.type" : "ups",
"driver.parameter.vendorid" : "051d",
"ups.serial" : "4B1713P32195 ",
"ups.load" : "22",
"driver.version.internal" : "0.41",
"battery.runtime" : "1968",
"driver.parameter.synchronous" : "no",
"ups.productid" : "0002"
}

43
tests/fixtures/nut/CP1500PFCLCD.json vendored Normal file
View File

@ -0,0 +1,43 @@
{
"battery.runtime.low" : "300",
"driver.parameter.port" : "auto",
"ups.delay.shutdown" : "20",
"driver.parameter.pollfreq" : "30",
"ups.beeper.status" : "disabled",
"input.voltage.nominal" : "120",
"device.serial" : "000000000000",
"ups.timer.shutdown" : "-60",
"input.voltage" : "122.0",
"ups.status" : "OL",
"ups.model" : "CP1500PFCLCD",
"device.mfr" : "CPS",
"device.model" : "CP1500PFCLCD",
"input.transfer.low" : "88",
"battery.mfr.date" : "CPS",
"driver.version" : "2.7.4",
"driver.version.data" : "CyberPower HID 0.4",
"driver.parameter.synchronous" : "no",
"ups.realpower.nominal" : "900",
"ups.productid" : "0501",
"ups.mfr" : "CPS",
"ups.vendorid" : "0764",
"driver.version.internal" : "0.41",
"output.voltage" : "138.0",
"battery.runtime" : "10530",
"device.type" : "ups",
"battery.charge.low" : "10",
"ups.timer.start" : "-60",
"driver.parameter.pollinterval" : "15",
"ups.load" : "0",
"ups.serial" : "000000000000",
"input.transfer.high" : "139",
"battery.charge.warning" : "20",
"battery.voltage.nominal" : "24",
"driver.parameter.vendorid" : "0764",
"driver.name" : "usbhid-ups",
"battery.type" : "PbAcid",
"ups.delay.start" : "30",
"battery.voltage" : "24.0",
"battery.charge" : "100",
"ups.test.result" : "No test initiated"
}