Migrate Switchbot to new entity naming style (#80008)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
Patrick ZAJDA 2022-10-10 14:12:50 +02:00 committed by GitHub
parent c7b56f4079
commit d0bffb6c75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 7 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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

View File

@ -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)