|
1 | 1 | """Tests for Shelly valve platform.""" |
2 | 2 |
|
| 3 | +from copy import deepcopy |
3 | 4 | from unittest.mock import Mock |
4 | 5 |
|
5 | 6 | from aioshelly.const import MODEL_GAS |
6 | 7 | import pytest |
7 | 8 |
|
8 | | -from homeassistant.components.valve import DOMAIN as VALVE_DOMAIN, ValveState |
9 | | -from homeassistant.const import ATTR_ENTITY_ID, SERVICE_CLOSE_VALVE, SERVICE_OPEN_VALVE |
| 9 | +from homeassistant.components.shelly.const import ( |
| 10 | + MODEL_FRANKEVER_WATER_VALVE, |
| 11 | + MODEL_NEO_WATER_VALVE, |
| 12 | +) |
| 13 | +from homeassistant.components.valve import ( |
| 14 | + ATTR_CURRENT_POSITION, |
| 15 | + ATTR_POSITION, |
| 16 | + DOMAIN as VALVE_DOMAIN, |
| 17 | + ValveState, |
| 18 | +) |
| 19 | +from homeassistant.const import ( |
| 20 | + ATTR_ENTITY_ID, |
| 21 | + SERVICE_CLOSE_VALVE, |
| 22 | + SERVICE_OPEN_VALVE, |
| 23 | + SERVICE_SET_VALVE_POSITION, |
| 24 | +) |
10 | 25 | from homeassistant.core import HomeAssistant |
11 | 26 | from homeassistant.helpers.entity_registry import EntityRegistry |
12 | 27 |
|
@@ -64,3 +79,157 @@ async def test_block_device_gas_valve( |
64 | 79 |
|
65 | 80 | assert (state := hass.states.get(entity_id)) |
66 | 81 | assert state.state == ValveState.CLOSED |
| 82 | + |
| 83 | + |
| 84 | +async def test_rpc_water_valve( |
| 85 | + hass: HomeAssistant, |
| 86 | + entity_registry: EntityRegistry, |
| 87 | + mock_rpc_device: Mock, |
| 88 | + monkeypatch: pytest.MonkeyPatch, |
| 89 | +) -> None: |
| 90 | + """Test RPC device Shelly Water Valve.""" |
| 91 | + config = deepcopy(mock_rpc_device.config) |
| 92 | + config["number:200"] = { |
| 93 | + "name": "Position", |
| 94 | + "min": 0, |
| 95 | + "max": 100, |
| 96 | + "meta": {"ui": {"step": 10, "view": "slider", "unit": "%"}}, |
| 97 | + "role": "position", |
| 98 | + } |
| 99 | + monkeypatch.setattr(mock_rpc_device, "config", config) |
| 100 | + |
| 101 | + status = deepcopy(mock_rpc_device.status) |
| 102 | + status["number:200"] = {"value": 0} |
| 103 | + monkeypatch.setattr(mock_rpc_device, "status", status) |
| 104 | + |
| 105 | + await init_integration(hass, 3, model=MODEL_FRANKEVER_WATER_VALVE) |
| 106 | + entity_id = "valve.test_name" |
| 107 | + |
| 108 | + assert (entry := entity_registry.async_get(entity_id)) |
| 109 | + assert entry.unique_id == "123456789ABC-number:200-water_valve" |
| 110 | + |
| 111 | + assert (state := hass.states.get(entity_id)) |
| 112 | + assert state.state == ValveState.CLOSED |
| 113 | + |
| 114 | + # Open valve |
| 115 | + await hass.services.async_call( |
| 116 | + VALVE_DOMAIN, |
| 117 | + SERVICE_OPEN_VALVE, |
| 118 | + {ATTR_ENTITY_ID: entity_id}, |
| 119 | + blocking=True, |
| 120 | + ) |
| 121 | + |
| 122 | + mock_rpc_device.number_set.assert_called_once_with(200, 100) |
| 123 | + |
| 124 | + status["number:200"] = {"value": 100} |
| 125 | + monkeypatch.setattr(mock_rpc_device, "status", status) |
| 126 | + mock_rpc_device.mock_update() |
| 127 | + await hass.async_block_till_done() |
| 128 | + |
| 129 | + assert (state := hass.states.get(entity_id)) |
| 130 | + assert state.state == ValveState.OPEN |
| 131 | + |
| 132 | + # Close valve |
| 133 | + mock_rpc_device.number_set.reset_mock() |
| 134 | + await hass.services.async_call( |
| 135 | + VALVE_DOMAIN, |
| 136 | + SERVICE_CLOSE_VALVE, |
| 137 | + {ATTR_ENTITY_ID: entity_id}, |
| 138 | + blocking=True, |
| 139 | + ) |
| 140 | + |
| 141 | + mock_rpc_device.number_set.assert_called_once_with(200, 0) |
| 142 | + |
| 143 | + status["number:200"] = {"value": 0} |
| 144 | + monkeypatch.setattr(mock_rpc_device, "status", status) |
| 145 | + mock_rpc_device.mock_update() |
| 146 | + await hass.async_block_till_done() |
| 147 | + |
| 148 | + assert (state := hass.states.get(entity_id)) |
| 149 | + assert state.state == ValveState.CLOSED |
| 150 | + |
| 151 | + # Set valve position to 50% |
| 152 | + mock_rpc_device.number_set.reset_mock() |
| 153 | + await hass.services.async_call( |
| 154 | + VALVE_DOMAIN, |
| 155 | + SERVICE_SET_VALVE_POSITION, |
| 156 | + {ATTR_ENTITY_ID: entity_id, ATTR_POSITION: 50}, |
| 157 | + blocking=True, |
| 158 | + ) |
| 159 | + |
| 160 | + mock_rpc_device.number_set.assert_called_once_with(200, 50) |
| 161 | + |
| 162 | + status["number:200"] = {"value": 50} |
| 163 | + monkeypatch.setattr(mock_rpc_device, "status", status) |
| 164 | + mock_rpc_device.mock_update() |
| 165 | + await hass.async_block_till_done() |
| 166 | + |
| 167 | + assert (state := hass.states.get(entity_id)) |
| 168 | + assert state.state == ValveState.OPEN |
| 169 | + assert state.attributes.get(ATTR_CURRENT_POSITION) == 50 |
| 170 | + |
| 171 | + |
| 172 | +async def test_rpc_neo_water_valve( |
| 173 | + hass: HomeAssistant, |
| 174 | + entity_registry: EntityRegistry, |
| 175 | + mock_rpc_device: Mock, |
| 176 | + monkeypatch: pytest.MonkeyPatch, |
| 177 | +) -> None: |
| 178 | + """Test RPC device Shelly NEO Water Valve.""" |
| 179 | + config = deepcopy(mock_rpc_device.config) |
| 180 | + config["boolean:200"] = { |
| 181 | + "name": "State", |
| 182 | + "meta": {"ui": {"view": "toggle"}}, |
| 183 | + "role": "state", |
| 184 | + } |
| 185 | + monkeypatch.setattr(mock_rpc_device, "config", config) |
| 186 | + |
| 187 | + status = deepcopy(mock_rpc_device.status) |
| 188 | + status["boolean:200"] = {"value": False} |
| 189 | + monkeypatch.setattr(mock_rpc_device, "status", status) |
| 190 | + |
| 191 | + await init_integration(hass, 3, model=MODEL_NEO_WATER_VALVE) |
| 192 | + entity_id = "valve.test_name" |
| 193 | + |
| 194 | + assert (entry := entity_registry.async_get(entity_id)) |
| 195 | + assert entry.unique_id == "123456789ABC-boolean:200-neo_water_valve" |
| 196 | + |
| 197 | + assert (state := hass.states.get(entity_id)) |
| 198 | + assert state.state == ValveState.CLOSED |
| 199 | + |
| 200 | + # Open valve |
| 201 | + await hass.services.async_call( |
| 202 | + VALVE_DOMAIN, |
| 203 | + SERVICE_OPEN_VALVE, |
| 204 | + {ATTR_ENTITY_ID: entity_id}, |
| 205 | + blocking=True, |
| 206 | + ) |
| 207 | + |
| 208 | + mock_rpc_device.boolean_set.assert_called_once_with(200, True) |
| 209 | + |
| 210 | + status["boolean:200"] = {"value": True} |
| 211 | + monkeypatch.setattr(mock_rpc_device, "status", status) |
| 212 | + mock_rpc_device.mock_update() |
| 213 | + await hass.async_block_till_done() |
| 214 | + |
| 215 | + assert (state := hass.states.get(entity_id)) |
| 216 | + assert state.state == ValveState.OPEN |
| 217 | + |
| 218 | + # Close valve |
| 219 | + mock_rpc_device.boolean_set.reset_mock() |
| 220 | + await hass.services.async_call( |
| 221 | + VALVE_DOMAIN, |
| 222 | + SERVICE_CLOSE_VALVE, |
| 223 | + {ATTR_ENTITY_ID: entity_id}, |
| 224 | + blocking=True, |
| 225 | + ) |
| 226 | + |
| 227 | + mock_rpc_device.boolean_set.assert_called_once_with(200, False) |
| 228 | + |
| 229 | + status["boolean:200"] = {"value": False} |
| 230 | + monkeypatch.setattr(mock_rpc_device, "status", status) |
| 231 | + mock_rpc_device.mock_update() |
| 232 | + await hass.async_block_till_done() |
| 233 | + |
| 234 | + assert (state := hass.states.get(entity_id)) |
| 235 | + assert state.state == ValveState.CLOSED |
0 commit comments