-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Palazzetti power control #131833
Open
dotvav
wants to merge
7
commits into
home-assistant:dev
Choose a base branch
from
dotvav:palazzetti_power_control
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+206
−1
Open
Palazzetti power control #131833
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
042d72d
Add number entity
dotvav ebb4064
Catch exceptions
dotvav 5ebb7f1
Add test coverage
dotvav fb5f741
Add translation
dotvav 0f4ac40
Fix exception message
dotvav d348aeb
Simplify number.py
dotvav 33e096e
Remove dead code
dotvav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""Number platform for Palazzetti settings.""" | ||
|
||
from __future__ import annotations | ||
|
||
from pypalazzetti.exceptions import CommunicationError, ValidationError | ||
|
||
from homeassistant.components.number import NumberDeviceClass, NumberEntity | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from . import PalazzettiConfigEntry | ||
from .const import DOMAIN | ||
from .coordinator import PalazzettiDataUpdateCoordinator | ||
from .entity import PalazzettiEntity | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: PalazzettiConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up Palazzetti number platform.""" | ||
async_add_entities([PalazzettiCombustionPowerEntity(config_entry.runtime_data)]) | ||
|
||
|
||
class PalazzettiCombustionPowerEntity(PalazzettiEntity, NumberEntity): | ||
"""Representation of Palazzetti number entity for Combustion power.""" | ||
|
||
_attr_translation_key = "combustion_power" | ||
_attr_device_class = NumberDeviceClass.POWER_FACTOR | ||
_attr_native_min_value = 1 | ||
_attr_native_max_value = 5 | ||
_attr_native_step = 1 | ||
|
||
def __init__( | ||
self, | ||
coordinator: PalazzettiDataUpdateCoordinator, | ||
) -> None: | ||
"""Initialize the Palazzetti number entity.""" | ||
super().__init__(coordinator) | ||
self._attr_unique_id = f"{coordinator.config_entry.unique_id}-combustion_power" | ||
|
||
@property | ||
def native_value(self) -> float: | ||
"""Return the state of the setting entity.""" | ||
return self.coordinator.client.power_mode | ||
|
||
async def async_set_native_value(self, value: float) -> None: | ||
"""Update the setting.""" | ||
try: | ||
await self.coordinator.client.set_power_mode(int(value)) | ||
except CommunicationError as err: | ||
raise HomeAssistantError( | ||
translation_domain=DOMAIN, translation_key="cannot_connect" | ||
) from err | ||
except ValidationError as err: | ||
raise ServiceValidationError( | ||
translation_domain=DOMAIN, | ||
translation_key="invalid_combustion_power", | ||
translation_placeholders={ | ||
"value": str(value), | ||
}, | ||
) from err | ||
|
||
await self.coordinator.async_request_refresh() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# serializer version: 1 | ||
# name: test_all_entities[number.stove_combustion_power-entry] | ||
EntityRegistryEntrySnapshot({ | ||
'aliases': set({ | ||
}), | ||
'area_id': None, | ||
'capabilities': dict({ | ||
'max': 5, | ||
'min': 1, | ||
'mode': <NumberMode.AUTO: 'auto'>, | ||
'step': 1, | ||
}), | ||
'config_entry_id': <ANY>, | ||
'device_class': None, | ||
'device_id': <ANY>, | ||
'disabled_by': None, | ||
'domain': 'number', | ||
'entity_category': None, | ||
'entity_id': 'number.stove_combustion_power', | ||
'has_entity_name': True, | ||
'hidden_by': None, | ||
'icon': None, | ||
'id': <ANY>, | ||
'labels': set({ | ||
}), | ||
'name': None, | ||
'options': dict({ | ||
}), | ||
'original_device_class': <NumberDeviceClass.POWER_FACTOR: 'power_factor'>, | ||
'original_icon': None, | ||
'original_name': 'Combustion power', | ||
'platform': 'palazzetti', | ||
'previous_unique_id': None, | ||
'supported_features': 0, | ||
'translation_key': 'combustion_power', | ||
'unique_id': '11:22:33:44:55:66-combustion_power', | ||
'unit_of_measurement': None, | ||
}) | ||
# --- | ||
# name: test_all_entities[number.stove_combustion_power-state] | ||
StateSnapshot({ | ||
'attributes': ReadOnlyDict({ | ||
'device_class': 'power_factor', | ||
'friendly_name': 'Stove Combustion power', | ||
'max': 5, | ||
'min': 1, | ||
'mode': <NumberMode.AUTO: 'auto'>, | ||
'step': 1, | ||
}), | ||
'context': <ANY>, | ||
'entity_id': 'number.stove_combustion_power', | ||
'last_changed': <ANY>, | ||
'last_reported': <ANY>, | ||
'last_updated': <ANY>, | ||
'state': '3', | ||
}) | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"""Tests for the Palazzetti sensor platform.""" | ||
|
||
from unittest.mock import AsyncMock, patch | ||
|
||
from pypalazzetti.exceptions import CommunicationError, ValidationError | ||
import pytest | ||
from syrupy import SnapshotAssertion | ||
|
||
from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN, SERVICE_SET_VALUE | ||
from homeassistant.const import ATTR_ENTITY_ID, Platform | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError | ||
from homeassistant.helpers import entity_registry as er | ||
|
||
from . import setup_integration | ||
|
||
from tests.common import MockConfigEntry, snapshot_platform | ||
|
||
ENTITY_ID = "number.stove_combustion_power" | ||
|
||
|
||
async def test_all_entities( | ||
hass: HomeAssistant, | ||
snapshot: SnapshotAssertion, | ||
mock_palazzetti_client: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
entity_registry: er.EntityRegistry, | ||
) -> None: | ||
"""Test all entities.""" | ||
with patch("homeassistant.components.palazzetti.PLATFORMS", [Platform.NUMBER]): | ||
await setup_integration(hass, mock_config_entry) | ||
|
||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) | ||
|
||
|
||
async def test_async_set_data( | ||
hass: HomeAssistant, | ||
mock_palazzetti_client: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
) -> None: | ||
"""Test setting number data via service call.""" | ||
await setup_integration(hass, mock_config_entry) | ||
|
||
# Set value: Success | ||
await hass.services.async_call( | ||
NUMBER_DOMAIN, | ||
SERVICE_SET_VALUE, | ||
{ATTR_ENTITY_ID: ENTITY_ID, "value": 1}, | ||
blocking=True, | ||
) | ||
mock_palazzetti_client.set_power_mode.assert_called_once_with(1) | ||
mock_palazzetti_client.set_on.reset_mock() | ||
|
||
# Set value: Error | ||
mock_palazzetti_client.set_power_mode.side_effect = CommunicationError() | ||
with pytest.raises(HomeAssistantError): | ||
await hass.services.async_call( | ||
NUMBER_DOMAIN, | ||
SERVICE_SET_VALUE, | ||
{ATTR_ENTITY_ID: ENTITY_ID, "value": 1}, | ||
blocking=True, | ||
) | ||
mock_palazzetti_client.set_on.reset_mock() | ||
|
||
mock_palazzetti_client.set_power_mode.side_effect = ValidationError() | ||
with pytest.raises(ServiceValidationError): | ||
await hass.services.async_call( | ||
NUMBER_DOMAIN, | ||
SERVICE_SET_VALUE, | ||
{ATTR_ENTITY_ID: ENTITY_ID, "value": 1}, | ||
blocking=True, | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe verify that the value did indeed change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that I can do that. The client API is fully mocked and does not update its state.