-
-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add binary_sensor platform for Smlight integration (#125284)
* Support binary_sensors for SMLight integration * Add strings for binary sensors * Add tests for binary_sensor platform * Update binary sensor docstring Co-authored-by: Shay Levy <[email protected]> * Regenerate snapshot --------- Co-authored-by: Shay Levy <[email protected]> Co-authored-by: Tim Lunn <[email protected]>
- Loading branch information
1 parent
f80acda
commit a341bfd
Showing
5 changed files
with
236 additions
and
0 deletions.
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,80 @@ | ||
"""Support for SLZB-06 binary sensors.""" | ||
|
||
from __future__ import annotations | ||
|
||
from _collections_abc import Callable | ||
from dataclasses import dataclass | ||
|
||
from pysmlight import Sensors | ||
|
||
from homeassistant.components.binary_sensor import ( | ||
BinarySensorDeviceClass, | ||
BinarySensorEntity, | ||
BinarySensorEntityDescription, | ||
) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import EntityCategory | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from .coordinator import SmDataUpdateCoordinator | ||
from .entity import SmEntity | ||
|
||
|
||
@dataclass(frozen=True, kw_only=True) | ||
class SmBinarySensorEntityDescription(BinarySensorEntityDescription): | ||
"""Class describing SMLIGHT binary sensor entities.""" | ||
|
||
value_fn: Callable[[Sensors], bool] | ||
|
||
|
||
SENSORS = [ | ||
SmBinarySensorEntityDescription( | ||
key="ethernet", | ||
translation_key="ethernet", | ||
value_fn=lambda x: x.ethernet, | ||
), | ||
SmBinarySensorEntityDescription( | ||
key="wifi", | ||
translation_key="wifi", | ||
entity_registry_enabled_default=False, | ||
value_fn=lambda x: x.wifi_connected, | ||
), | ||
] | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
entry: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up SMLIGHT sensor based on a config entry.""" | ||
coordinator = entry.runtime_data | ||
|
||
async_add_entities( | ||
SmBinarySensorEntity(coordinator, description) for description in SENSORS | ||
) | ||
|
||
|
||
class SmBinarySensorEntity(SmEntity, BinarySensorEntity): | ||
"""Representation of a slzb binary sensor.""" | ||
|
||
entity_description: SmBinarySensorEntityDescription | ||
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY | ||
_attr_entity_category = EntityCategory.DIAGNOSTIC | ||
|
||
def __init__( | ||
self, | ||
coordinator: SmDataUpdateCoordinator, | ||
description: SmBinarySensorEntityDescription, | ||
) -> None: | ||
"""Initialize slzb binary sensor.""" | ||
super().__init__(coordinator) | ||
|
||
self.entity_description = description | ||
self._attr_unique_id = f"{coordinator.unique_id}_{description.key}" | ||
|
||
@property | ||
def is_on(self) -> bool: | ||
"""Return the state of the sensor.""" | ||
return self.entity_description.value_fn(self.coordinator.data.sensors) |
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
95 changes: 95 additions & 0 deletions
95
tests/components/smlight/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,95 @@ | ||
# serializer version: 1 | ||
# name: test_all_binary_sensors[binary_sensor.mock_title_ethernet-entry] | ||
EntityRegistryEntrySnapshot({ | ||
'aliases': set({ | ||
}), | ||
'area_id': None, | ||
'capabilities': None, | ||
'config_entry_id': <ANY>, | ||
'device_class': None, | ||
'device_id': <ANY>, | ||
'disabled_by': None, | ||
'domain': 'binary_sensor', | ||
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>, | ||
'entity_id': 'binary_sensor.mock_title_ethernet', | ||
'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': 'Ethernet', | ||
'platform': 'smlight', | ||
'previous_unique_id': None, | ||
'supported_features': 0, | ||
'translation_key': 'ethernet', | ||
'unique_id': 'aa:bb:cc:dd:ee:ff_ethernet', | ||
'unit_of_measurement': None, | ||
}) | ||
# --- | ||
# name: test_all_binary_sensors[binary_sensor.mock_title_ethernet-state] | ||
StateSnapshot({ | ||
'attributes': ReadOnlyDict({ | ||
'device_class': 'connectivity', | ||
'friendly_name': 'Mock Title Ethernet', | ||
}), | ||
'context': <ANY>, | ||
'entity_id': 'binary_sensor.mock_title_ethernet', | ||
'last_changed': <ANY>, | ||
'last_reported': <ANY>, | ||
'last_updated': <ANY>, | ||
'state': 'on', | ||
}) | ||
# --- | ||
# name: test_all_binary_sensors[binary_sensor.mock_title_wi_fi-entry] | ||
EntityRegistryEntrySnapshot({ | ||
'aliases': set({ | ||
}), | ||
'area_id': None, | ||
'capabilities': None, | ||
'config_entry_id': <ANY>, | ||
'device_class': None, | ||
'device_id': <ANY>, | ||
'disabled_by': None, | ||
'domain': 'binary_sensor', | ||
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>, | ||
'entity_id': 'binary_sensor.mock_title_wi_fi', | ||
'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': 'Wi-Fi', | ||
'platform': 'smlight', | ||
'previous_unique_id': None, | ||
'supported_features': 0, | ||
'translation_key': 'wifi', | ||
'unique_id': 'aa:bb:cc:dd:ee:ff_wifi', | ||
'unit_of_measurement': None, | ||
}) | ||
# --- | ||
# name: test_all_binary_sensors[binary_sensor.mock_title_wi_fi-state] | ||
StateSnapshot({ | ||
'attributes': ReadOnlyDict({ | ||
'device_class': 'connectivity', | ||
'friendly_name': 'Mock Title Wi-Fi', | ||
}), | ||
'context': <ANY>, | ||
'entity_id': 'binary_sensor.mock_title_wi_fi', | ||
'last_changed': <ANY>, | ||
'last_reported': <ANY>, | ||
'last_updated': <ANY>, | ||
'state': 'off', | ||
}) | ||
# --- |
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,52 @@ | ||
"""Tests for the SMLIGHT binary sensor platform.""" | ||
|
||
import pytest | ||
from syrupy.assertion import SnapshotAssertion | ||
|
||
from homeassistant.const import Platform | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers import entity_registry as er | ||
|
||
from .conftest import setup_integration | ||
|
||
from tests.common import MockConfigEntry, snapshot_platform | ||
|
||
pytestmark = [ | ||
pytest.mark.usefixtures( | ||
"mock_smlight_client", | ||
) | ||
] | ||
|
||
|
||
@pytest.fixture | ||
def platforms() -> list[Platform]: | ||
"""Platforms, which should be loaded during the test.""" | ||
return [Platform.BINARY_SENSOR] | ||
|
||
|
||
@pytest.mark.usefixtures("entity_registry_enabled_by_default") | ||
async def test_all_binary_sensors( | ||
hass: HomeAssistant, | ||
entity_registry: er.EntityRegistry, | ||
mock_config_entry: MockConfigEntry, | ||
snapshot: SnapshotAssertion, | ||
) -> None: | ||
"""Test the SMLIGHT binary sensors.""" | ||
entry = await setup_integration(hass, mock_config_entry) | ||
|
||
await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id) | ||
|
||
|
||
async def test_disabled_by_default_sensors( | ||
hass: HomeAssistant, | ||
entity_registry: er.EntityRegistry, | ||
mock_config_entry: MockConfigEntry, | ||
) -> None: | ||
"""Test wifi sensor is disabled by default .""" | ||
await setup_integration(hass, mock_config_entry) | ||
|
||
assert not hass.states.get("binary_sensor.mock_title_wi_fi") | ||
|
||
assert (entry := entity_registry.async_get("binary_sensor.mock_title_wi_fi")) | ||
assert entry.disabled | ||
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION |