Convert rfxtrx to entity naming (#74720)

pull/74864/head
Joakim Plate 2022-07-09 17:15:08 +02:00 committed by GitHub
parent 8bf692d046
commit 3a5cca3ff2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 47 deletions

View File

@ -456,6 +456,9 @@ class RfxtrxEntity(RestoreEntity):
Contains the common logic for Rfxtrx lights and switches.
"""
_attr_assumed_state = True
_attr_has_entity_name = True
_attr_should_poll = False
_device: rfxtrxmod.RFXtrxDevice
_event: rfxtrxmod.RFXtrxEvent | None
@ -466,7 +469,6 @@ class RfxtrxEntity(RestoreEntity):
event: rfxtrxmod.RFXtrxEvent | None = None,
) -> None:
"""Initialize the device."""
self._name = f"{device.type_string} {device.id_string}"
self._device = device
self._event = event
self._device_id = device_id
@ -484,16 +486,6 @@ class RfxtrxEntity(RestoreEntity):
async_dispatcher_connect(self.hass, SIGNAL_EVENT, self._handle_event)
)
@property
def should_poll(self):
"""No polling needed for a RFXtrx switch."""
return False
@property
def name(self):
"""Return the name of the device if any."""
return self._name
@property
def extra_state_attributes(self):
"""Return the device state attributes."""
@ -501,11 +493,6 @@ class RfxtrxEntity(RestoreEntity):
return None
return {ATTR_EVENT: "".join(f"{x:02x}" for x in self._event.data)}
@property
def assumed_state(self):
"""Return true if unable to access real state of entity."""
return True
@property
def unique_id(self):
"""Return unique identifier of remote device."""

View File

@ -63,12 +63,14 @@ class RfxtrxSensorEntityDescription(SensorEntityDescription):
SENSOR_TYPES = (
RfxtrxSensorEntityDescription(
key="Barometer",
name="Barometer",
device_class=SensorDeviceClass.PRESSURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=PRESSURE_HPA,
),
RfxtrxSensorEntityDescription(
key="Battery numeric",
name="Battery",
device_class=SensorDeviceClass.BATTERY,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=PERCENTAGE,
@ -77,42 +79,49 @@ SENSOR_TYPES = (
),
RfxtrxSensorEntityDescription(
key="Current",
name="Current",
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
),
RfxtrxSensorEntityDescription(
key="Current Ch. 1",
name="Current Ch. 1",
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
),
RfxtrxSensorEntityDescription(
key="Current Ch. 2",
name="Current Ch. 2",
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
),
RfxtrxSensorEntityDescription(
key="Current Ch. 3",
name="Current Ch. 3",
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
),
RfxtrxSensorEntityDescription(
key="Energy usage",
name="Instantaneous power",
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=POWER_WATT,
),
RfxtrxSensorEntityDescription(
key="Humidity",
name="Humidity",
device_class=SensorDeviceClass.HUMIDITY,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=PERCENTAGE,
),
RfxtrxSensorEntityDescription(
key="Rssi numeric",
name="Signal strength",
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
@ -121,86 +130,104 @@ SENSOR_TYPES = (
),
RfxtrxSensorEntityDescription(
key="Temperature",
name="Temperature",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=TEMP_CELSIUS,
),
RfxtrxSensorEntityDescription(
key="Temperature2",
name="Temperature 2",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=TEMP_CELSIUS,
),
RfxtrxSensorEntityDescription(
key="Total usage",
name="Total energy usage",
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
),
RfxtrxSensorEntityDescription(
key="Voltage",
name="Voltage",
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
),
RfxtrxSensorEntityDescription(
key="Wind direction",
name="Wind direction",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=DEGREE,
),
RfxtrxSensorEntityDescription(
key="Rain rate",
name="Rain rate",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=PRECIPITATION_MILLIMETERS_PER_HOUR,
),
RfxtrxSensorEntityDescription(
key="Sound",
name="Sound",
),
RfxtrxSensorEntityDescription(
key="Sensor Status",
name="Sensor status",
),
RfxtrxSensorEntityDescription(
key="Count",
name="Count",
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement="count",
),
RfxtrxSensorEntityDescription(
key="Counter value",
name="Counter value",
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement="count",
),
RfxtrxSensorEntityDescription(
key="Chill",
name="Chill",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=TEMP_CELSIUS,
),
RfxtrxSensorEntityDescription(
key="Wind average speed",
name="Wind average speed",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=SPEED_METERS_PER_SECOND,
),
RfxtrxSensorEntityDescription(
key="Wind gust",
name="Wind gust",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=SPEED_METERS_PER_SECOND,
),
RfxtrxSensorEntityDescription(
key="Rain total",
name="Rain total",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=LENGTH_MILLIMETERS,
),
RfxtrxSensorEntityDescription(
key="Forecast",
name="Forecast status",
),
RfxtrxSensorEntityDescription(
key="Forecast numeric",
name="Forecast",
),
RfxtrxSensorEntityDescription(
key="Humidity status",
name="Humidity status",
),
RfxtrxSensorEntityDescription(
key="UV",
name="UV index",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UV_INDEX,
),
@ -252,7 +279,6 @@ class RfxtrxSensor(RfxtrxEntity, SensorEntity):
"""Initialize the sensor."""
super().__init__(device, device_id, event=event)
self.entity_description = entity_description
self._name = f"{device.type_string} {device.id_string} {entity_description.key}"
self._unique_id = "_".join(
x for x in (*self._device_id, entity_description.key)
)

View File

@ -475,11 +475,11 @@ async def test_options_replace_sensor_device(hass):
await start_options_flow(hass, entry)
state = hass.states.get(
"sensor.thgn122_123_thgn132_thgr122_228_238_268_f0_04_rssi_numeric"
"sensor.thgn122_123_thgn132_thgr122_228_238_268_f0_04_signal_strength"
)
assert state
state = hass.states.get(
"sensor.thgn122_123_thgn132_thgr122_228_238_268_f0_04_battery_numeric"
"sensor.thgn122_123_thgn132_thgr122_228_238_268_f0_04_battery"
)
assert state
state = hass.states.get(
@ -495,11 +495,11 @@ async def test_options_replace_sensor_device(hass):
)
assert state
state = hass.states.get(
"sensor.thgn122_123_thgn132_thgr122_228_238_268_23_04_rssi_numeric"
"sensor.thgn122_123_thgn132_thgr122_228_238_268_23_04_signal_strength"
)
assert state
state = hass.states.get(
"sensor.thgn122_123_thgn132_thgr122_228_238_268_23_04_battery_numeric"
"sensor.thgn122_123_thgn132_thgr122_228_238_268_23_04_battery"
)
assert state
state = hass.states.get(
@ -565,7 +565,7 @@ async def test_options_replace_sensor_device(hass):
entity_registry = er.async_get(hass)
entry = entity_registry.async_get(
"sensor.thgn122_123_thgn132_thgr122_228_238_268_f0_04_rssi_numeric"
"sensor.thgn122_123_thgn132_thgr122_228_238_268_f0_04_signal_strength"
)
assert entry
assert entry.device_id == new_device
@ -580,7 +580,7 @@ async def test_options_replace_sensor_device(hass):
assert entry
assert entry.device_id == new_device
entry = entity_registry.async_get(
"sensor.thgn122_123_thgn132_thgr122_228_238_268_f0_04_battery_numeric"
"sensor.thgn122_123_thgn132_thgr122_228_238_268_f0_04_battery"
)
assert entry
assert entry.device_id == new_device
@ -591,11 +591,11 @@ async def test_options_replace_sensor_device(hass):
assert entry.device_id == new_device
state = hass.states.get(
"sensor.thgn122_123_thgn132_thgr122_228_238_268_23_04_rssi_numeric"
"sensor.thgn122_123_thgn132_thgr122_228_238_268_23_04_signal_strength"
)
assert not state
state = hass.states.get(
"sensor.thgn122_123_thgn132_thgr122_228_238_268_23_04_battery_numeric"
"sensor.thgn122_123_thgn132_thgr122_228_238_268_23_04_battery"
)
assert not state
state = hass.states.get(
@ -637,13 +637,13 @@ async def test_options_replace_control_device(hass):
state = hass.states.get("binary_sensor.ac_118cdea_2")
assert state
state = hass.states.get("sensor.ac_118cdea_2_rssi_numeric")
state = hass.states.get("sensor.ac_118cdea_2_signal_strength")
assert state
state = hass.states.get("switch.ac_118cdea_2")
assert state
state = hass.states.get("binary_sensor.ac_1118cdea_2")
assert state
state = hass.states.get("sensor.ac_1118cdea_2_rssi_numeric")
state = hass.states.get("sensor.ac_1118cdea_2_signal_strength")
assert state
state = hass.states.get("switch.ac_1118cdea_2")
assert state
@ -700,7 +700,7 @@ async def test_options_replace_control_device(hass):
entry = entity_registry.async_get("binary_sensor.ac_118cdea_2")
assert entry
assert entry.device_id == new_device
entry = entity_registry.async_get("sensor.ac_118cdea_2_rssi_numeric")
entry = entity_registry.async_get("sensor.ac_118cdea_2_signal_strength")
assert entry
assert entry.device_id == new_device
entry = entity_registry.async_get("switch.ac_118cdea_2")
@ -709,7 +709,7 @@ async def test_options_replace_control_device(hass):
state = hass.states.get("binary_sensor.ac_1118cdea_2")
assert not state
state = hass.states.get("sensor.ac_1118cdea_2_rssi_numeric")
state = hass.states.get("sensor.ac_1118cdea_2_signal_strength")
assert not state
state = hass.states.get("switch.ac_1118cdea_2")
assert not state

View File

@ -101,19 +101,19 @@ async def test_one_sensor_no_datatype(hass, rfxtrx):
assert state.attributes.get("friendly_name") == f"{base_name} Humidity status"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
state = hass.states.get(f"{base_id}_rssi_numeric")
state = hass.states.get(f"{base_id}_signal_strength")
assert state
assert state.state == "unknown"
assert state.attributes.get("friendly_name") == f"{base_name} Rssi numeric"
assert state.attributes.get("friendly_name") == f"{base_name} Signal strength"
assert (
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
== SIGNAL_STRENGTH_DECIBELS_MILLIWATT
)
state = hass.states.get(f"{base_id}_battery_numeric")
state = hass.states.get(f"{base_id}_battery")
assert state
assert state.state == "unknown"
assert state.attributes.get("friendly_name") == f"{base_name} Battery numeric"
assert state.attributes.get("friendly_name") == f"{base_name} Battery"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
@ -179,7 +179,7 @@ async def test_discover_sensor(hass, rfxtrx_automatic):
assert state.state == "normal"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
state = hass.states.get(f"{base_id}_rssi_numeric")
state = hass.states.get(f"{base_id}_signal_strength")
assert state
assert state.state == "-64"
assert (
@ -192,7 +192,7 @@ async def test_discover_sensor(hass, rfxtrx_automatic):
assert state.state == "18.4"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS
state = hass.states.get(f"{base_id}_battery_numeric")
state = hass.states.get(f"{base_id}_battery")
assert state
assert state.state == "100"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
@ -211,7 +211,7 @@ async def test_discover_sensor(hass, rfxtrx_automatic):
assert state.state == "normal"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
state = hass.states.get(f"{base_id}_rssi_numeric")
state = hass.states.get(f"{base_id}_signal_strength")
assert state
assert state.state == "-64"
assert (
@ -224,7 +224,7 @@ async def test_discover_sensor(hass, rfxtrx_automatic):
assert state.state == "14.9"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS
state = hass.states.get(f"{base_id}_battery_numeric")
state = hass.states.get(f"{base_id}_battery")
assert state
assert state.state == "100"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
@ -243,7 +243,7 @@ async def test_discover_sensor(hass, rfxtrx_automatic):
assert state.state == "normal"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
state = hass.states.get(f"{base_id}_rssi_numeric")
state = hass.states.get(f"{base_id}_signal_strength")
assert state
assert state.state == "-64"
assert (
@ -256,7 +256,7 @@ async def test_discover_sensor(hass, rfxtrx_automatic):
assert state.state == "17.9"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS
state = hass.states.get(f"{base_id}_battery_numeric")
state = hass.states.get(f"{base_id}_battery")
assert state
assert state.state == "100"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
@ -328,19 +328,19 @@ async def test_rssi_sensor(hass, rfxtrx):
await hass.async_block_till_done()
await hass.async_start()
state = hass.states.get("sensor.pt2262_22670e_rssi_numeric")
state = hass.states.get("sensor.pt2262_22670e_signal_strength")
assert state
assert state.state == "unknown"
assert state.attributes.get("friendly_name") == "PT2262 22670e Rssi numeric"
assert state.attributes.get("friendly_name") == "PT2262 22670e Signal strength"
assert (
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
== SIGNAL_STRENGTH_DECIBELS_MILLIWATT
)
state = hass.states.get("sensor.ac_213c7f2_48_rssi_numeric")
state = hass.states.get("sensor.ac_213c7f2_48_signal_strength")
assert state
assert state.state == "unknown"
assert state.attributes.get("friendly_name") == "AC 213c7f2:48 Rssi numeric"
assert state.attributes.get("friendly_name") == "AC 213c7f2:48 Signal strength"
assert (
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
== SIGNAL_STRENGTH_DECIBELS_MILLIWATT
@ -349,21 +349,21 @@ async def test_rssi_sensor(hass, rfxtrx):
await rfxtrx.signal("0913000022670e013b70")
await rfxtrx.signal("0b1100cd0213c7f230010f71")
state = hass.states.get("sensor.pt2262_22670e_rssi_numeric")
state = hass.states.get("sensor.pt2262_22670e_signal_strength")
assert state
assert state.state == "-64"
state = hass.states.get("sensor.ac_213c7f2_48_rssi_numeric")
state = hass.states.get("sensor.ac_213c7f2_48_signal_strength")
assert state
assert state.state == "-64"
await rfxtrx.signal("0913000022670e013b60")
await rfxtrx.signal("0b1100cd0213c7f230010f61")
state = hass.states.get("sensor.pt2262_22670e_rssi_numeric")
state = hass.states.get("sensor.pt2262_22670e_signal_strength")
assert state
assert state.state == "-72"
state = hass.states.get("sensor.ac_213c7f2_48_rssi_numeric")
state = hass.states.get("sensor.ac_213c7f2_48_signal_strength")
assert state
assert state.state == "-72"