mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Add unique id to amcrest sensors (#55243)
* Add unique id to amcrest sensors * Change 'unique_id' to 'serial_number' on api wrapper * Update unique id's with channel value that can be used in future changes and remove unrelated camera changes
This commit is contained in:
parent
175f207d28
commit
6d99a7a730
@ -169,6 +169,7 @@ class AmcrestBinarySensor(BinarySensorEntity):
|
||||
"""Initialize entity."""
|
||||
self._signal_name = name
|
||||
self._api = device.api
|
||||
self._channel = 0 # Used in unique id, reserved for future use
|
||||
self.entity_description: AmcrestSensorEntityDescription = entity_description
|
||||
|
||||
self._attr_name = f"{name} {entity_description.name}"
|
||||
@ -192,6 +193,9 @@ class AmcrestBinarySensor(BinarySensorEntity):
|
||||
if not (self._api.available or self.is_on):
|
||||
return
|
||||
_LOGGER.debug(_UPDATE_MSG, self.name)
|
||||
|
||||
self._update_unique_id()
|
||||
|
||||
if self._api.available:
|
||||
# Send a command to the camera to test if we can still communicate with it.
|
||||
# Override of Http.command() in __init__.py will set self._api.available
|
||||
@ -205,6 +209,8 @@ class AmcrestBinarySensor(BinarySensorEntity):
|
||||
return
|
||||
_LOGGER.debug(_UPDATE_MSG, self.name)
|
||||
|
||||
self._update_unique_id()
|
||||
|
||||
event_code = self.entity_description.event_code
|
||||
if event_code is None:
|
||||
_LOGGER.error("Binary sensor %s event code not set", self.name)
|
||||
@ -215,6 +221,15 @@ class AmcrestBinarySensor(BinarySensorEntity):
|
||||
except AmcrestError as error:
|
||||
log_update_error(_LOGGER, "update", self.name, "binary sensor", error)
|
||||
|
||||
def _update_unique_id(self) -> None:
|
||||
"""Set the unique id."""
|
||||
if self._attr_unique_id is None:
|
||||
serial_number = self._api.serial_number
|
||||
if serial_number:
|
||||
self._attr_unique_id = (
|
||||
f"{serial_number}-{self.entity_description.key}-{self._channel}"
|
||||
)
|
||||
|
||||
async def async_on_demand_update(self) -> None:
|
||||
"""Update state."""
|
||||
if self.entity_description.key == _ONLINE_KEY:
|
||||
|
@ -77,6 +77,7 @@ class AmcrestSensor(SensorEntity):
|
||||
self.entity_description = description
|
||||
self._signal_name = name
|
||||
self._api = device.api
|
||||
self._channel = 0 # Used in unique id, reserved for future use
|
||||
self._unsub_dispatcher: Callable[[], None] | None = None
|
||||
|
||||
self._attr_name = f"{name} {description.name}"
|
||||
@ -94,6 +95,11 @@ class AmcrestSensor(SensorEntity):
|
||||
_LOGGER.debug("Updating %s sensor", self.name)
|
||||
|
||||
sensor_type = self.entity_description.key
|
||||
if self._attr_unique_id is None:
|
||||
serial_number = self._api.serial_number
|
||||
if serial_number:
|
||||
self._attr_unique_id = f"{serial_number}-{sensor_type}-{self._channel}"
|
||||
|
||||
try:
|
||||
if sensor_type == SENSOR_PTZ_PRESET:
|
||||
self._attr_native_value = self._api.ptz_presets_count
|
||||
|
Loading…
x
Reference in New Issue
Block a user