mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 02:07:54 +00:00
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 <levyshay1@gmail.com> * Regenerate snapshot --------- Co-authored-by: Shay Levy <levyshay1@gmail.com> Co-authored-by: Tim Lunn <tim@feathertop.org>
This commit is contained in:
parent
f80acdada0
commit
a341bfd8ca
@ -9,6 +9,7 @@ from homeassistant.core import HomeAssistant
|
||||
from .coordinator import SmDataUpdateCoordinator
|
||||
|
||||
PLATFORMS: list[Platform] = [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.BUTTON,
|
||||
Platform.SENSOR,
|
||||
]
|
||||
|
80
homeassistant/components/smlight/binary_sensor.py
Normal file
80
homeassistant/components/smlight/binary_sensor.py
Normal file
@ -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)
|
@ -42,6 +42,14 @@
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"binary_sensor": {
|
||||
"ethernet": {
|
||||
"name": "Ethernet"
|
||||
},
|
||||
"wifi": {
|
||||
"name": "Wi-Fi"
|
||||
}
|
||||
},
|
||||
"sensor": {
|
||||
"zigbee_temperature": {
|
||||
"name": "Zigbee chip temp"
|
||||
|
95
tests/components/smlight/snapshots/test_binary_sensor.ambr
Normal file
95
tests/components/smlight/snapshots/test_binary_sensor.ambr
Normal file
@ -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',
|
||||
})
|
||||
# ---
|
52
tests/components/smlight/test_binary_sensor.py
Normal file
52
tests/components/smlight/test_binary_sensor.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user