From d0bffb6c75def1caaada9b0c4a8b279f6d811a74 Mon Sep 17 00:00:00 2001 From: Patrick ZAJDA Date: Mon, 10 Oct 2022 14:12:50 +0200 Subject: [PATCH] Migrate Switchbot to new entity naming style (#80008) Co-authored-by: Franck Nijhof --- homeassistant/components/switchbot/binary_sensor.py | 2 -- homeassistant/components/switchbot/entity.py | 2 +- homeassistant/components/switchbot/sensor.py | 8 ++++++-- tests/components/switchbot/test_sensor.py | 6 ++++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/switchbot/binary_sensor.py b/homeassistant/components/switchbot/binary_sensor.py index a5378028264..296cd4c1800 100644 --- a/homeassistant/components/switchbot/binary_sensor.py +++ b/homeassistant/components/switchbot/binary_sensor.py @@ -62,8 +62,6 @@ async def async_setup_entry( class SwitchBotBinarySensor(SwitchbotEntity, BinarySensorEntity): """Representation of a Switchbot binary sensor.""" - _attr_has_entity_name = True - def __init__( self, coordinator: SwitchbotDataUpdateCoordinator, diff --git a/homeassistant/components/switchbot/entity.py b/homeassistant/components/switchbot/entity.py index b8d08e74f5f..fcf0bdc4da2 100644 --- a/homeassistant/components/switchbot/entity.py +++ b/homeassistant/components/switchbot/entity.py @@ -24,6 +24,7 @@ class SwitchbotEntity(PassiveBluetoothCoordinatorEntity): coordinator: SwitchbotDataUpdateCoordinator _device: SwitchbotDevice + _attr_has_entity_name = True def __init__(self, coordinator: SwitchbotDataUpdateCoordinator) -> None: """Initialize the entity.""" @@ -32,7 +33,6 @@ class SwitchbotEntity(PassiveBluetoothCoordinatorEntity): self._last_run_success: bool | None = None self._address = coordinator.ble_device.address self._attr_unique_id = coordinator.base_unique_id - self._attr_name = coordinator.device_name self._attr_device_info = DeviceInfo( connections={(dr.CONNECTION_BLUETOOTH, self._address)}, manufacturer=MANUFACTURER, diff --git a/homeassistant/components/switchbot/sensor.py b/homeassistant/components/switchbot/sensor.py index 8e5d0e92d5a..1077fd4fce6 100644 --- a/homeassistant/components/switchbot/sensor.py +++ b/homeassistant/components/switchbot/sensor.py @@ -26,6 +26,7 @@ PARALLEL_UPDATES = 0 SENSOR_TYPES: dict[str, SensorEntityDescription] = { "rssi": SensorEntityDescription( key="rssi", + name="Bluetooth signal strength", native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, device_class=SensorDeviceClass.SIGNAL_STRENGTH, state_class=SensorStateClass.MEASUREMENT, @@ -34,6 +35,7 @@ SENSOR_TYPES: dict[str, SensorEntityDescription] = { ), "wifi_rssi": SensorEntityDescription( key="wifi_rssi", + name="Wi-Fi signal strength", native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, device_class=SensorDeviceClass.SIGNAL_STRENGTH, state_class=SensorStateClass.MEASUREMENT, @@ -42,6 +44,7 @@ SENSOR_TYPES: dict[str, SensorEntityDescription] = { ), "battery": SensorEntityDescription( key="battery", + name="Battery", native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.BATTERY, state_class=SensorStateClass.MEASUREMENT, @@ -49,18 +52,21 @@ SENSOR_TYPES: dict[str, SensorEntityDescription] = { ), "lightLevel": SensorEntityDescription( key="lightLevel", + name="Light level", native_unit_of_measurement="Level", state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.ILLUMINANCE, ), "humidity": SensorEntityDescription( key="humidity", + name="Humidity", native_unit_of_measurement=PERCENTAGE, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.HUMIDITY, ), "temperature": SensorEntityDescription( key="temperature", + name="Temperature", native_unit_of_measurement=TEMP_CELSIUS, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.TEMPERATURE, @@ -97,8 +103,6 @@ class SwitchBotSensor(SwitchbotEntity, SensorEntity): super().__init__(coordinator) self._sensor = sensor self._attr_unique_id = f"{coordinator.base_unique_id}-{sensor}" - name = coordinator.device_name - self._attr_name = f"{name} {sensor.replace('_', ' ').title()}" self.entity_description = SENSOR_TYPES[sensor] @property diff --git a/tests/components/switchbot/test_sensor.py b/tests/components/switchbot/test_sensor.py index ae77c5a8de4..9de1403a634 100644 --- a/tests/components/switchbot/test_sensor.py +++ b/tests/components/switchbot/test_sensor.py @@ -48,10 +48,12 @@ async def test_sensors(hass, entity_registry_enabled_by_default): assert battery_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "%" assert battery_sensor_attrs[ATTR_STATE_CLASS] == "measurement" - rssi_sensor = hass.states.get("sensor.test_name_rssi") + rssi_sensor = hass.states.get("sensor.test_name_bluetooth_signal_strength") rssi_sensor_attrs = rssi_sensor.attributes assert rssi_sensor.state == "-60" - assert rssi_sensor_attrs[ATTR_FRIENDLY_NAME] == "test-name Rssi" + assert ( + rssi_sensor_attrs[ATTR_FRIENDLY_NAME] == "test-name Bluetooth signal strength" + ) assert rssi_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "dBm" assert await hass.config_entries.async_unload(entry.entry_id)