Compare commits

...

3 Commits

Author SHA1 Message Date
abmantis
c2229bfbdf Add translated reasons to Govee Light Local setup failures 2026-02-19 22:11:18 +00:00
Patrick Vorgers
0996ad4d1d Add pagination support for IDrive e2 (#162960) 2026-02-19 22:42:04 +01:00
wollew
e8885de8c2 add number platform to Velux integration for ExteriorHeating nodes (#162857) 2026-02-19 19:58:13 +01:00
10 changed files with 424 additions and 40 deletions

View File

@@ -15,7 +15,7 @@ from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from .const import DISCOVERY_TIMEOUT
from .const import DISCOVERY_TIMEOUT, DOMAIN
from .coordinator import GoveeLocalApiCoordinator, GoveeLocalConfigEntry
PLATFORMS: list[Platform] = [Platform.LIGHT]
@@ -52,7 +52,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: GoveeLocalConfigEntry) -
_LOGGER.error("Start failed, errno: %d", ex.errno)
return False
_LOGGER.error("Port %s already in use", LISTENING_PORT)
raise ConfigEntryNotReady from ex
raise ConfigEntryNotReady(
translation_domain=DOMAIN,
translation_key="port_in_use",
translation_placeholders={"port": LISTENING_PORT},
) from ex
await coordinator.async_config_entry_first_refresh()
@@ -61,7 +65,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: GoveeLocalConfigEntry) -
while not coordinator.devices:
await asyncio.sleep(delay=1)
except TimeoutError as ex:
raise ConfigEntryNotReady from ex
raise ConfigEntryNotReady(
translation_domain=DOMAIN, translation_key="no_devices_found"
) from ex
entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

View File

@@ -33,5 +33,13 @@
}
}
}
},
"exceptions": {
"no_devices_found": {
"message": "[%key:common::config_flow::abort::no_devices_found%]"
},
"port_in_use": {
"message": "Port {port} is already in use"
}
}
}

View File

@@ -329,14 +329,14 @@ class IDriveE2BackupAgent(BackupAgent):
return self._backup_cache
backups = {}
response = await cast(Any, self._client).list_objects_v2(Bucket=self._bucket)
# Filter for metadata files only
metadata_files = [
obj
for obj in response.get("Contents", [])
if obj["Key"].endswith(".metadata.json")
]
paginator = self._client.get_paginator("list_objects_v2")
metadata_files: list[dict[str, Any]] = []
async for page in paginator.paginate(Bucket=self._bucket):
metadata_files.extend(
obj
for obj in page.get("Contents", [])
if obj["Key"].endswith(".metadata.json")
)
for metadata_file in metadata_files:
try:

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,7 +4,7 @@ from __future__ import annotations
from collections.abc import AsyncIterator, Generator
import json
from unittest.mock import AsyncMock, patch
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -53,9 +53,11 @@ def mock_client(agent_backup: AgentBackup) -> Generator[AsyncMock]:
client = create_client.return_value
tar_file, metadata_file = suggested_filenames(agent_backup)
client.list_objects_v2.return_value = {
"Contents": [{"Key": tar_file}, {"Key": metadata_file}]
}
# Mock the paginator for list_objects_v2
client.get_paginator = MagicMock()
client.get_paginator.return_value.paginate.return_value.__aiter__.return_value = [
{"Contents": [{"Key": tar_file}, {"Key": metadata_file}]}
]
client.create_multipart_upload.return_value = {"UploadId": "upload_id"}
client.upload_part.return_value = {"ETag": "etag"}
client.list_buckets.return_value = {

View File

@@ -179,7 +179,9 @@ async def test_agents_get_backup_does_not_throw_on_not_found(
mock_client: MagicMock,
) -> None:
"""Test agent get backup does not throw on a backup not found."""
mock_client.list_objects_v2.return_value = {"Contents": []}
mock_client.get_paginator.return_value.paginate.return_value.__aiter__.return_value = [
{"Contents": []}
]
client = await hass_ws_client(hass)
await client.send_json_auto_id({"type": "backup/details", "backup_id": "random"})
@@ -202,18 +204,20 @@ async def test_agents_list_backups_with_corrupted_metadata(
agent = IDriveE2BackupAgent(hass, mock_config_entry)
# Set up mock responses for both valid and corrupted metadata files
mock_client.list_objects_v2.return_value = {
"Contents": [
{
"Key": "valid_backup.metadata.json",
"LastModified": "2023-01-01T00:00:00+00:00",
},
{
"Key": "corrupted_backup.metadata.json",
"LastModified": "2023-01-01T00:00:00+00:00",
},
]
}
mock_client.get_paginator.return_value.paginate.return_value.__aiter__.return_value = [
{
"Contents": [
{
"Key": "valid_backup.metadata.json",
"LastModified": "2023-01-01T00:00:00+00:00",
},
{
"Key": "corrupted_backup.metadata.json",
"LastModified": "2023-01-01T00:00:00+00:00",
},
]
}
]
# Mock responses for get_object calls
valid_metadata = json.dumps(agent_backup.as_dict())
@@ -270,7 +274,9 @@ async def test_agents_delete_not_throwing_on_not_found(
mock_client: MagicMock,
) -> None:
"""Test agent delete backup does not throw on a backup not found."""
mock_client.list_objects_v2.return_value = {"Contents": []}
mock_client.get_paginator.return_value.paginate.return_value.__aiter__.return_value = [
{"Contents": []}
]
client = await hass_ws_client(hass)
@@ -284,7 +290,7 @@ async def test_agents_delete_not_throwing_on_not_found(
assert response["success"]
assert response["result"] == {"agent_errors": {}}
assert mock_client.delete_object.call_count == 0
assert mock_client.delete_objects.call_count == 0
async def test_agents_upload(
@@ -490,20 +496,27 @@ async def test_cache_expiration(
metadata_content = json.dumps(agent_backup.as_dict())
mock_body = AsyncMock()
mock_body.read.return_value = metadata_content.encode()
mock_client.list_objects_v2.return_value = {
"Contents": [
{"Key": "test.metadata.json", "LastModified": "2023-01-01T00:00:00+00:00"}
]
}
mock_client.get_paginator.return_value.paginate.return_value.__aiter__.return_value = [
{
"Contents": [
{
"Key": "test.metadata.json",
"LastModified": "2023-01-01T00:00:00+00:00",
}
]
}
]
mock_client.get_object.return_value = {"Body": mock_body}
# First call should query IDrive e2
await agent.async_list_backups()
assert mock_client.list_objects_v2.call_count == 1
assert mock_client.get_paginator.call_count == 1
assert mock_client.get_object.call_count == 1
# Second call should use cache
await agent.async_list_backups()
assert mock_client.list_objects_v2.call_count == 1
assert mock_client.get_paginator.call_count == 1
assert mock_client.get_object.call_count == 1
# Set cache to expire
@@ -511,7 +524,7 @@ async def test_cache_expiration(
# Third call should query IDrive e2 again
await agent.async_list_backups()
assert mock_client.list_objects_v2.call_count == 2
assert mock_client.get_paginator.call_count == 2
assert mock_client.get_object.call_count == 2
@@ -526,3 +539,88 @@ async def test_listeners_get_cleaned_up(hass: HomeAssistant) -> None:
remove_listener()
assert DATA_BACKUP_AGENT_LISTENERS not in hass.data
async def test_list_backups_with_pagination(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test listing backups when paginating through multiple pages."""
# Create agent
agent = IDriveE2BackupAgent(hass, mock_config_entry)
# Create two different backups
backup1 = AgentBackup(
backup_id="backup1",
date="2023-01-01T00:00:00+00:00",
addons=[],
database_included=False,
extra_metadata={},
folders=[],
homeassistant_included=False,
homeassistant_version=None,
name="Backup 1",
protected=False,
size=0,
)
backup2 = AgentBackup(
backup_id="backup2",
date="2023-01-02T00:00:00+00:00",
addons=[],
database_included=False,
extra_metadata={},
folders=[],
homeassistant_included=False,
homeassistant_version=None,
name="Backup 2",
protected=False,
size=0,
)
# Setup two pages of results
page1 = {
"Contents": [
{
"Key": "backup1.metadata.json",
"LastModified": "2023-01-01T00:00:00+00:00",
},
{"Key": "backup1.tar", "LastModified": "2023-01-01T00:00:00+00:00"},
]
}
page2 = {
"Contents": [
{
"Key": "backup2.metadata.json",
"LastModified": "2023-01-02T00:00:00+00:00",
},
{"Key": "backup2.tar", "LastModified": "2023-01-02T00:00:00+00:00"},
]
}
# Setup mock client
mock_client = mock_config_entry.runtime_data
mock_client.get_paginator.return_value.paginate.return_value.__aiter__.return_value = [
page1,
page2,
]
# Mock get_object responses based on the key
async def mock_get_object(**kwargs):
"""Mock get_object with different responses based on the key."""
key = kwargs.get("Key", "")
if "backup1" in key:
mock_body = AsyncMock()
mock_body.read.return_value = json.dumps(backup1.as_dict()).encode()
return {"Body": mock_body}
# backup2
mock_body = AsyncMock()
mock_body.read.return_value = json.dumps(backup2.as_dict()).encode()
return {"Body": mock_body}
mock_client.get_object.side_effect = mock_get_object
# List backups and verify we got both
backups = await agent.async_list_backups()
assert len(backups) == 2
backup_ids = {backup.backup_id for backup in backups}
assert backup_ids == {"backup1", "backup2"}

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()