-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
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
Add attachment and connection status for IOmeter #140998
Merged
zweckj
merged 3 commits into
home-assistant:dev
from
MaestroOnICe:iometer_binary_sensor
Mar 25, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,87 @@ | ||
"""IOmeter binary sensor.""" | ||
|
||
from collections.abc import Callable | ||
from dataclasses import dataclass | ||
|
||
from homeassistant.components.binary_sensor import ( | ||
BinarySensorDeviceClass, | ||
BinarySensorEntity, | ||
BinarySensorEntityDescription, | ||
) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback | ||
|
||
from .coordinator import IOMeterCoordinator, IOmeterData | ||
from .entity import IOmeterEntity | ||
|
||
|
||
@dataclass(frozen=True, kw_only=True) | ||
class IOmeterBinarySensorDescription(BinarySensorEntityDescription): | ||
"""Describes Iometer binary sensor entity.""" | ||
|
||
value_fn: Callable[[IOmeterData], bool | None] | ||
|
||
|
||
SENSOR_TYPES: list[IOmeterBinarySensorDescription] = [ | ||
IOmeterBinarySensorDescription( | ||
key="connection_status", | ||
translation_key="connection_status", | ||
device_class=BinarySensorDeviceClass.CONNECTIVITY, | ||
entity_registry_enabled_default=False, | ||
value_fn=lambda data: ( | ||
data.status.device.core.connection_status == "connected" | ||
if data.status.device.core.connection_status is not None | ||
else None | ||
), | ||
), | ||
IOmeterBinarySensorDescription( | ||
key="attachment_status", | ||
translation_key="attachment_status", | ||
device_class=BinarySensorDeviceClass.CONNECTIVITY, | ||
entity_registry_enabled_default=False, | ||
value_fn=lambda data: ( | ||
data.status.device.core.attachment_status == "attached" | ||
if data.status.device.core.attachment_status is not None | ||
else None | ||
), | ||
), | ||
] | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: ConfigEntry, | ||
async_add_entities: AddConfigEntryEntitiesCallback, | ||
) -> None: | ||
"""Set up the Sensors.""" | ||
coordinator: IOMeterCoordinator = config_entry.runtime_data | ||
|
||
async_add_entities( | ||
IOmeterBinarySensor( | ||
coordinator=coordinator, | ||
description=description, | ||
) | ||
for description in SENSOR_TYPES | ||
) | ||
|
||
|
||
class IOmeterBinarySensor(IOmeterEntity, BinarySensorEntity): | ||
"""Defines a IOmeter binary sensor.""" | ||
|
||
entity_description: IOmeterBinarySensorDescription | ||
|
||
def __init__( | ||
self, | ||
coordinator: IOMeterCoordinator, | ||
description: IOmeterBinarySensorDescription, | ||
) -> None: | ||
"""Initialize the sensor.""" | ||
super().__init__(coordinator) | ||
self.entity_description = description | ||
self._attr_unique_id = f"{coordinator.identifier}_{description.key}" | ||
|
||
@property | ||
def is_on(self) -> bool | None: | ||
"""Return the binary sensor state.""" | ||
return self.entity_description.value_fn(self.coordinator.data) |
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 |
---|---|---|
@@ -1,13 +1,19 @@ | ||
"""Tests for the IOmeter integration.""" | ||
|
||
from unittest.mock import patch | ||
|
||
from homeassistant.const import Platform | ||
from homeassistant.core import HomeAssistant | ||
|
||
from tests.common import MockConfigEntry | ||
|
||
|
||
async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: | ||
"""Fixture for setting up the component.""" | ||
async def setup_platform( | ||
hass: HomeAssistant, config_entry: MockConfigEntry, platforms: list[Platform] | ||
) -> MockConfigEntry: | ||
"""Fixture for setting up the IOmeter platform.""" | ||
config_entry.add_to_hass(hass) | ||
|
||
await hass.config_entries.async_setup(config_entry.entry_id) | ||
await hass.async_block_till_done() | ||
with patch("homeassistant.components.iometer.PLATFORMS", platforms): | ||
await hass.config_entries.async_setup(config_entry.entry_id) | ||
await hass.async_block_till_done() |
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
97 changes: 97 additions & 0 deletions
97
tests/components/iometer/snapshots/test_binary_sensor.ambr
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,97 @@ | ||
# serializer version: 1 | ||
# name: test_binary_sensors[binary_sensor.iometer_1isk0000000000_core_attachment_status-entry] | ||
EntityRegistryEntrySnapshot({ | ||
'aliases': set({ | ||
}), | ||
'area_id': None, | ||
'capabilities': None, | ||
'config_entry_id': <ANY>, | ||
'config_subentry_id': <ANY>, | ||
'device_class': None, | ||
'device_id': <ANY>, | ||
'disabled_by': None, | ||
'domain': 'binary_sensor', | ||
'entity_category': None, | ||
'entity_id': 'binary_sensor.iometer_1isk0000000000_core_attachment_status', | ||
'has_entity_name': True, | ||
'hidden_by': None, | ||
'icon': None, | ||
'id': <ANY>, | ||
'labels': set({ | ||
}), | ||
'name': None, | ||
'options': dict({ | ||
}), | ||
'original_device_class': <BinarySensorDeviceClass.CONNECTIVITY: 'connectivity'>, | ||
'original_icon': None, | ||
'original_name': 'Core attachment status', | ||
'platform': 'iometer', | ||
'previous_unique_id': None, | ||
'supported_features': 0, | ||
'translation_key': 'attachment_status', | ||
'unique_id': '01JQ6G5395176MAAWKAAPEZHV6_attachment_status', | ||
'unit_of_measurement': None, | ||
}) | ||
# --- | ||
# name: test_binary_sensors[binary_sensor.iometer_1isk0000000000_core_attachment_status-state] | ||
StateSnapshot({ | ||
'attributes': ReadOnlyDict({ | ||
'device_class': 'connectivity', | ||
'friendly_name': 'IOmeter-1ISK0000000000 Core attachment status', | ||
}), | ||
'context': <ANY>, | ||
'entity_id': 'binary_sensor.iometer_1isk0000000000_core_attachment_status', | ||
'last_changed': <ANY>, | ||
'last_reported': <ANY>, | ||
'last_updated': <ANY>, | ||
'state': 'on', | ||
}) | ||
# --- | ||
# name: test_binary_sensors[binary_sensor.iometer_1isk0000000000_core_bridge_connection_status-entry] | ||
EntityRegistryEntrySnapshot({ | ||
'aliases': set({ | ||
}), | ||
'area_id': None, | ||
'capabilities': None, | ||
'config_entry_id': <ANY>, | ||
'config_subentry_id': <ANY>, | ||
'device_class': None, | ||
'device_id': <ANY>, | ||
'disabled_by': None, | ||
'domain': 'binary_sensor', | ||
'entity_category': None, | ||
'entity_id': 'binary_sensor.iometer_1isk0000000000_core_bridge_connection_status', | ||
'has_entity_name': True, | ||
'hidden_by': None, | ||
'icon': None, | ||
'id': <ANY>, | ||
'labels': set({ | ||
}), | ||
'name': None, | ||
'options': dict({ | ||
}), | ||
'original_device_class': <BinarySensorDeviceClass.CONNECTIVITY: 'connectivity'>, | ||
'original_icon': None, | ||
'original_name': 'Core/Bridge connection status', | ||
'platform': 'iometer', | ||
'previous_unique_id': None, | ||
'supported_features': 0, | ||
'translation_key': 'connection_status', | ||
'unique_id': '01JQ6G5395176MAAWKAAPEZHV6_connection_status', | ||
'unit_of_measurement': None, | ||
}) | ||
# --- | ||
# name: test_binary_sensors[binary_sensor.iometer_1isk0000000000_core_bridge_connection_status-state] | ||
StateSnapshot({ | ||
'attributes': ReadOnlyDict({ | ||
'device_class': 'connectivity', | ||
'friendly_name': 'IOmeter-1ISK0000000000 Core/Bridge connection status', | ||
}), | ||
'context': <ANY>, | ||
'entity_id': 'binary_sensor.iometer_1isk0000000000_core_bridge_connection_status', | ||
'last_changed': <ANY>, | ||
'last_reported': <ANY>, | ||
'last_updated': <ANY>, | ||
'state': 'on', | ||
}) | ||
# --- |
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,135 @@ | ||
"""Test the IOmeter binary sensors.""" | ||
|
||
from datetime import timedelta | ||
from unittest.mock import AsyncMock | ||
|
||
from freezegun.api import FrozenDateTimeFactory | ||
import pytest | ||
from syrupy.assertion import SnapshotAssertion | ||
|
||
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNKNOWN, Platform | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers import entity_registry as er | ||
|
||
from . import setup_platform | ||
|
||
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform | ||
|
||
|
||
@pytest.mark.usefixtures("entity_registry_enabled_by_default") | ||
async def test_binary_sensors( | ||
hass: HomeAssistant, | ||
snapshot: SnapshotAssertion, | ||
mock_iometer_client: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
entity_registry: er.EntityRegistry, | ||
) -> None: | ||
"""Test binary sensors.""" | ||
await setup_platform(hass, mock_config_entry, [Platform.BINARY_SENSOR]) | ||
|
||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) | ||
|
||
|
||
@pytest.mark.usefixtures("entity_registry_enabled_by_default") | ||
async def test_connection_status_sensors( | ||
hass: HomeAssistant, | ||
mock_config_entry: MockConfigEntry, | ||
mock_iometer_client: AsyncMock, | ||
freezer: FrozenDateTimeFactory, | ||
) -> None: | ||
"""Test connection status sensor.""" | ||
await setup_platform(hass, mock_config_entry, [Platform.BINARY_SENSOR]) | ||
|
||
assert ( | ||
hass.states.get( | ||
"binary_sensor.iometer_1isk0000000000_core_bridge_connection_status" | ||
).state | ||
== STATE_ON | ||
) | ||
|
||
freezer.tick(delta=timedelta(minutes=1)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
mock_iometer_client.get_current_status.return_value.device.core.connection_status = "disconnected" | ||
|
||
freezer.tick(delta=timedelta(minutes=1)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
assert ( | ||
hass.states.get( | ||
"binary_sensor.iometer_1isk0000000000_core_bridge_connection_status" | ||
).state | ||
== STATE_OFF | ||
) | ||
|
||
|
||
@pytest.mark.usefixtures("entity_registry_enabled_by_default") | ||
async def test_attachment_status_sensors( | ||
hass: HomeAssistant, | ||
mock_config_entry: MockConfigEntry, | ||
mock_iometer_client: AsyncMock, | ||
freezer: FrozenDateTimeFactory, | ||
) -> None: | ||
"""Test connection status sensor.""" | ||
await setup_platform(hass, mock_config_entry, [Platform.BINARY_SENSOR]) | ||
|
||
assert ( | ||
hass.states.get( | ||
"binary_sensor.iometer_1isk0000000000_core_attachment_status" | ||
).state | ||
== STATE_ON | ||
) | ||
|
||
freezer.tick(delta=timedelta(minutes=1)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
mock_iometer_client.get_current_status.return_value.device.core.attachment_status = "detached" | ||
|
||
freezer.tick(delta=timedelta(minutes=1)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
assert ( | ||
hass.states.get( | ||
"binary_sensor.iometer_1isk0000000000_core_attachment_status" | ||
).state | ||
== STATE_OFF | ||
) | ||
|
||
|
||
@pytest.mark.usefixtures("entity_registry_enabled_by_default") | ||
async def test_attachment_status_sensors_unkown( | ||
hass: HomeAssistant, | ||
mock_config_entry: MockConfigEntry, | ||
mock_iometer_client: AsyncMock, | ||
freezer: FrozenDateTimeFactory, | ||
) -> None: | ||
"""Test connection status sensor.""" | ||
await setup_platform(hass, mock_config_entry, [Platform.BINARY_SENSOR]) | ||
|
||
assert ( | ||
hass.states.get( | ||
"binary_sensor.iometer_1isk0000000000_core_attachment_status" | ||
).state | ||
== STATE_ON | ||
) | ||
|
||
freezer.tick(delta=timedelta(minutes=1)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
mock_iometer_client.get_current_status.return_value.device.core.attachment_status = None | ||
|
||
freezer.tick(delta=timedelta(minutes=1)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
assert ( | ||
hass.states.get( | ||
"binary_sensor.iometer_1isk0000000000_core_attachment_status" | ||
).state | ||
== STATE_UNKNOWN | ||
) |
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
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.
split those to a separate test
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.
Done