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:
Sean Vig 2021-09-17 01:48:17 -04:00 committed by GitHub
parent 175f207d28
commit 6d99a7a730
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

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

View File

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