diff --git a/homeassistant/components/amcrest/binary_sensor.py b/homeassistant/components/amcrest/binary_sensor.py index 76fdcf100ae..48bc6727585 100644 --- a/homeassistant/components/amcrest/binary_sensor.py +++ b/homeassistant/components/amcrest/binary_sensor.py @@ -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: diff --git a/homeassistant/components/amcrest/sensor.py b/homeassistant/components/amcrest/sensor.py index e6040cfa728..87bb1d5c758 100644 --- a/homeassistant/components/amcrest/sensor.py +++ b/homeassistant/components/amcrest/sensor.py @@ -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