mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Migrate Switchbot to new entity naming style (#80008)
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
c7b56f4079
commit
d0bffb6c75
@ -62,8 +62,6 @@ async def async_setup_entry(
|
|||||||
class SwitchBotBinarySensor(SwitchbotEntity, BinarySensorEntity):
|
class SwitchBotBinarySensor(SwitchbotEntity, BinarySensorEntity):
|
||||||
"""Representation of a Switchbot binary sensor."""
|
"""Representation of a Switchbot binary sensor."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: SwitchbotDataUpdateCoordinator,
|
coordinator: SwitchbotDataUpdateCoordinator,
|
||||||
|
@ -24,6 +24,7 @@ class SwitchbotEntity(PassiveBluetoothCoordinatorEntity):
|
|||||||
|
|
||||||
coordinator: SwitchbotDataUpdateCoordinator
|
coordinator: SwitchbotDataUpdateCoordinator
|
||||||
_device: SwitchbotDevice
|
_device: SwitchbotDevice
|
||||||
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
def __init__(self, coordinator: SwitchbotDataUpdateCoordinator) -> None:
|
def __init__(self, coordinator: SwitchbotDataUpdateCoordinator) -> None:
|
||||||
"""Initialize the entity."""
|
"""Initialize the entity."""
|
||||||
@ -32,7 +33,6 @@ class SwitchbotEntity(PassiveBluetoothCoordinatorEntity):
|
|||||||
self._last_run_success: bool | None = None
|
self._last_run_success: bool | None = None
|
||||||
self._address = coordinator.ble_device.address
|
self._address = coordinator.ble_device.address
|
||||||
self._attr_unique_id = coordinator.base_unique_id
|
self._attr_unique_id = coordinator.base_unique_id
|
||||||
self._attr_name = coordinator.device_name
|
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
connections={(dr.CONNECTION_BLUETOOTH, self._address)},
|
connections={(dr.CONNECTION_BLUETOOTH, self._address)},
|
||||||
manufacturer=MANUFACTURER,
|
manufacturer=MANUFACTURER,
|
||||||
|
@ -26,6 +26,7 @@ PARALLEL_UPDATES = 0
|
|||||||
SENSOR_TYPES: dict[str, SensorEntityDescription] = {
|
SENSOR_TYPES: dict[str, SensorEntityDescription] = {
|
||||||
"rssi": SensorEntityDescription(
|
"rssi": SensorEntityDescription(
|
||||||
key="rssi",
|
key="rssi",
|
||||||
|
name="Bluetooth signal strength",
|
||||||
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||||
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
@ -34,6 +35,7 @@ SENSOR_TYPES: dict[str, SensorEntityDescription] = {
|
|||||||
),
|
),
|
||||||
"wifi_rssi": SensorEntityDescription(
|
"wifi_rssi": SensorEntityDescription(
|
||||||
key="wifi_rssi",
|
key="wifi_rssi",
|
||||||
|
name="Wi-Fi signal strength",
|
||||||
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||||
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
@ -42,6 +44,7 @@ SENSOR_TYPES: dict[str, SensorEntityDescription] = {
|
|||||||
),
|
),
|
||||||
"battery": SensorEntityDescription(
|
"battery": SensorEntityDescription(
|
||||||
key="battery",
|
key="battery",
|
||||||
|
name="Battery",
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
device_class=SensorDeviceClass.BATTERY,
|
device_class=SensorDeviceClass.BATTERY,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
@ -49,18 +52,21 @@ SENSOR_TYPES: dict[str, SensorEntityDescription] = {
|
|||||||
),
|
),
|
||||||
"lightLevel": SensorEntityDescription(
|
"lightLevel": SensorEntityDescription(
|
||||||
key="lightLevel",
|
key="lightLevel",
|
||||||
|
name="Light level",
|
||||||
native_unit_of_measurement="Level",
|
native_unit_of_measurement="Level",
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
device_class=SensorDeviceClass.ILLUMINANCE,
|
device_class=SensorDeviceClass.ILLUMINANCE,
|
||||||
),
|
),
|
||||||
"humidity": SensorEntityDescription(
|
"humidity": SensorEntityDescription(
|
||||||
key="humidity",
|
key="humidity",
|
||||||
|
name="Humidity",
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
device_class=SensorDeviceClass.HUMIDITY,
|
device_class=SensorDeviceClass.HUMIDITY,
|
||||||
),
|
),
|
||||||
"temperature": SensorEntityDescription(
|
"temperature": SensorEntityDescription(
|
||||||
key="temperature",
|
key="temperature",
|
||||||
|
name="Temperature",
|
||||||
native_unit_of_measurement=TEMP_CELSIUS,
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
device_class=SensorDeviceClass.TEMPERATURE,
|
device_class=SensorDeviceClass.TEMPERATURE,
|
||||||
@ -97,8 +103,6 @@ class SwitchBotSensor(SwitchbotEntity, SensorEntity):
|
|||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._sensor = sensor
|
self._sensor = sensor
|
||||||
self._attr_unique_id = f"{coordinator.base_unique_id}-{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]
|
self.entity_description = SENSOR_TYPES[sensor]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -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_UNIT_OF_MEASUREMENT] == "%"
|
||||||
assert battery_sensor_attrs[ATTR_STATE_CLASS] == "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
|
rssi_sensor_attrs = rssi_sensor.attributes
|
||||||
assert rssi_sensor.state == "-60"
|
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 rssi_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "dBm"
|
||||||
|
|
||||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user