Skip to content

Commit

Permalink
Update number platform values before add in APSystems and add tests (#…
Browse files Browse the repository at this point in the history
…131938)

Co-authored-by: epenet <[email protected]>
  • Loading branch information
Thomas55555 and epenet authored Dec 18, 2024
1 parent 352e948 commit 51bead3
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion homeassistant/components/apsystems/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async def async_setup_entry(
) -> None:
"""Set up the sensor platform."""

add_entities([ApSystemsMaxOutputNumber(config_entry.runtime_data)])
add_entities([ApSystemsMaxOutputNumber(config_entry.runtime_data)], True)


class ApSystemsMaxOutputNumber(ApSystemsEntity, NumberEntity):
Expand Down
1 change: 1 addition & 0 deletions tests/components/apsystems/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def mock_apsystems() -> Generator[MagicMock]:
operating=False,
)
mock_api.get_device_power_status.return_value = True
mock_api.get_max_power.return_value = 666
yield mock_api


Expand Down
58 changes: 58 additions & 0 deletions tests/components/apsystems/snapshots/test_number.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# serializer version: 1
# name: test_all_entities[number.mock_title_max_output-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'max': 1000,
'min': 0,
'mode': <NumberMode.BOX: 'box'>,
'step': 1,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'number',
'entity_category': None,
'entity_id': 'number.mock_title_max_output',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <NumberDeviceClass.POWER: 'power'>,
'original_icon': None,
'original_name': 'Max output',
'platform': 'apsystems',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'max_output',
'unique_id': 'MY_SERIAL_NUMBER_output_limit',
'unit_of_measurement': <UnitOfPower.WATT: 'W'>,
})
# ---
# name: test_all_entities[number.mock_title_max_output-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'power',
'friendly_name': 'Mock Title Max output',
'max': 1000,
'min': 0,
'mode': <NumberMode.BOX: 'box'>,
'step': 1,
'unit_of_measurement': <UnitOfPower.WATT: 'W'>,
}),
'context': <ANY>,
'entity_id': 'number.mock_title_max_output',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '666',
})
# ---
61 changes: 61 additions & 0 deletions tests/components/apsystems/test_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Test the APSystem number module."""

import datetime
from unittest.mock import AsyncMock, patch

from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy import SnapshotAssertion

from homeassistant.components.number import (
ATTR_VALUE,
DOMAIN as NUMBER_DOMAIN,
SERVICE_SET_VALUE,
)
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er

from . import setup_integration

from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform

SCAN_INTERVAL = datetime.timedelta(seconds=30)


async def test_number(
hass: HomeAssistant,
mock_apsystems: AsyncMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test number command."""
await setup_integration(hass, mock_config_entry)
entity_id = "number.mock_title_max_output"
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
service_data={ATTR_VALUE: 50.1},
target={ATTR_ENTITY_ID: entity_id},
blocking=True,
)
mock_apsystems.set_max_power.assert_called_once_with(50)
mock_apsystems.get_max_power.return_value = 50
freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.state == "50"


@pytest.mark.usefixtures("mock_apsystems")
@patch("homeassistant.components.apsystems.PLATFORMS", [Platform.NUMBER])
async def test_all_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test all entities."""
await setup_integration(hass, mock_config_entry)
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)

0 comments on commit 51bead3

Please sign in to comment.