|
| 1 | +"""Test the NUT button platform.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS |
| 6 | +from homeassistant.components.nut.const import INTEGRATION_SUPPORTED_COMMANDS |
| 7 | +from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN |
| 8 | +from homeassistant.core import HomeAssistant |
| 9 | +from homeassistant.helpers import entity_registry as er |
| 10 | + |
| 11 | +from .util import async_init_integration |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.parametrize( |
| 15 | + "model", |
| 16 | + [ |
| 17 | + "CP1350C", |
| 18 | + "5E650I", |
| 19 | + "5E850I", |
| 20 | + "CP1500PFCLCD", |
| 21 | + "DL650ELCD", |
| 22 | + "EATON5P1550", |
| 23 | + "blazer_usb", |
| 24 | + ], |
| 25 | +) |
| 26 | +async def test_buttons_ups( |
| 27 | + hass: HomeAssistant, entity_registry: er.EntityRegistry, model: str |
| 28 | +) -> None: |
| 29 | + """Tests that there are no standard buttons.""" |
| 30 | + |
| 31 | + list_commands_return_value = { |
| 32 | + supported_command: supported_command |
| 33 | + for supported_command in INTEGRATION_SUPPORTED_COMMANDS |
| 34 | + } |
| 35 | + |
| 36 | + await async_init_integration( |
| 37 | + hass, |
| 38 | + model, |
| 39 | + list_commands_return_value=list_commands_return_value, |
| 40 | + ) |
| 41 | + |
| 42 | + button = hass.states.get("button.ups1_power_cycle_outlet_1") |
| 43 | + assert not button |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize( |
| 47 | + ("model", "unique_id_base"), |
| 48 | + [ |
| 49 | + ( |
| 50 | + "EATON-EPDU-G3", |
| 51 | + "EATON_ePDU MA 00U-C IN: TYPE 00A 0P OUT: 00xTYPE_A000A00000_", |
| 52 | + ), |
| 53 | + ], |
| 54 | +) |
| 55 | +async def test_buttons_pdu_dynamic_outlets( |
| 56 | + hass: HomeAssistant, |
| 57 | + entity_registry: er.EntityRegistry, |
| 58 | + model: str, |
| 59 | + unique_id_base: str, |
| 60 | +) -> None: |
| 61 | + """Tests that the button entities are correct.""" |
| 62 | + |
| 63 | + list_commands_return_value = { |
| 64 | + supported_command: supported_command |
| 65 | + for supported_command in INTEGRATION_SUPPORTED_COMMANDS |
| 66 | + } |
| 67 | + |
| 68 | + for num in range(1, 25): |
| 69 | + command = f"outlet.{num!s}.load.cycle" |
| 70 | + list_commands_return_value[command] = command |
| 71 | + |
| 72 | + await async_init_integration( |
| 73 | + hass, |
| 74 | + model, |
| 75 | + list_commands_return_value=list_commands_return_value, |
| 76 | + ) |
| 77 | + |
| 78 | + entity_id = "button.ups1_power_cycle_outlet_a1" |
| 79 | + entry = entity_registry.async_get(entity_id) |
| 80 | + assert entry |
| 81 | + assert entry.unique_id == f"{unique_id_base}outlet.1.load.cycle" |
| 82 | + |
| 83 | + button = hass.states.get(entity_id) |
| 84 | + assert button |
| 85 | + assert button.state == STATE_UNKNOWN |
| 86 | + |
| 87 | + await hass.services.async_call( |
| 88 | + BUTTON_DOMAIN, |
| 89 | + SERVICE_PRESS, |
| 90 | + {ATTR_ENTITY_ID: entity_id}, |
| 91 | + blocking=True, |
| 92 | + ) |
| 93 | + await hass.async_block_till_done() |
| 94 | + |
| 95 | + button = hass.states.get(entity_id) |
| 96 | + assert button.state != STATE_UNKNOWN |
| 97 | + |
| 98 | + button = hass.states.get("button.ups1_power_cycle_outlet_25") |
| 99 | + assert not button |
| 100 | + |
| 101 | + button = hass.states.get("button.ups1_power_cycle_outlet_a25") |
| 102 | + assert not button |
0 commit comments