Skip to content

Commit e8885de

Browse files
authored
add number platform to Velux integration for ExteriorHeating nodes (home-assistant#162857)
1 parent 03d9c2c commit e8885de

File tree

5 files changed

+272
-2
lines changed

5 files changed

+272
-2
lines changed

homeassistant/components/velux/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Platform.BUTTON,
1111
Platform.COVER,
1212
Platform.LIGHT,
13+
Platform.NUMBER,
1314
Platform.SCENE,
1415
Platform.SWITCH,
1516
]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Support for Velux exterior heating number entities."""
2+
3+
from __future__ import annotations
4+
5+
from pyvlx import ExteriorHeating, Intensity
6+
7+
from homeassistant.components.number import NumberEntity
8+
from homeassistant.const import PERCENTAGE
9+
from homeassistant.core import HomeAssistant
10+
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
11+
12+
from . import VeluxConfigEntry
13+
from .entity import VeluxEntity, wrap_pyvlx_call_exceptions
14+
15+
PARALLEL_UPDATES = 1
16+
17+
18+
async def async_setup_entry(
19+
hass: HomeAssistant,
20+
config_entry: VeluxConfigEntry,
21+
async_add_entities: AddConfigEntryEntitiesCallback,
22+
) -> None:
23+
"""Set up number entities for the Velux platform."""
24+
pyvlx = config_entry.runtime_data
25+
async_add_entities(
26+
VeluxExteriorHeatingNumber(node, config_entry.entry_id)
27+
for node in pyvlx.nodes
28+
if isinstance(node, ExteriorHeating)
29+
)
30+
31+
32+
class VeluxExteriorHeatingNumber(VeluxEntity, NumberEntity):
33+
"""Representation of an exterior heating intensity control."""
34+
35+
_attr_native_min_value = 0
36+
_attr_native_max_value = 100
37+
_attr_native_step = 1
38+
_attr_native_unit_of_measurement = PERCENTAGE
39+
_attr_name = None
40+
41+
node: ExteriorHeating
42+
43+
@property
44+
def native_value(self) -> float | None:
45+
"""Return the current heating intensity in percent."""
46+
return (
47+
self.node.intensity.intensity_percent if self.node.intensity.known else None
48+
)
49+
50+
@wrap_pyvlx_call_exceptions
51+
async def async_set_native_value(self, value: float) -> None:
52+
"""Set the heating intensity."""
53+
await self.node.set_intensity(
54+
Intensity(intensity_percent=round(value)),
55+
wait_for_completion=True,
56+
)

tests/components/velux/conftest.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44
from unittest.mock import AsyncMock, MagicMock, patch
55

66
import pytest
7-
from pyvlx import Light, OnOffLight, OnOffSwitch, Scene
8-
from pyvlx.opening_device import Blind, DualRollerShutter, Window
7+
from pyvlx import (
8+
Blind,
9+
DualRollerShutter,
10+
ExteriorHeating,
11+
Light,
12+
OnOffLight,
13+
OnOffSwitch,
14+
Scene,
15+
Window,
16+
)
917

1018
from homeassistant.components.velux import DOMAIN
1119
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_PASSWORD, Platform
@@ -131,6 +139,18 @@ def mock_onoff_light() -> AsyncMock:
131139
return light
132140

133141

142+
# an exterior heating device
143+
@pytest.fixture
144+
def mock_exterior_heating() -> AsyncMock:
145+
"""Create a mock Velux exterior heating device."""
146+
exterior_heating = AsyncMock(spec=ExteriorHeating, autospec=True)
147+
exterior_heating.name = "Test Exterior Heating"
148+
exterior_heating.serial_number = "1984"
149+
exterior_heating.intensity = MagicMock(intensity_percent=33)
150+
exterior_heating.pyvlx = MagicMock()
151+
return exterior_heating
152+
153+
134154
# an on/off switch
135155
@pytest.fixture
136156
def mock_onoff_switch() -> AsyncMock:
@@ -168,6 +188,7 @@ def mock_pyvlx(
168188
mock_onoff_switch: AsyncMock,
169189
mock_window: AsyncMock,
170190
mock_blind: AsyncMock,
191+
mock_exterior_heating: AsyncMock,
171192
mock_dual_roller_shutter: AsyncMock,
172193
request: pytest.FixtureRequest,
173194
) -> Generator[MagicMock]:
@@ -190,6 +211,7 @@ def mock_pyvlx(
190211
mock_onoff_switch,
191212
mock_blind,
192213
mock_window,
214+
mock_exterior_heating,
193215
mock_cover_type,
194216
]
195217

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# serializer version: 1
2+
# name: test_number_setup[number.test_exterior_heating-entry]
3+
EntityRegistryEntrySnapshot({
4+
'aliases': set({
5+
}),
6+
'area_id': None,
7+
'capabilities': dict({
8+
'max': 100,
9+
'min': 0,
10+
'mode': <NumberMode.AUTO: 'auto'>,
11+
'step': 1,
12+
}),
13+
'config_entry_id': <ANY>,
14+
'config_subentry_id': <ANY>,
15+
'device_class': None,
16+
'device_id': <ANY>,
17+
'disabled_by': None,
18+
'domain': 'number',
19+
'entity_category': None,
20+
'entity_id': 'number.test_exterior_heating',
21+
'has_entity_name': True,
22+
'hidden_by': None,
23+
'icon': None,
24+
'id': <ANY>,
25+
'labels': set({
26+
}),
27+
'name': None,
28+
'object_id_base': None,
29+
'options': dict({
30+
}),
31+
'original_device_class': None,
32+
'original_icon': None,
33+
'original_name': None,
34+
'platform': 'velux',
35+
'previous_unique_id': None,
36+
'suggested_object_id': None,
37+
'supported_features': 0,
38+
'translation_key': None,
39+
'unique_id': '1984',
40+
'unit_of_measurement': '%',
41+
})
42+
# ---
43+
# name: test_number_setup[number.test_exterior_heating-state]
44+
StateSnapshot({
45+
'attributes': ReadOnlyDict({
46+
'friendly_name': 'Test Exterior Heating',
47+
'max': 100,
48+
'min': 0,
49+
'mode': <NumberMode.AUTO: 'auto'>,
50+
'step': 1,
51+
'unit_of_measurement': '%',
52+
}),
53+
'context': <ANY>,
54+
'entity_id': 'number.test_exterior_heating',
55+
'last_changed': <ANY>,
56+
'last_reported': <ANY>,
57+
'last_updated': <ANY>,
58+
'state': '33',
59+
})
60+
# ---
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
"""Test Velux number entities."""
2+
3+
from __future__ import annotations
4+
5+
from unittest.mock import AsyncMock
6+
7+
import pytest
8+
from pyvlx import Intensity
9+
10+
from homeassistant.components.number import (
11+
ATTR_VALUE,
12+
DOMAIN as NUMBER_DOMAIN,
13+
SERVICE_SET_VALUE,
14+
)
15+
from homeassistant.components.velux.const import DOMAIN
16+
from homeassistant.const import STATE_UNKNOWN, Platform
17+
from homeassistant.core import HomeAssistant
18+
from homeassistant.exceptions import ServiceValidationError
19+
from homeassistant.helpers import device_registry as dr, entity_registry as er
20+
21+
from . import update_callback_entity
22+
23+
from tests.common import MockConfigEntry, SnapshotAssertion, snapshot_platform
24+
25+
pytestmark = pytest.mark.usefixtures("setup_integration")
26+
27+
28+
@pytest.fixture
29+
def platform() -> Platform:
30+
"""Fixture to specify platform to test."""
31+
return Platform.NUMBER
32+
33+
34+
def get_number_entity_id(mock: AsyncMock) -> str:
35+
"""Helper to get the entity ID for a given mock node."""
36+
return f"number.{mock.name.lower().replace(' ', '_')}"
37+
38+
39+
async def test_number_setup(
40+
hass: HomeAssistant,
41+
mock_config_entry: MockConfigEntry,
42+
entity_registry: er.EntityRegistry,
43+
snapshot: SnapshotAssertion,
44+
) -> None:
45+
"""Snapshot the entity and validate registry metadata."""
46+
await snapshot_platform(
47+
hass,
48+
entity_registry,
49+
snapshot,
50+
mock_config_entry.entry_id,
51+
)
52+
53+
54+
async def test_number_device_association(
55+
hass: HomeAssistant,
56+
mock_exterior_heating: AsyncMock,
57+
entity_registry: er.EntityRegistry,
58+
device_registry: dr.DeviceRegistry,
59+
) -> None:
60+
"""Ensure exterior heating number entity is associated with a device."""
61+
entity_id = get_number_entity_id(mock_exterior_heating)
62+
63+
entry = entity_registry.async_get(entity_id)
64+
assert entry is not None
65+
assert entry.device_id is not None
66+
device_entry = device_registry.async_get(entry.device_id)
67+
assert device_entry is not None
68+
assert (DOMAIN, mock_exterior_heating.serial_number) in device_entry.identifiers
69+
70+
71+
async def test_get_intensity(
72+
hass: HomeAssistant,
73+
mock_exterior_heating: AsyncMock,
74+
) -> None:
75+
"""Entity state follows intensity value and becomes unknown when not known."""
76+
entity_id = get_number_entity_id(mock_exterior_heating)
77+
78+
# Set initial intensity values
79+
mock_exterior_heating.intensity.intensity_percent = 20
80+
await update_callback_entity(hass, mock_exterior_heating)
81+
82+
state = hass.states.get(entity_id)
83+
assert state is not None
84+
assert state.state == "20"
85+
86+
mock_exterior_heating.intensity.known = False
87+
await update_callback_entity(hass, mock_exterior_heating)
88+
89+
state = hass.states.get(entity_id)
90+
assert state is not None
91+
assert state.state == STATE_UNKNOWN
92+
93+
94+
async def test_set_value_sets_intensity(
95+
hass: HomeAssistant,
96+
mock_exterior_heating: AsyncMock,
97+
) -> None:
98+
"""Calling set_value forwards to set_intensity."""
99+
entity_id = get_number_entity_id(mock_exterior_heating)
100+
101+
await hass.services.async_call(
102+
NUMBER_DOMAIN,
103+
SERVICE_SET_VALUE,
104+
{ATTR_VALUE: 30, "entity_id": entity_id},
105+
blocking=True,
106+
)
107+
108+
mock_exterior_heating.set_intensity.assert_awaited_once()
109+
args, kwargs = mock_exterior_heating.set_intensity.await_args
110+
intensity = args[0]
111+
assert isinstance(intensity, Intensity)
112+
assert intensity.intensity_percent == 30
113+
assert kwargs.get("wait_for_completion") is True
114+
115+
116+
async def test_set_invalid_value_fails(
117+
hass: HomeAssistant,
118+
mock_exterior_heating: AsyncMock,
119+
) -> None:
120+
"""Values outside the valid range raise ServiceValidationError and do not call set_intensity."""
121+
entity_id = get_number_entity_id(mock_exterior_heating)
122+
123+
with pytest.raises(ServiceValidationError):
124+
await hass.services.async_call(
125+
NUMBER_DOMAIN,
126+
SERVICE_SET_VALUE,
127+
{ATTR_VALUE: 101, "entity_id": entity_id},
128+
blocking=True,
129+
)
130+
131+
mock_exterior_heating.set_intensity.assert_not_awaited()

0 commit comments

Comments
 (0)