Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions homeassistant/components/airthings_ble/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=0,
),
"connectivity_mode": SensorEntityDescription(
key="connectivity_mode",
translation_key="connectivity_mode",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
Comment on lines +132 to +137
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So there are 3 modes right? Let's make this an enum sensor and add the values (ideally snake_case so we can translate the states) to the options and then HA knows what values it could be

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 3, plus the Unknown state. That should not happen, and I realise now that it should then be None instead, and then just ignore it instead of showing Unknown.

Should I create a new enum here? Or reuse the one from the library?

If I reuse it, HA is not the "source of truth" and the values could change, meaning the translations keys could be wrong.

}

PARALLEL_UPDATES = 0
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/airthings_ble/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"ambient_noise": {
"name": "Ambient noise"
},
"connectivity_mode": {
"name": "Connectivity mode"
},
"illuminance": {
"name": "[%key:component::sensor::entity_component::illuminance::name%]"
},
Expand Down
40 changes: 40 additions & 0 deletions tests/components/airthings_ble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@ def patch_airthings_device_update():
tx_power=0,
)

CORENTIUM_HOME_2_SERVICE_INFO = BluetoothServiceInfoBleak(
name="cc-cc-cc-cc-cc-cc",
address="cc:cc:cc:cc:cc:cc",
device=generate_ble_device(
address="cc:cc:cc:cc:cc:cc",
name="Airthings Corentium Home 2",
),
rssi=-61,
manufacturer_data={820: b"\xe4/\xa5\xae\t\x00"},
service_data={},
service_uuids=[],
source="local",
advertisement=generate_advertisement_data(
manufacturer_data={820: b"\xe4/\xa5\xae\t\x00"},
service_uuids=[],
),
connectable=True,
time=0,
tx_power=0,
)

VIEW_PLUS_SERVICE_INFO = BluetoothServiceInfoBleak(
name="cc-cc-cc-cc-cc-cc",
address="cc:cc:cc:cc:cc:cc",
Expand Down Expand Up @@ -253,6 +274,7 @@ def patch_airthings_device_update():
name="Airthings Wave Enhance",
identifier="123456",
sensors={
"connectivity_mode": "Bluetooth",
"lux": 25,
"battery": 85,
"humidity": 60.0,
Expand All @@ -265,6 +287,24 @@ def patch_airthings_device_update():
address="cc:cc:cc:cc:cc:cc",
)

CORENTIUM_HOME_2_DEVICE_INFO = AirthingsDevice(
manufacturer="Airthings AS",
hw_version="REV Y",
sw_version="C-HOME-2.3.1-master+0",
model=AirthingsDeviceType.CORENTIUM_HOME_2,
name="Airthings Corentium Home 2",
identifier="123456",
sensors={
"connectivity_mode": "Bluetooth",
"battery": 90,
"temperature": 20.0,
"humidity": 55.0,
"radon_1day_avg": 45,
"radon_1day_level": "low",
},
address="cc:cc:cc:cc:cc:cc",
)

TEMPERATURE_V1 = MockEntity(
unique_id="Airthings Wave Plus 123456_temperature",
name="Airthings Wave Plus 123456 Temperature",
Expand Down
65 changes: 63 additions & 2 deletions tests/components/airthings_ble/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from . import (
CO2_V1,
CO2_V2,
CORENTIUM_HOME_2_DEVICE_INFO,
CORENTIUM_HOME_2_SERVICE_INFO,
HUMIDITY_V2,
TEMPERATURE_V1,
VOC_V1,
Expand Down Expand Up @@ -231,15 +233,15 @@ async def test_migration_with_all_unique_ids(
("noise", "Ambient noise"),
],
)
async def test_translation_keys(
async def test_translation_keys_wave_enhance(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
unique_suffix: str,
expected_sensor_name: str,
) -> None:
"""Test that translated sensor names are correct."""
entry = create_entry(hass, WAVE_ENHANCE_SERVICE_INFO, WAVE_DEVICE_INFO)
entry = create_entry(hass, WAVE_ENHANCE_SERVICE_INFO, WAVE_ENHANCE_DEVICE_INFO)
device = create_device(
entry, device_registry, WAVE_ENHANCE_SERVICE_INFO, WAVE_ENHANCE_DEVICE_INFO
)
Expand Down Expand Up @@ -267,3 +269,62 @@ async def test_translation_keys(

expected_name = f"Airthings Wave Enhance (123456) {expected_sensor_name}"
assert state.attributes.get("friendly_name") == expected_name


async def test_disabled_translation_keys_corentium_home_2(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test that translated sensor names are correct for disabled sensors."""
entry = create_entry(
hass,
CORENTIUM_HOME_2_SERVICE_INFO,
CORENTIUM_HOME_2_DEVICE_INFO,
)
device = create_device(
entry,
device_registry,
CORENTIUM_HOME_2_SERVICE_INFO,
CORENTIUM_HOME_2_DEVICE_INFO,
)

with (
patch_async_ble_device_from_address(CORENTIUM_HOME_2_SERVICE_INFO.device),
patch_async_discovered_service_info([CORENTIUM_HOME_2_SERVICE_INFO]),
patch_airthings_ble(CORENTIUM_HOME_2_DEVICE_INFO),
):
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

assert device is not None
assert device.name == "Airthings Corentium Home 2 (123456)"

unique_id = f"{CORENTIUM_HOME_2_DEVICE_INFO.address}_connectivity_mode"

entity_id = entity_registry.async_get_entity_id(Platform.SENSOR, DOMAIN, unique_id)
assert entity_id is not None

entity_entry = entity_registry.async_get(entity_id)
assert entity_entry is not None
assert entity_entry.disabled
assert entity_entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION

updated_entry = entity_registry.async_update_entity(
entity_entry.entity_id, disabled_by=None
)
assert updated_entry != entity_entry
assert updated_entry.disabled is False

await hass.config_entries.async_forward_entry_unload(entry, Platform.SENSOR)
await hass.config_entries.async_forward_entry_setups(entry, [Platform.SENSOR])
await hass.async_block_till_done()

state = hass.states.get(entity_id)
assert state is not None

expected_value = CORENTIUM_HOME_2_DEVICE_INFO.sensors["connectivity_mode"]
assert state.state == str(expected_value)

expected_name = "Airthings Corentium Home 2 (123456) Connectivity mode"
assert state.attributes.get("friendly_name") == expected_name
Loading