Compare commits

...

1 Commits

Author SHA1 Message Date
wollew
e8885de8c2 add number platform to Velux integration for ExteriorHeating nodes (#162857) 2026-02-19 19:58:13 +01:00
5 changed files with 272 additions and 2 deletions

View File

@@ -10,6 +10,7 @@ PLATFORMS = [
Platform.BUTTON,
Platform.COVER,
Platform.LIGHT,
Platform.NUMBER,
Platform.SCENE,
Platform.SWITCH,
]

View File

@@ -0,0 +1,56 @@
"""Support for Velux exterior heating number entities."""
from __future__ import annotations
from pyvlx import ExteriorHeating, Intensity
from homeassistant.components.number import NumberEntity
from homeassistant.const import PERCENTAGE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import VeluxConfigEntry
from .entity import VeluxEntity, wrap_pyvlx_call_exceptions
PARALLEL_UPDATES = 1
async def async_setup_entry(
hass: HomeAssistant,
config_entry: VeluxConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up number entities for the Velux platform."""
pyvlx = config_entry.runtime_data
async_add_entities(
VeluxExteriorHeatingNumber(node, config_entry.entry_id)
for node in pyvlx.nodes
if isinstance(node, ExteriorHeating)
)
class VeluxExteriorHeatingNumber(VeluxEntity, NumberEntity):
"""Representation of an exterior heating intensity control."""
_attr_native_min_value = 0
_attr_native_max_value = 100
_attr_native_step = 1
_attr_native_unit_of_measurement = PERCENTAGE
_attr_name = None
node: ExteriorHeating
@property
def native_value(self) -> float | None:
"""Return the current heating intensity in percent."""
return (
self.node.intensity.intensity_percent if self.node.intensity.known else None
)
@wrap_pyvlx_call_exceptions
async def async_set_native_value(self, value: float) -> None:
"""Set the heating intensity."""
await self.node.set_intensity(
Intensity(intensity_percent=round(value)),
wait_for_completion=True,
)

View File

@@ -4,8 +4,16 @@ from collections.abc import Generator
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from pyvlx import Light, OnOffLight, OnOffSwitch, Scene
from pyvlx.opening_device import Blind, DualRollerShutter, Window
from pyvlx import (
Blind,
DualRollerShutter,
ExteriorHeating,
Light,
OnOffLight,
OnOffSwitch,
Scene,
Window,
)
from homeassistant.components.velux import DOMAIN
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_PASSWORD, Platform
@@ -131,6 +139,18 @@ def mock_onoff_light() -> AsyncMock:
return light
# an exterior heating device
@pytest.fixture
def mock_exterior_heating() -> AsyncMock:
"""Create a mock Velux exterior heating device."""
exterior_heating = AsyncMock(spec=ExteriorHeating, autospec=True)
exterior_heating.name = "Test Exterior Heating"
exterior_heating.serial_number = "1984"
exterior_heating.intensity = MagicMock(intensity_percent=33)
exterior_heating.pyvlx = MagicMock()
return exterior_heating
# an on/off switch
@pytest.fixture
def mock_onoff_switch() -> AsyncMock:
@@ -168,6 +188,7 @@ def mock_pyvlx(
mock_onoff_switch: AsyncMock,
mock_window: AsyncMock,
mock_blind: AsyncMock,
mock_exterior_heating: AsyncMock,
mock_dual_roller_shutter: AsyncMock,
request: pytest.FixtureRequest,
) -> Generator[MagicMock]:
@@ -190,6 +211,7 @@ def mock_pyvlx(
mock_onoff_switch,
mock_blind,
mock_window,
mock_exterior_heating,
mock_cover_type,
]

View File

@@ -0,0 +1,60 @@
# serializer version: 1
# name: test_number_setup[number.test_exterior_heating-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'max': 100,
'min': 0,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1,
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'number',
'entity_category': None,
'entity_id': 'number.test_exterior_heating',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': None,
'platform': 'velux',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '1984',
'unit_of_measurement': '%',
})
# ---
# name: test_number_setup[number.test_exterior_heating-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Test Exterior Heating',
'max': 100,
'min': 0,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1,
'unit_of_measurement': '%',
}),
'context': <ANY>,
'entity_id': 'number.test_exterior_heating',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '33',
})
# ---

View File

@@ -0,0 +1,131 @@
"""Test Velux number entities."""
from __future__ import annotations
from unittest.mock import AsyncMock
import pytest
from pyvlx import Intensity
from homeassistant.components.number import (
ATTR_VALUE,
DOMAIN as NUMBER_DOMAIN,
SERVICE_SET_VALUE,
)
from homeassistant.components.velux.const import DOMAIN
from homeassistant.const import STATE_UNKNOWN, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import device_registry as dr, entity_registry as er
from . import update_callback_entity
from tests.common import MockConfigEntry, SnapshotAssertion, snapshot_platform
pytestmark = pytest.mark.usefixtures("setup_integration")
@pytest.fixture
def platform() -> Platform:
"""Fixture to specify platform to test."""
return Platform.NUMBER
def get_number_entity_id(mock: AsyncMock) -> str:
"""Helper to get the entity ID for a given mock node."""
return f"number.{mock.name.lower().replace(' ', '_')}"
async def test_number_setup(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Snapshot the entity and validate registry metadata."""
await snapshot_platform(
hass,
entity_registry,
snapshot,
mock_config_entry.entry_id,
)
async def test_number_device_association(
hass: HomeAssistant,
mock_exterior_heating: AsyncMock,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Ensure exterior heating number entity is associated with a device."""
entity_id = get_number_entity_id(mock_exterior_heating)
entry = entity_registry.async_get(entity_id)
assert entry is not None
assert entry.device_id is not None
device_entry = device_registry.async_get(entry.device_id)
assert device_entry is not None
assert (DOMAIN, mock_exterior_heating.serial_number) in device_entry.identifiers
async def test_get_intensity(
hass: HomeAssistant,
mock_exterior_heating: AsyncMock,
) -> None:
"""Entity state follows intensity value and becomes unknown when not known."""
entity_id = get_number_entity_id(mock_exterior_heating)
# Set initial intensity values
mock_exterior_heating.intensity.intensity_percent = 20
await update_callback_entity(hass, mock_exterior_heating)
state = hass.states.get(entity_id)
assert state is not None
assert state.state == "20"
mock_exterior_heating.intensity.known = False
await update_callback_entity(hass, mock_exterior_heating)
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_UNKNOWN
async def test_set_value_sets_intensity(
hass: HomeAssistant,
mock_exterior_heating: AsyncMock,
) -> None:
"""Calling set_value forwards to set_intensity."""
entity_id = get_number_entity_id(mock_exterior_heating)
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{ATTR_VALUE: 30, "entity_id": entity_id},
blocking=True,
)
mock_exterior_heating.set_intensity.assert_awaited_once()
args, kwargs = mock_exterior_heating.set_intensity.await_args
intensity = args[0]
assert isinstance(intensity, Intensity)
assert intensity.intensity_percent == 30
assert kwargs.get("wait_for_completion") is True
async def test_set_invalid_value_fails(
hass: HomeAssistant,
mock_exterior_heating: AsyncMock,
) -> None:
"""Values outside the valid range raise ServiceValidationError and do not call set_intensity."""
entity_id = get_number_entity_id(mock_exterior_heating)
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{ATTR_VALUE: 101, "entity_id": entity_id},
blocking=True,
)
mock_exterior_heating.set_intensity.assert_not_awaited()