1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Name unnamed binary sensors by their device class (#92940)

* Name unnamed binary sensors by their device class

* Update type annotations

* Fix loading of entity component translations

* Add test

* Update integrations

* Set abode and rfxtrx binary_sensor name to None

* Revert changes in homekit_controller
This commit is contained in:
Erik Montnemery
2023-06-13 19:48:54 +02:00
committed by GitHub
parent 223394eaee
commit 2406b235b4
27 changed files with 287 additions and 47 deletions

View File

@@ -1,8 +1,25 @@
"""The tests for the Binary sensor component."""
from collections.abc import Generator
from unittest import mock
import pytest
from homeassistant.components import binary_sensor
from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from tests.common import (
MockConfigEntry,
MockModule,
MockPlatform,
mock_config_flow,
mock_integration,
mock_platform,
)
TEST_DOMAIN = "test"
def test_state() -> None:
@@ -19,3 +36,93 @@ def test_state() -> None:
new=True,
):
assert binary_sensor.BinarySensorEntity().state == STATE_ON
class STTFlow(ConfigFlow):
"""Test flow."""
@pytest.fixture(autouse=True)
def config_flow_fixture(hass: HomeAssistant) -> Generator[None, None, None]:
"""Mock config flow."""
mock_platform(hass, f"{TEST_DOMAIN}.config_flow")
with mock_config_flow(TEST_DOMAIN, STTFlow):
yield
async def test_name(hass: HomeAssistant) -> None:
"""Test binary sensor name."""
async def async_setup_entry_init(
hass: HomeAssistant, config_entry: ConfigEntry
) -> bool:
"""Set up test config entry."""
await hass.config_entries.async_forward_entry_setup(
config_entry, binary_sensor.DOMAIN
)
return True
mock_platform(hass, f"{TEST_DOMAIN}.config_flow")
mock_integration(
hass,
MockModule(
TEST_DOMAIN,
async_setup_entry=async_setup_entry_init,
),
)
# Unnamed binary sensor without device class -> no name
entity1 = binary_sensor.BinarySensorEntity()
entity1.entity_id = "binary_sensor.test1"
# Unnamed binary sensor with device class but has_entity_name False -> no name
entity2 = binary_sensor.BinarySensorEntity()
entity2.entity_id = "binary_sensor.test2"
entity2._attr_device_class = binary_sensor.BinarySensorDeviceClass.BATTERY
# Unnamed binary sensor with device class and has_entity_name True -> named
entity3 = binary_sensor.BinarySensorEntity()
entity3.entity_id = "binary_sensor.test3"
entity3._attr_device_class = binary_sensor.BinarySensorDeviceClass.BATTERY
entity3._attr_has_entity_name = True
# Unnamed binary sensor with device class and has_entity_name True -> named
entity4 = binary_sensor.BinarySensorEntity()
entity4.entity_id = "binary_sensor.test4"
entity4.entity_description = binary_sensor.BinarySensorEntityDescription(
"test",
binary_sensor.BinarySensorDeviceClass.BATTERY,
has_entity_name=True,
)
async def async_setup_entry_platform(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up test stt platform via config entry."""
async_add_entities([entity1, entity2, entity3, entity4])
mock_platform(
hass,
f"{TEST_DOMAIN}.{binary_sensor.DOMAIN}",
MockPlatform(async_setup_entry=async_setup_entry_platform),
)
config_entry = MockConfigEntry(domain=TEST_DOMAIN)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get(entity1.entity_id)
assert state.attributes == {}
state = hass.states.get(entity2.entity_id)
assert state.attributes == {"device_class": "battery"}
state = hass.states.get(entity3.entity_id)
assert state.attributes == {"device_class": "battery", "friendly_name": "Battery"}
state = hass.states.get(entity4.entity_id)
assert state.attributes == {"device_class": "battery", "friendly_name": "Battery"}