Add new Liebherr integration (#161197)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
mettolen
2026-01-29 14:49:09 +02:00
committed by GitHub
parent acbdbc9be7
commit 72e7bf7f9c
22 changed files with 1350 additions and 0 deletions

2
CODEOWNERS generated
View File

@@ -921,6 +921,8 @@ build.json @home-assistant/supervisor
/tests/components/libre_hardware_monitor/ @Sab44
/homeassistant/components/lidarr/ @tkdrob
/tests/components/lidarr/ @tkdrob
/homeassistant/components/liebherr/ @mettolen
/tests/components/liebherr/ @mettolen
/homeassistant/components/lifx/ @Djelibeybi
/tests/components/lifx/ @Djelibeybi
/homeassistant/components/light/ @home-assistant/core

View File

@@ -0,0 +1,67 @@
"""The liebherr integration."""
from __future__ import annotations
import asyncio
from pyliebherrhomeapi import LiebherrClient
from pyliebherrhomeapi.exceptions import (
LiebherrAuthenticationError,
LiebherrConnectionError,
)
from homeassistant.const import CONF_API_KEY, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .coordinator import LiebherrConfigEntry, LiebherrCoordinator
PLATFORMS: list[Platform] = [Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: LiebherrConfigEntry) -> bool:
"""Set up Liebherr from a config entry."""
# Create shared API client
client = LiebherrClient(
api_key=entry.data[CONF_API_KEY],
session=async_get_clientsession(hass),
)
# Fetch device list to create coordinators
try:
devices = await client.get_devices()
except LiebherrAuthenticationError as err:
raise ConfigEntryError("Invalid API key") from err
except LiebherrConnectionError as err:
raise ConfigEntryNotReady(f"Failed to connect to Liebherr API: {err}") from err
# Create a coordinator for each device (may be empty if no devices)
coordinators: dict[str, LiebherrCoordinator] = {}
for device in devices:
coordinator = LiebherrCoordinator(
hass=hass,
config_entry=entry,
client=client,
device_id=device.device_id,
)
coordinators[device.device_id] = coordinator
await asyncio.gather(
*(
coordinator.async_config_entry_first_refresh()
for coordinator in coordinators.values()
)
)
# Store coordinators in runtime data
entry.runtime_data = coordinators
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: LiebherrConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@@ -0,0 +1,68 @@
"""Config flow for the liebherr integration."""
from __future__ import annotations
import logging
from typing import Any
from pyliebherrhomeapi import LiebherrClient
from pyliebherrhomeapi.exceptions import (
LiebherrAuthenticationError,
LiebherrConnectionError,
)
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_API_KEY
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_API_KEY): str,
}
)
class LiebherrConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for liebherr."""
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the initial step."""
errors: dict[str, str] = {}
if user_input is not None:
user_input[CONF_API_KEY] = user_input[CONF_API_KEY].strip()
self._async_abort_entries_match({CONF_API_KEY: user_input[CONF_API_KEY]})
try:
# Create a client and test the connection
client = LiebherrClient(
api_key=user_input[CONF_API_KEY],
session=async_get_clientsession(self.hass),
)
devices = await client.get_devices()
except LiebherrAuthenticationError:
errors["base"] = "invalid_auth"
except LiebherrConnectionError:
errors["base"] = "cannot_connect"
except Exception:
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
if not devices:
return self.async_abort(reason="no_devices")
return self.async_create_entry(
title="Liebherr",
data=user_input,
)
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)

View File

@@ -0,0 +1,6 @@
"""Constants for the liebherr integration."""
from typing import Final
DOMAIN: Final = "liebherr"
MANUFACTURER: Final = "Liebherr"

View File

@@ -0,0 +1,75 @@
"""DataUpdateCoordinator for Liebherr integration."""
from __future__ import annotations
from datetime import timedelta
import logging
from pyliebherrhomeapi import (
DeviceState,
LiebherrAuthenticationError,
LiebherrClient,
LiebherrConnectionError,
LiebherrTimeoutError,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DOMAIN
type LiebherrConfigEntry = ConfigEntry[dict[str, LiebherrCoordinator]]
_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=60)
class LiebherrCoordinator(DataUpdateCoordinator[DeviceState]):
"""Class to manage fetching Liebherr data from the API for a single device."""
def __init__(
self,
hass: HomeAssistant,
config_entry: LiebherrConfigEntry,
client: LiebherrClient,
device_id: str,
) -> None:
"""Initialize coordinator."""
super().__init__(
hass,
logger=_LOGGER,
name=f"{DOMAIN}_{device_id}",
update_interval=SCAN_INTERVAL,
config_entry=config_entry,
)
self.client = client
self.device_id = device_id
async def _async_setup(self) -> None:
"""Set up the coordinator by validating device access."""
try:
await self.client.get_device(self.device_id)
except LiebherrAuthenticationError as err:
raise ConfigEntryError("Invalid API key") from err
except LiebherrConnectionError as err:
raise ConfigEntryNotReady(
f"Failed to connect to device {self.device_id}: {err}"
) from err
async def _async_update_data(self) -> DeviceState:
"""Fetch data from API for this device."""
try:
return await self.client.get_device_state(self.device_id)
except LiebherrAuthenticationError as err:
raise ConfigEntryError("API key is no longer valid") from err
except LiebherrTimeoutError as err:
raise UpdateFailed(
f"Timeout communicating with device {self.device_id}"
) from err
except LiebherrConnectionError as err:
raise UpdateFailed(
f"Error communicating with device {self.device_id}"
) from err

View File

@@ -0,0 +1,75 @@
"""Base entity for Liebherr integration."""
from __future__ import annotations
from pyliebherrhomeapi import TemperatureControl, ZonePosition
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN, MANUFACTURER
from .coordinator import LiebherrCoordinator
# Zone position to translation key mapping
ZONE_POSITION_MAP = {
ZonePosition.TOP: "top_zone",
ZonePosition.MIDDLE: "middle_zone",
ZonePosition.BOTTOM: "bottom_zone",
}
class LiebherrEntity(CoordinatorEntity[LiebherrCoordinator]):
"""Base entity for Liebherr devices."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: LiebherrCoordinator,
) -> None:
"""Initialize the Liebherr entity."""
super().__init__(coordinator)
device = coordinator.data.device
model = None
if device.device_type:
model = device.device_type.title()
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, coordinator.device_id)},
name=device.nickname or device.device_name,
manufacturer=MANUFACTURER,
model=model,
model_id=device.device_name,
)
class LiebherrZoneEntity(LiebherrEntity):
"""Base entity for zone-based Liebherr entities.
This class should be used for entities that are associated with a specific
temperature control zone (e.g., climate, zone sensors).
"""
def __init__(
self,
coordinator: LiebherrCoordinator,
zone_id: int,
) -> None:
"""Initialize the zone entity."""
super().__init__(coordinator)
self._zone_id = zone_id
@property
def temperature_control(self) -> TemperatureControl | None:
"""Get the temperature control for this zone."""
return self.coordinator.data.get_temperature_controls().get(self._zone_id)
def _get_zone_translation_key(self) -> str | None:
"""Get the translation key for this zone."""
control = self.temperature_control
if control and isinstance(control.zone_position, ZonePosition):
return ZONE_POSITION_MAP.get(control.zone_position)
# Fallback to None to use device model name
return None

View File

@@ -0,0 +1,18 @@
{
"domain": "liebherr",
"name": "Liebherr",
"codeowners": ["@mettolen"],
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/liebherr",
"integration_type": "hub",
"iot_class": "cloud_polling",
"loggers": ["pyliebherrhomeapi"],
"quality_scale": "bronze",
"requirements": ["pyliebherrhomeapi==0.2.1"],
"zeroconf": [
{
"name": "liebherr*",
"type": "_http._tcp.local."
}
]
}

View File

@@ -0,0 +1,72 @@
rules:
# Bronze
action-setup:
status: exempt
comment: Integration does not register custom actions.
appropriate-polling: done
brands: done
common-modules: done
config-flow-test-coverage: done
config-flow: done
dependency-transparency: done
docs-actions:
status: exempt
comment: Integration does not register custom actions.
docs-high-level-description: done
docs-installation-instructions: done
docs-removal-instructions: done
entity-event-setup: done
entity-unique-id: done
has-entity-name: done
runtime-data: done
test-before-configure: done
test-before-setup: done
unique-config-entry: done
# Silver
action-exceptions:
status: exempt
comment: Integration does not register custom actions.
config-entry-unloading: done
docs-configuration-parameters:
status: exempt
comment: Integration has no configurable parameters after initial setup.
docs-installation-parameters: done
entity-unavailable: done
integration-owner: done
log-when-unavailable: todo
parallel-updates: done
reauthentication-flow: todo
test-coverage: done
# Gold
devices: done
diagnostics: todo
discovery-update-info:
status: exempt
comment: Cloud API does not require updating entry data from network discovery.
discovery: done
docs-data-update: done
docs-examples: todo
docs-known-limitations: done
docs-supported-devices: done
docs-supported-functions: done
docs-troubleshooting: done
docs-use-cases: done
dynamic-devices: todo
entity-category: done
entity-device-class: todo
entity-disabled-by-default: todo
entity-translations: done
exception-translations: todo
icon-translations: todo
reconfiguration-flow: todo
repair-issues:
status: exempt
comment: No repair issues to implement at this time.
stale-devices: todo
# Platinum
async-dependency: done
inject-websession: done
strict-typing: todo

View File

@@ -0,0 +1,118 @@
"""Sensor platform for Liebherr integration."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from pyliebherrhomeapi import TemperatureControl, TemperatureUnit
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.typing import StateType
from .coordinator import LiebherrConfigEntry, LiebherrCoordinator
from .entity import LiebherrZoneEntity
PARALLEL_UPDATES = 0
@dataclass(frozen=True, kw_only=True)
class LiebherrSensorEntityDescription(SensorEntityDescription):
"""Describes Liebherr sensor entity."""
value_fn: Callable[[TemperatureControl], StateType]
unit_fn: Callable[[TemperatureControl], str]
SENSOR_TYPES: tuple[LiebherrSensorEntityDescription, ...] = (
LiebherrSensorEntityDescription(
key="temperature",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=0,
value_fn=lambda control: control.value,
unit_fn=lambda control: (
UnitOfTemperature.FAHRENHEIT
if control.unit == TemperatureUnit.FAHRENHEIT
else UnitOfTemperature.CELSIUS
),
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: LiebherrConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up Liebherr sensor entities."""
coordinators = entry.runtime_data
entities: list[LiebherrSensor] = []
for coordinator in coordinators.values():
# Get all temperature controls for this device
temp_controls = coordinator.data.get_temperature_controls()
for temp_control in temp_controls.values():
entities.extend(
LiebherrSensor(
coordinator=coordinator,
zone_id=temp_control.zone_id,
description=description,
)
for description in SENSOR_TYPES
)
async_add_entities(entities)
class LiebherrSensor(LiebherrZoneEntity, SensorEntity):
"""Representation of a Liebherr sensor."""
entity_description: LiebherrSensorEntityDescription
def __init__(
self,
coordinator: LiebherrCoordinator,
zone_id: int,
description: LiebherrSensorEntityDescription,
) -> None:
"""Initialize the sensor entity."""
super().__init__(coordinator, zone_id)
self.entity_description = description
self._attr_unique_id = f"{coordinator.device_id}_{description.key}_{zone_id}"
# If device has only one zone, use model name instead of zone name
temp_controls = coordinator.data.get_temperature_controls()
if len(temp_controls) == 1:
self._attr_name = None
else:
# Set translation key based on zone position for multi-zone devices
self._attr_translation_key = self._get_zone_translation_key()
@property
def native_unit_of_measurement(self) -> str | None:
"""Return the unit of measurement."""
if (temp_control := self.temperature_control) is None:
return None
return self.entity_description.unit_fn(temp_control)
@property
def native_value(self) -> StateType:
"""Return the current value."""
if (temp_control := self.temperature_control) is None:
return None
return self.entity_description.value_fn(temp_control)
@property
def available(self) -> bool:
"""Return if entity is available."""
return super().available and self.temperature_control is not None

View File

@@ -0,0 +1,38 @@
{
"config": {
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"no_devices": "No devices found for this API key"
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"no_devices": "No devices found for this API key",
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"step": {
"user": {
"data": {
"api_key": "[%key:common::config_flow::data::api_key%]"
},
"data_description": {
"api_key": "The API key from the Liebherr SmartDevice app. Note: The API key can only be copied once from the app."
},
"description": "Enter your Liebherr HomeAPI key. You can find it in the Liebherr SmartDevice app under Settings → Become a beta tester."
}
}
},
"entity": {
"sensor": {
"bottom_zone": {
"name": "Bottom zone"
},
"middle_zone": {
"name": "Middle zone"
},
"top_zone": {
"name": "Top zone"
}
}
}
}

View File

@@ -379,6 +379,7 @@ FLOWS = {
"lg_thinq",
"libre_hardware_monitor",
"lidarr",
"liebherr",
"lifx",
"linkplay",
"litejet",

View File

@@ -3604,6 +3604,12 @@
"config_flow": true,
"iot_class": "local_polling"
},
"liebherr": {
"name": "Liebherr",
"integration_type": "hub",
"config_flow": true,
"iot_class": "cloud_polling"
},
"lifx": {
"name": "LIFX",
"integration_type": "device",

View File

@@ -597,6 +597,10 @@ ZEROCONF = {
"domain": "lektrico",
"name": "lektrico*",
},
{
"domain": "liebherr",
"name": "liebherr*",
},
{
"domain": "loqed",
"name": "loqed*",

3
requirements_all.txt generated
View File

@@ -2187,6 +2187,9 @@ pylgnetcast==0.3.9
# homeassistant.components.forked_daapd
pylibrespot-java==0.1.1
# homeassistant.components.liebherr
pyliebherrhomeapi==0.2.1
# homeassistant.components.litejet
pylitejet==0.6.3

View File

@@ -1855,6 +1855,9 @@ pylgnetcast==0.3.9
# homeassistant.components.forked_daapd
pylibrespot-java==0.1.1
# homeassistant.components.liebherr
pyliebherrhomeapi==0.2.1
# homeassistant.components.litejet
pylitejet==0.6.3

View File

@@ -0,0 +1 @@
"""Tests for the liebherr integration."""

View File

@@ -0,0 +1,110 @@
"""Common fixtures for the liebherr tests."""
from collections.abc import Generator
from unittest.mock import AsyncMock, MagicMock, patch
from pyliebherrhomeapi import (
Device,
DeviceState,
DeviceType,
TemperatureControl,
TemperatureUnit,
ZonePosition,
)
import pytest
from homeassistant.components.liebherr.const import DOMAIN
from homeassistant.const import CONF_API_KEY, Platform
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
# Complete multi-zone device for comprehensive testing
MOCK_DEVICE = Device(
device_id="test_device_id",
nickname="Test Fridge",
device_type=DeviceType.COMBI,
device_name="CBNes1234",
)
MOCK_DEVICE_STATE = DeviceState(
device=MOCK_DEVICE,
controls=[
TemperatureControl(
zone_id=1,
zone_position=ZonePosition.TOP,
name="Fridge",
type="fridge",
value=5,
unit=TemperatureUnit.CELSIUS,
),
TemperatureControl(
zone_id=2,
zone_position=ZonePosition.BOTTOM,
name="Freezer",
type="freezer",
value=-18,
unit=TemperatureUnit.CELSIUS,
),
],
)
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.liebherr.async_setup_entry", return_value=True
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Return a mock config entry."""
return MockConfigEntry(
domain=DOMAIN,
data={CONF_API_KEY: "test-api-key"},
title="Liebherr",
)
@pytest.fixture
def mock_liebherr_client() -> Generator[MagicMock]:
"""Return a mocked Liebherr client."""
with (
patch(
"homeassistant.components.liebherr.LiebherrClient",
autospec=True,
) as mock_client,
patch(
"homeassistant.components.liebherr.config_flow.LiebherrClient",
new=mock_client,
),
):
client = mock_client.return_value
client.get_devices.return_value = [MOCK_DEVICE]
client.get_device_state.return_value = MOCK_DEVICE_STATE
client.set_temperature = AsyncMock()
yield client
@pytest.fixture
def platforms() -> list[Platform]:
"""Fixture to specify platforms to test."""
return [Platform.SENSOR]
@pytest.fixture
async def init_integration(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_liebherr_client: MagicMock,
) -> MockConfigEntry:
"""Set up the Liebherr integration for testing."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
return mock_config_entry

View File

@@ -0,0 +1,38 @@
# serializer version: 1
# name: test_liebherr_entity_with_device_type
dict({
'identifiers': set({
tuple(
'liebherr',
'test_device_id',
),
}),
'manufacturer': 'Liebherr',
'model': 'Fridge',
'model_id': 'CBNes1234',
'name': 'Test Device',
})
# ---
# name: test_liebherr_entity_without_device_type
dict({
'identifiers': set({
tuple(
'liebherr',
'test_device_id_2',
),
}),
'manufacturer': 'Liebherr',
'model': None,
'model_id': 'CBNes5678',
'name': 'CBNes5678',
})
# ---
# name: test_liebherr_zone_entity_temperature_control
1
# ---
# name: test_liebherr_zone_entity_temperature_control.1
<ZonePosition.TOP: 'top'>
# ---
# name: test_liebherr_zone_entity_temperature_control.2
5
# ---

View File

@@ -0,0 +1,172 @@
# serializer version: 1
# name: test_sensors[sensor.test_fridge_bottom_zone-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.test_fridge_bottom_zone',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Bottom zone',
'options': dict({
'sensor': dict({
'suggested_display_precision': 0,
}),
}),
'original_device_class': <SensorDeviceClass.TEMPERATURE: 'temperature'>,
'original_icon': None,
'original_name': 'Bottom zone',
'platform': 'liebherr',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'bottom_zone',
'unique_id': 'test_device_id_temperature_2',
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
})
# ---
# name: test_sensors[sensor.test_fridge_bottom_zone-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'temperature',
'friendly_name': 'Test Fridge Bottom zone',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
}),
'context': <ANY>,
'entity_id': 'sensor.test_fridge_bottom_zone',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '-18',
})
# ---
# name: test_sensors[sensor.test_fridge_top_zone-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.test_fridge_top_zone',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Top zone',
'options': dict({
'sensor': dict({
'suggested_display_precision': 0,
}),
}),
'original_device_class': <SensorDeviceClass.TEMPERATURE: 'temperature'>,
'original_icon': None,
'original_name': 'Top zone',
'platform': 'liebherr',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'top_zone',
'unique_id': 'test_device_id_temperature_1',
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
})
# ---
# name: test_sensors[sensor.test_fridge_top_zone-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'temperature',
'friendly_name': 'Test Fridge Top zone',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
}),
'context': <ANY>,
'entity_id': 'sensor.test_fridge_top_zone',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '5',
})
# ---
# name: test_single_zone_sensor[sensor.single_zone_fridge-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.single_zone_fridge',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': None,
'options': dict({
'sensor': dict({
'suggested_display_precision': 0,
}),
}),
'original_device_class': <SensorDeviceClass.TEMPERATURE: 'temperature'>,
'original_icon': None,
'original_name': None,
'platform': 'liebherr',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'single_zone_id_temperature_1',
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
})
# ---
# name: test_single_zone_sensor[sensor.single_zone_fridge-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'temperature',
'friendly_name': 'Single Zone Fridge',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
}),
'context': <ANY>,
'entity_id': 'sensor.single_zone_fridge',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '4',
})
# ---

View File

@@ -0,0 +1,170 @@
"""Test the liebherr config flow."""
from ipaddress import ip_address
from unittest.mock import AsyncMock, MagicMock
from pyliebherrhomeapi.exceptions import (
LiebherrAuthenticationError,
LiebherrConnectionError,
)
import pytest
from homeassistant import config_entries
from homeassistant.components.liebherr.const import DOMAIN
from homeassistant.const import CONF_API_KEY
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
from tests.common import MockConfigEntry
MOCK_API_KEY = "test-api-key"
MOCK_USER_INPUT = {CONF_API_KEY: MOCK_API_KEY}
MOCK_ZEROCONF_SERVICE_INFO = ZeroconfServiceInfo(
ip_address=ip_address("192.168.1.100"),
ip_addresses=[ip_address("192.168.1.100")],
port=80,
hostname="liebherr-device.local.",
type="_http._tcp.local.",
name="liebherr-fridge._http._tcp.local.",
properties={},
)
async def test_form(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_liebherr_client: MagicMock,
) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "user"
assert result.get("errors") == {}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], MOCK_USER_INPUT
)
assert result.get("type") is FlowResultType.CREATE_ENTRY
assert result.get("title") == "Liebherr"
assert result.get("data") == MOCK_USER_INPUT
assert len(mock_setup_entry.mock_calls) == 1
@pytest.mark.parametrize(
("side_effect", "expected_error"),
[
(LiebherrAuthenticationError("Invalid"), "invalid_auth"),
(LiebherrConnectionError("Failed"), "cannot_connect"),
(Exception("Unexpected"), "unknown"),
],
)
async def test_form_errors_with_recovery(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_liebherr_client: MagicMock,
side_effect: Exception,
expected_error: str,
) -> None:
"""Test error handling with successful recovery."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result.get("type") is FlowResultType.FORM
assert result.get("errors") == {}
# Trigger error
mock_liebherr_client.get_devices.side_effect = side_effect
result = await hass.config_entries.flow.async_configure(
result["flow_id"], MOCK_USER_INPUT
)
assert result.get("type") is FlowResultType.FORM
assert result.get("errors") == {"base": expected_error}
# Recover and complete successfully
mock_liebherr_client.get_devices.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"], MOCK_USER_INPUT
)
assert result.get("type") is FlowResultType.CREATE_ENTRY
assert result.get("title") == "Liebherr"
assert result.get("data") == MOCK_USER_INPUT
async def test_form_no_devices(
hass: HomeAssistant,
mock_liebherr_client: MagicMock,
) -> None:
"""Test we handle no devices found."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result.get("type") is FlowResultType.FORM
mock_liebherr_client.get_devices.return_value = []
result = await hass.config_entries.flow.async_configure(
result["flow_id"], MOCK_USER_INPUT
)
assert result.get("type") is FlowResultType.ABORT
assert result.get("reason") == "no_devices"
async def test_form_already_configured(
hass: HomeAssistant,
mock_liebherr_client: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test we abort if already configured."""
mock_config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result.get("type") is FlowResultType.FORM
result = await hass.config_entries.flow.async_configure(
result["flow_id"], MOCK_USER_INPUT
)
assert result.get("type") is FlowResultType.ABORT
assert result.get("reason") == "already_configured"
async def test_zeroconf_discovery(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_liebherr_client: MagicMock,
) -> None:
"""Test zeroconf discovery triggers the config flow."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF},
data=MOCK_ZEROCONF_SERVICE_INFO,
)
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "user"
result = await hass.config_entries.flow.async_configure(
result["flow_id"], MOCK_USER_INPUT
)
assert result.get("type") is FlowResultType.CREATE_ENTRY
assert result.get("title") == "Liebherr"
assert result.get("data") == MOCK_USER_INPUT
async def test_zeroconf_already_configured(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test zeroconf discovery aborts if already configured."""
mock_config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF},
data=MOCK_ZEROCONF_SERVICE_INFO,
)
assert result.get("type") is FlowResultType.ABORT
assert result.get("reason") == "already_configured"

View File

@@ -0,0 +1,87 @@
"""Test the liebherr integration init."""
from typing import Any
from unittest.mock import MagicMock
from pyliebherrhomeapi.exceptions import (
LiebherrAuthenticationError,
LiebherrConnectionError,
)
import pytest
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from .conftest import MOCK_DEVICE
from tests.common import MockConfigEntry
# Test errors during initial get_devices() call in async_setup_entry
@pytest.mark.parametrize(
("side_effect", "expected_state"),
[
(LiebherrAuthenticationError("Invalid API key"), ConfigEntryState.SETUP_ERROR),
(LiebherrConnectionError("Connection failed"), ConfigEntryState.SETUP_RETRY),
],
ids=["auth_failed", "connection_error"],
)
async def test_setup_entry_errors(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_liebherr_client: MagicMock,
side_effect: Any,
expected_state: ConfigEntryState,
) -> None:
"""Test setup handles various error conditions."""
mock_config_entry.add_to_hass(hass)
mock_liebherr_client.get_devices.side_effect = side_effect
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is expected_state
# Test errors during get_device() call in coordinator setup (after successful get_devices)
@pytest.mark.parametrize(
("side_effect", "expected_state"),
[
(LiebherrAuthenticationError("Invalid API key"), ConfigEntryState.SETUP_ERROR),
(LiebherrConnectionError("Connection failed"), ConfigEntryState.SETUP_RETRY),
],
ids=["auth_failed", "connection_error"],
)
async def test_coordinator_setup_errors(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_liebherr_client: MagicMock,
side_effect: Exception,
expected_state: ConfigEntryState,
) -> None:
"""Test coordinator setup handles device access errors."""
mock_config_entry.add_to_hass(hass)
mock_liebherr_client.get_devices.return_value = [MOCK_DEVICE]
mock_liebherr_client.get_device.side_effect = side_effect
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is expected_state
async def test_unload_entry(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_liebherr_client: MagicMock,
) -> None:
"""Test successful unload of entry."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED
await hass.config_entries.async_unload(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED

View File

@@ -0,0 +1,216 @@
"""Test the Liebherr sensor platform."""
from datetime import timedelta
from unittest.mock import MagicMock
from freezegun.api import FrozenDateTimeFactory
from pyliebherrhomeapi import (
Device,
DeviceState,
DeviceType,
TemperatureControl,
TemperatureUnit,
ZonePosition,
)
from pyliebherrhomeapi.exceptions import (
LiebherrAuthenticationError,
LiebherrConnectionError,
LiebherrTimeoutError,
)
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from .conftest import MOCK_DEVICE
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
@pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration")
async def test_sensors(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test all sensor entities with multi-zone device."""
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_single_zone_sensor(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
mock_liebherr_client: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test single zone device uses device name without zone suffix."""
device = Device(
device_id="single_zone_id",
nickname="Single Zone Fridge",
device_type=DeviceType.FRIDGE,
device_name="K2601",
)
mock_liebherr_client.get_devices.return_value = [device]
mock_liebherr_client.get_device_state.return_value = DeviceState(
device=device,
controls=[
TemperatureControl(
zone_id=1,
zone_position=ZonePosition.TOP,
name="Fridge",
type="fridge",
value=4,
unit=TemperatureUnit.CELSIUS,
)
],
)
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_multi_zone_with_none_position(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_liebherr_client: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test multi-zone device with None zone_position falls back to no translation key."""
device = Device(
device_id="multi_zone_none",
nickname="Multi Zone Fridge",
device_type=DeviceType.COMBI,
device_name="CBNes9999",
)
mock_liebherr_client.get_devices.return_value = [device]
mock_liebherr_client.get_device_state.return_value = DeviceState(
device=device,
controls=[
TemperatureControl(
zone_id=1,
zone_position=None, # None triggers fallback in _get_zone_translation_key
name="Fridge",
type="fridge",
value=5,
unit=TemperatureUnit.CELSIUS,
),
TemperatureControl(
zone_id=2,
zone_position=ZonePosition.BOTTOM,
name="Freezer",
type="freezer",
value=-18,
unit=TemperatureUnit.CELSIUS,
),
],
)
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
# Zone with None position should have no translation key (fallback)
zone1_entity = entity_registry.async_get("sensor.multi_zone_fridge_temperature")
assert zone1_entity is not None
assert zone1_entity.translation_key is None
# Zone with valid position should have translation key
zone2_entity = entity_registry.async_get("sensor.multi_zone_fridge_bottom_zone")
assert zone2_entity is not None
assert zone2_entity.translation_key == "bottom_zone"
@pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration")
@pytest.mark.parametrize(
"exception",
[
LiebherrConnectionError("Connection failed"),
LiebherrTimeoutError("Timeout"),
LiebherrAuthenticationError("API key revoked"),
],
ids=["connection_error", "timeout_error", "auth_error"],
)
async def test_sensor_update_failure(
hass: HomeAssistant,
mock_liebherr_client: MagicMock,
freezer: FrozenDateTimeFactory,
exception: Exception,
) -> None:
"""Test sensor becomes unavailable when coordinator update fails."""
entity_id = "sensor.test_fridge_top_zone"
# Initial state should be available with value
state = hass.states.get(entity_id)
assert state is not None
assert state.state == "5"
# Simulate update error
mock_liebherr_client.get_device_state.side_effect = exception
# Advance time to trigger coordinator refresh (60 second interval)
freezer.tick(timedelta(seconds=61))
async_fire_time_changed(hass)
await hass.async_block_till_done()
# Sensor should now be unavailable
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_UNAVAILABLE
# Simulate recovery
mock_liebherr_client.get_device_state.side_effect = None
freezer.tick(timedelta(seconds=61))
async_fire_time_changed(hass)
await hass.async_block_till_done()
# Sensor should recover
state = hass.states.get(entity_id)
assert state is not None
assert state.state == "5"
@pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration")
async def test_sensor_unavailable_when_control_missing(
hass: HomeAssistant,
mock_liebherr_client: MagicMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test sensor becomes unavailable when temperature control is removed from device."""
entity_id = "sensor.test_fridge_top_zone"
# Initial state should be available
state = hass.states.get(entity_id)
assert state is not None
assert state.state == "5"
# Device stops reporting controls (e.g., zone removed or API issue)
mock_liebherr_client.get_device_state.return_value = DeviceState(
device=MOCK_DEVICE, controls=[]
)
# Advance time to trigger coordinator refresh
freezer.tick(timedelta(seconds=61))
async_fire_time_changed(hass)
await hass.async_block_till_done()
# Sensor should now be unavailable
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_UNAVAILABLE
# Verify entity properties return None when control is missing
entity = hass.data["entity_components"]["sensor"].get_entity(entity_id)
assert entity is not None
assert entity.native_value is None
assert entity.native_unit_of_measurement is None