Skip to content

Commit e7edd51

Browse files
thecodeCopilot
andauthored
Refactor Shelly number platform to use upstream set_thermostat_state (home-assistant#157527)
Co-authored-by: Copilot <[email protected]>
1 parent 0c4f232 commit e7edd51

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

homeassistant/components/shelly/const.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,5 @@ class DeprecatedFirmwareInfo(TypedDict):
341341
MODEL_FRANKEVER_IRRIGATION_CONTROLLER = "Irrigation"
342342

343343
ROLE_GENERIC = "generic"
344+
345+
TRV_CHANNEL = 0

homeassistant/components/shelly/number.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from collections.abc import Callable
66
from dataclasses import dataclass
7-
from typing import TYPE_CHECKING, Any, Final, cast
7+
from typing import TYPE_CHECKING, Final, cast
88

99
from aioshelly.block_device import Block
1010
from aioshelly.const import RPC_GENERATIONS
@@ -34,6 +34,7 @@
3434
MODEL_LINKEDGO_ST1820_THERMOSTAT,
3535
MODEL_TOP_EV_CHARGER_EVE01,
3636
ROLE_GENERIC,
37+
TRV_CHANNEL,
3738
VIRTUAL_NUMBER_MODE_MAP,
3839
)
3940
from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
@@ -62,9 +63,6 @@
6263
class BlockNumberDescription(BlockEntityDescription, NumberEntityDescription):
6364
"""Class to describe a BLOCK sensor."""
6465

65-
rest_path: str = ""
66-
rest_arg: str = ""
67-
6866

6967
@dataclass(frozen=True, kw_only=True)
7068
class RpcNumberDescription(RpcEntityDescription, NumberEntityDescription):
@@ -178,7 +176,7 @@ async def async_set_native_value(self, value: float) -> None:
178176
self.async_write_ha_state()
179177

180178

181-
NUMBERS: dict[tuple[str, str], BlockNumberDescription] = {
179+
BLOCK_NUMBERS: dict[tuple[str, str], BlockNumberDescription] = {
182180
("device", "valvePos"): BlockNumberDescription(
183181
key="device|valvepos",
184182
translation_key="valve_position",
@@ -189,8 +187,6 @@ async def async_set_native_value(self, value: float) -> None:
189187
native_max_value=100,
190188
native_step=1,
191189
mode=NumberMode.SLIDER,
192-
rest_path="thermostat/0",
193-
rest_arg="pos",
194190
),
195191
}
196192

@@ -357,7 +353,7 @@ def _async_setup_block_entry(
357353
hass,
358354
config_entry,
359355
async_add_entities,
360-
NUMBERS,
356+
BLOCK_NUMBERS,
361357
BlockSleepingNumber,
362358
)
363359

@@ -426,18 +422,11 @@ def native_value(self) -> float | None:
426422

427423
async def async_set_native_value(self, value: float) -> None:
428424
"""Set value."""
429-
# Example for Shelly Valve: http://192.168.188.187/thermostat/0?pos=13.0
430-
await self._set_state_full_path(
431-
self.entity_description.rest_path,
432-
{self.entity_description.rest_arg: value},
425+
LOGGER.debug(
426+
"Setting thermostat position for entity %s to %s", self.name, value
433427
)
434-
self.async_write_ha_state()
435-
436-
async def _set_state_full_path(self, path: str, params: Any) -> Any:
437-
"""Set block state (HTTP request)."""
438-
LOGGER.debug("Setting state for entity %s, state: %s", self.name, params)
439428
try:
440-
return await self.coordinator.device.http_request("get", path, params)
429+
await self.coordinator.device.set_thermostat_state(TRV_CHANNEL, pos=value)
441430
except DeviceConnectionError as err:
442431
self.coordinator.last_update_success = False
443432
raise HomeAssistantError(
@@ -450,3 +439,4 @@ async def _set_state_full_path(self, path: str, params: Any) -> Any:
450439
) from err
451440
except InvalidAuthError:
452441
await self.coordinator.async_shutdown_device_and_start_reauth()
442+
self.async_write_ha_state()

tests/components/shelly/test_number.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,7 @@ async def test_block_number_set_value(
192192
{ATTR_ENTITY_ID: "number.test_name_valve_position", ATTR_VALUE: 30},
193193
blocking=True,
194194
)
195-
mock_block_device.http_request.assert_called_once_with(
196-
"get", "thermostat/0", {"pos": 30.0}
197-
)
195+
mock_block_device.set_thermostat_state.assert_called_once_with(0, pos=30.0)
198196

199197

200198
async def test_block_set_value_connection_error(
@@ -208,7 +206,7 @@ async def test_block_set_value_connection_error(
208206
)
209207
monkeypatch.setattr(
210208
mock_block_device,
211-
"http_request",
209+
"set_thermostat_state",
212210
AsyncMock(side_effect=DeviceConnectionError),
213211
)
214212
await init_integration(hass, 1, sleep_period=3600)
@@ -240,7 +238,7 @@ async def test_block_set_value_auth_error(
240238
)
241239
monkeypatch.setattr(
242240
mock_block_device,
243-
"http_request",
241+
"set_thermostat_state",
244242
AsyncMock(side_effect=InvalidAuthError),
245243
)
246244
entry = await init_integration(hass, 1, sleep_period=3600)

0 commit comments

Comments
 (0)