Migrate sabnzbd sensors unique ids (#71455)
* Migrate sensors unique ids 1. migrate sensors to have unique id constructed also from entry_id 2. add migration flow in init 3. bump config flow to version 2 4. add tests for migration * move migrate to async_setup_entry * 1. Use the entity registry api in tests 2. Set up the config entry and not use integration directly 3. remove patch for entity registry * fix too many lines * Update tests/components/sabnzbd/test_init.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/sabnzbd/test_init.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/sabnzbd/test_init.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/sabnzbd/test_init.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
1be2438ef6
commit
f50681e3d3
4 changed files with 164 additions and 13 deletions
|
@ -113,11 +113,16 @@ async def async_setup_entry(
|
|||
) -> None:
|
||||
"""Set up a Sabnzbd sensor entry."""
|
||||
|
||||
sab_api_data = hass.data[DOMAIN][config_entry.entry_id][KEY_API_DATA]
|
||||
client_name = hass.data[DOMAIN][config_entry.entry_id][KEY_NAME]
|
||||
entry_id = config_entry.entry_id
|
||||
|
||||
sab_api_data = hass.data[DOMAIN][entry_id][KEY_API_DATA]
|
||||
client_name = hass.data[DOMAIN][entry_id][KEY_NAME]
|
||||
|
||||
async_add_entities(
|
||||
[SabnzbdSensor(sab_api_data, client_name, sensor) for sensor in SENSOR_TYPES]
|
||||
[
|
||||
SabnzbdSensor(sab_api_data, client_name, sensor, entry_id)
|
||||
for sensor in SENSOR_TYPES
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
|
@ -128,17 +133,21 @@ class SabnzbdSensor(SensorEntity):
|
|||
_attr_should_poll = False
|
||||
|
||||
def __init__(
|
||||
self, sabnzbd_api_data, client_name, description: SabnzbdSensorEntityDescription
|
||||
self,
|
||||
sabnzbd_api_data,
|
||||
client_name,
|
||||
description: SabnzbdSensorEntityDescription,
|
||||
entry_id,
|
||||
):
|
||||
"""Initialize the sensor."""
|
||||
unique_id = description.key
|
||||
self._attr_unique_id = unique_id
|
||||
|
||||
self._attr_unique_id = f"{entry_id}_{description.key}"
|
||||
self.entity_description = description
|
||||
self._sabnzbd_api = sabnzbd_api_data
|
||||
self._attr_name = f"{client_name} {description.name}"
|
||||
self._attr_device_info = DeviceInfo(
|
||||
entry_type=DeviceEntryType.SERVICE,
|
||||
identifiers={(DOMAIN, DOMAIN)},
|
||||
identifiers={(DOMAIN, entry_id)},
|
||||
name=DEFAULT_NAME,
|
||||
)
|
||||
|
||||
|
@ -156,9 +165,11 @@ class SabnzbdSensor(SensorEntity):
|
|||
self.entity_description.key
|
||||
)
|
||||
|
||||
if self.entity_description.key == SPEED_KEY:
|
||||
self._attr_native_value = round(float(self._attr_native_value) / 1024, 1)
|
||||
elif "size" in self.entity_description.key:
|
||||
self._attr_native_value = round(float(self._attr_native_value), 2)
|
||||
|
||||
if self._attr_native_value is not None:
|
||||
if self.entity_description.key == SPEED_KEY:
|
||||
self._attr_native_value = round(
|
||||
float(self._attr_native_value) / 1024, 1
|
||||
)
|
||||
elif "size" in self.entity_description.key:
|
||||
self._attr_native_value = round(float(self._attr_native_value), 2)
|
||||
self.schedule_update_ha_state()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue