mirror of
https://github.com/home-assistant/core.git
synced 2026-04-20 16:39:02 +02:00
Add numbers platform to Indevolt integration (#163298)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
@@ -7,7 +7,7 @@ from homeassistant.core import HomeAssistant
|
||||
|
||||
from .coordinator import IndevoltConfigEntry, IndevoltCoordinator
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
||||
PLATFORMS: list[Platform] = [Platform.NUMBER, Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: IndevoltConfigEntry) -> bool:
|
||||
|
||||
@@ -99,5 +99,6 @@ SENSOR_KEYS = {
|
||||
"11011",
|
||||
"11009",
|
||||
"11010",
|
||||
"6105",
|
||||
],
|
||||
}
|
||||
|
||||
@@ -6,12 +6,13 @@ from datetime import timedelta
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from aiohttp import ClientError
|
||||
from indevolt_api import IndevoltAPI, TimeOutException
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST, CONF_MODEL
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
@@ -78,3 +79,12 @@ class IndevoltCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||
return await self.api.fetch_data(sensor_keys)
|
||||
except TimeOutException as err:
|
||||
raise UpdateFailed(f"Device update timed out: {err}") from err
|
||||
|
||||
async def async_push_data(self, sensor_key: str, value: Any) -> bool:
|
||||
"""Push/write data values to given key on the device."""
|
||||
try:
|
||||
return await self.api.set_data(sensor_key, value)
|
||||
except TimeOutException as err:
|
||||
raise HomeAssistantError(f"Device push timed out: {err}") from err
|
||||
except (ClientError, ConnectionError, OSError) as err:
|
||||
raise HomeAssistantError(f"Device push failed: {err}") from err
|
||||
|
||||
142
homeassistant/components/indevolt/number.py
Normal file
142
homeassistant/components/indevolt/number.py
Normal file
@@ -0,0 +1,142 @@
|
||||
"""Number platform for Indevolt integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Final
|
||||
|
||||
from homeassistant.components.number import (
|
||||
NumberDeviceClass,
|
||||
NumberEntity,
|
||||
NumberEntityDescription,
|
||||
NumberMode,
|
||||
)
|
||||
from homeassistant.const import PERCENTAGE, UnitOfPower
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from . import IndevoltConfigEntry
|
||||
from .coordinator import IndevoltCoordinator
|
||||
from .entity import IndevoltEntity
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class IndevoltNumberEntityDescription(NumberEntityDescription):
|
||||
"""Custom entity description class for Indevolt number entities."""
|
||||
|
||||
generation: list[int] = field(default_factory=lambda: [1, 2])
|
||||
read_key: str
|
||||
write_key: str
|
||||
|
||||
|
||||
NUMBERS: Final = (
|
||||
IndevoltNumberEntityDescription(
|
||||
key="discharge_limit",
|
||||
generation=[2],
|
||||
translation_key="discharge_limit",
|
||||
read_key="6105",
|
||||
write_key="1142",
|
||||
native_min_value=0,
|
||||
native_max_value=100,
|
||||
native_step=1,
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
device_class=NumberDeviceClass.BATTERY,
|
||||
),
|
||||
IndevoltNumberEntityDescription(
|
||||
key="max_ac_output_power",
|
||||
generation=[2],
|
||||
translation_key="max_ac_output_power",
|
||||
read_key="11011",
|
||||
write_key="1147",
|
||||
native_min_value=0,
|
||||
native_max_value=2400,
|
||||
native_step=100,
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
device_class=NumberDeviceClass.POWER,
|
||||
),
|
||||
IndevoltNumberEntityDescription(
|
||||
key="inverter_input_limit",
|
||||
generation=[2],
|
||||
translation_key="inverter_input_limit",
|
||||
read_key="11009",
|
||||
write_key="1138",
|
||||
native_min_value=100,
|
||||
native_max_value=2400,
|
||||
native_step=100,
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
device_class=NumberDeviceClass.POWER,
|
||||
),
|
||||
IndevoltNumberEntityDescription(
|
||||
key="feedin_power_limit",
|
||||
generation=[2],
|
||||
translation_key="feedin_power_limit",
|
||||
read_key="11010",
|
||||
write_key="1146",
|
||||
native_min_value=0,
|
||||
native_max_value=2400,
|
||||
native_step=100,
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
device_class=NumberDeviceClass.POWER,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: IndevoltConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the number platform for Indevolt."""
|
||||
coordinator = entry.runtime_data
|
||||
device_gen = coordinator.generation
|
||||
|
||||
# Number initialization
|
||||
async_add_entities(
|
||||
IndevoltNumberEntity(coordinator, description)
|
||||
for description in NUMBERS
|
||||
if device_gen in description.generation
|
||||
)
|
||||
|
||||
|
||||
class IndevoltNumberEntity(IndevoltEntity, NumberEntity):
|
||||
"""Represents a number entity for Indevolt devices."""
|
||||
|
||||
entity_description: IndevoltNumberEntityDescription
|
||||
_attr_mode = NumberMode.BOX
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: IndevoltCoordinator,
|
||||
description: IndevoltNumberEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the Indevolt number entity."""
|
||||
super().__init__(coordinator)
|
||||
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{self.serial_number}_{description.key}"
|
||||
|
||||
@property
|
||||
def native_value(self) -> int | None:
|
||||
"""Return the current value of the entity."""
|
||||
raw_value = self.coordinator.data.get(self.entity_description.read_key)
|
||||
if raw_value is None:
|
||||
return None
|
||||
|
||||
return int(raw_value)
|
||||
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Set a new value for the entity."""
|
||||
|
||||
int_value = int(value)
|
||||
success = await self.coordinator.async_push_data(
|
||||
self.entity_description.write_key, int_value
|
||||
)
|
||||
|
||||
if success:
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
else:
|
||||
raise HomeAssistantError(f"Failed to set value {int_value} for {self.name}")
|
||||
@@ -23,6 +23,20 @@
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"number": {
|
||||
"discharge_limit": {
|
||||
"name": "Discharge limit"
|
||||
},
|
||||
"feedin_power_limit": {
|
||||
"name": "Feed-in power limit"
|
||||
},
|
||||
"inverter_input_limit": {
|
||||
"name": "Inverter input limit"
|
||||
},
|
||||
"max_ac_output_power": {
|
||||
"name": "Max AC output power"
|
||||
}
|
||||
},
|
||||
"sensor": {
|
||||
"ac_input_power": {
|
||||
"name": "AC input power"
|
||||
|
||||
@@ -86,6 +86,7 @@ def mock_indevolt(generation: int) -> Generator[AsyncMock]:
|
||||
# Mock coordinator API (get_data)
|
||||
client = mock_client.return_value
|
||||
client.fetch_data.return_value = fixture_data
|
||||
client.set_data.return_value = True
|
||||
client.get_config.return_value = {
|
||||
"device": {
|
||||
"sn": device_info["sn"],
|
||||
|
||||
241
tests/components/indevolt/snapshots/test_number.ambr
Normal file
241
tests/components/indevolt/snapshots/test_number.ambr
Normal file
@@ -0,0 +1,241 @@
|
||||
# serializer version: 1
|
||||
# name: test_number[2][number.cms_sf2000_discharge_limit-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'max': 100,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.BOX: 'box'>,
|
||||
'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.cms_sf2000_discharge_limit',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Discharge limit',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <NumberDeviceClass.BATTERY: 'battery'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Discharge limit',
|
||||
'platform': 'indevolt',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'discharge_limit',
|
||||
'unique_id': 'SolidFlex2000-87654321_discharge_limit',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[2][number.cms_sf2000_discharge_limit-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'battery',
|
||||
'friendly_name': 'CMS-SF2000 Discharge limit',
|
||||
'max': 100,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.BOX: 'box'>,
|
||||
'step': 1,
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'number.cms_sf2000_discharge_limit',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '5',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[2][number.cms_sf2000_feed_in_power_limit-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'max': 2400,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.BOX: 'box'>,
|
||||
'step': 100,
|
||||
}),
|
||||
'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.cms_sf2000_feed_in_power_limit',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Feed-in power limit',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <NumberDeviceClass.POWER: 'power'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Feed-in power limit',
|
||||
'platform': 'indevolt',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'feedin_power_limit',
|
||||
'unique_id': 'SolidFlex2000-87654321_feedin_power_limit',
|
||||
'unit_of_measurement': <UnitOfPower.WATT: 'W'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_number[2][number.cms_sf2000_feed_in_power_limit-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'power',
|
||||
'friendly_name': 'CMS-SF2000 Feed-in power limit',
|
||||
'max': 2400,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.BOX: 'box'>,
|
||||
'step': 100,
|
||||
'unit_of_measurement': <UnitOfPower.WATT: 'W'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'number.cms_sf2000_feed_in_power_limit',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '52',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[2][number.cms_sf2000_inverter_input_limit-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'max': 2400,
|
||||
'min': 100,
|
||||
'mode': <NumberMode.BOX: 'box'>,
|
||||
'step': 100,
|
||||
}),
|
||||
'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.cms_sf2000_inverter_input_limit',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Inverter input limit',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <NumberDeviceClass.POWER: 'power'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Inverter input limit',
|
||||
'platform': 'indevolt',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'inverter_input_limit',
|
||||
'unique_id': 'SolidFlex2000-87654321_inverter_input_limit',
|
||||
'unit_of_measurement': <UnitOfPower.WATT: 'W'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_number[2][number.cms_sf2000_inverter_input_limit-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'power',
|
||||
'friendly_name': 'CMS-SF2000 Inverter input limit',
|
||||
'max': 2400,
|
||||
'min': 100,
|
||||
'mode': <NumberMode.BOX: 'box'>,
|
||||
'step': 100,
|
||||
'unit_of_measurement': <UnitOfPower.WATT: 'W'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'number.cms_sf2000_inverter_input_limit',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '50',
|
||||
})
|
||||
# ---
|
||||
# name: test_number[2][number.cms_sf2000_max_ac_output_power-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'max': 2400,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.BOX: 'box'>,
|
||||
'step': 100,
|
||||
}),
|
||||
'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.cms_sf2000_max_ac_output_power',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Max AC output power',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <NumberDeviceClass.POWER: 'power'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Max AC output power',
|
||||
'platform': 'indevolt',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'max_ac_output_power',
|
||||
'unique_id': 'SolidFlex2000-87654321_max_ac_output_power',
|
||||
'unit_of_measurement': <UnitOfPower.WATT: 'W'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_number[2][number.cms_sf2000_max_ac_output_power-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'power',
|
||||
'friendly_name': 'CMS-SF2000 Max AC output power',
|
||||
'max': 2400,
|
||||
'min': 0,
|
||||
'mode': <NumberMode.BOX: 'box'>,
|
||||
'step': 100,
|
||||
'unit_of_measurement': <UnitOfPower.WATT: 'W'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'number.cms_sf2000_max_ac_output_power',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '85',
|
||||
})
|
||||
# ---
|
||||
167
tests/components/indevolt/test_number.py
Normal file
167
tests/components/indevolt/test_number.py
Normal file
@@ -0,0 +1,167 @@
|
||||
"""Tests for the Indevolt number platform."""
|
||||
|
||||
from datetime import timedelta
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.indevolt.coordinator import SCAN_INTERVAL
|
||||
from homeassistant.components.number import SERVICE_SET_VALUE
|
||||
from homeassistant.const import STATE_UNAVAILABLE, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
|
||||
|
||||
KEY_READ_DISCHARGE_LIMIT = "6105"
|
||||
KEY_WRITE_DISCHARGE_LIMIT = "1142"
|
||||
|
||||
KEY_READ_MAX_AC_OUTPUT_POWER = "11011"
|
||||
KEY_WRITE_MAX_AC_OUTPUT_POWER = "1147"
|
||||
|
||||
KEY_READ_INVERTER_INPUT_LIMIT = "11009"
|
||||
KEY_WRITE_INVERTER_INPUT_LIMIT = "1138"
|
||||
|
||||
KEY_READ_FEEDIN_POWER_LIMIT = "11010"
|
||||
KEY_WRITE_FEEDIN_POWER_LIMIT = "1146"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
@pytest.mark.parametrize("generation", [2], indirect=True)
|
||||
async def test_number(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_indevolt: AsyncMock,
|
||||
snapshot: SnapshotAssertion,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test number entity registration and values."""
|
||||
with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.NUMBER]):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("generation", [2], indirect=True)
|
||||
@pytest.mark.parametrize(
|
||||
("entity_id", "read_key", "write_key", "test_value"),
|
||||
[
|
||||
(
|
||||
"number.cms_sf2000_discharge_limit",
|
||||
KEY_READ_DISCHARGE_LIMIT,
|
||||
KEY_WRITE_DISCHARGE_LIMIT,
|
||||
50,
|
||||
),
|
||||
(
|
||||
"number.cms_sf2000_max_ac_output_power",
|
||||
KEY_READ_MAX_AC_OUTPUT_POWER,
|
||||
KEY_WRITE_MAX_AC_OUTPUT_POWER,
|
||||
1500,
|
||||
),
|
||||
(
|
||||
"number.cms_sf2000_inverter_input_limit",
|
||||
KEY_READ_INVERTER_INPUT_LIMIT,
|
||||
KEY_WRITE_INVERTER_INPUT_LIMIT,
|
||||
800,
|
||||
),
|
||||
(
|
||||
"number.cms_sf2000_feed_in_power_limit",
|
||||
KEY_READ_FEEDIN_POWER_LIMIT,
|
||||
KEY_WRITE_FEEDIN_POWER_LIMIT,
|
||||
1200,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_number_set_values(
|
||||
hass: HomeAssistant,
|
||||
mock_indevolt: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_id: str,
|
||||
read_key: str,
|
||||
write_key: str,
|
||||
test_value: int,
|
||||
) -> None:
|
||||
"""Test setting number values for all configurable parameters."""
|
||||
with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.NUMBER]):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
# Reset mock call count for this iteration
|
||||
mock_indevolt.set_data.reset_mock()
|
||||
|
||||
# Update mock data to reflect the new value
|
||||
mock_indevolt.fetch_data.return_value[read_key] = test_value
|
||||
|
||||
# Call the service to set the value
|
||||
await hass.services.async_call(
|
||||
Platform.NUMBER,
|
||||
SERVICE_SET_VALUE,
|
||||
{"entity_id": entity_id, "value": test_value},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
# Verify set_data was called with correct parameters
|
||||
mock_indevolt.set_data.assert_called_with(write_key, test_value)
|
||||
|
||||
# Verify updated state
|
||||
assert (state := hass.states.get(entity_id)) is not None
|
||||
assert int(float(state.state)) == test_value
|
||||
|
||||
|
||||
@pytest.mark.parametrize("generation", [2], indirect=True)
|
||||
async def test_number_set_value_error(
|
||||
hass: HomeAssistant,
|
||||
mock_indevolt: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test error handling when setting number values."""
|
||||
with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.NUMBER]):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
# Mock set_data to raise an error
|
||||
mock_indevolt.set_data.side_effect = HomeAssistantError(
|
||||
"Device communication failed"
|
||||
)
|
||||
|
||||
# Attempt to set value
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await hass.services.async_call(
|
||||
Platform.NUMBER,
|
||||
SERVICE_SET_VALUE,
|
||||
{
|
||||
"entity_id": "number.cms_sf2000_discharge_limit",
|
||||
"value": 50,
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
# Verify set_data was called before failing
|
||||
mock_indevolt.set_data.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("generation", [2], indirect=True)
|
||||
async def test_number_availability(
|
||||
hass: HomeAssistant,
|
||||
mock_indevolt: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test number entity availability / non-availability."""
|
||||
with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.NUMBER]):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert (state := hass.states.get("number.cms_sf2000_discharge_limit"))
|
||||
assert int(float(state.state)) == 5
|
||||
|
||||
# Simulate fetch_data error
|
||||
mock_indevolt.fetch_data.side_effect = ConnectionError
|
||||
freezer.tick(delta=timedelta(seconds=SCAN_INTERVAL))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (state := hass.states.get("number.cms_sf2000_discharge_limit"))
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
Reference in New Issue
Block a user