mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 07:07:28 +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."""
|
"""Initialize entity."""
|
||||||
self._signal_name = name
|
self._signal_name = name
|
||||||
self._api = device.api
|
self._api = device.api
|
||||||
|
self._channel = 0 # Used in unique id, reserved for future use
|
||||||
self.entity_description: AmcrestSensorEntityDescription = entity_description
|
self.entity_description: AmcrestSensorEntityDescription = entity_description
|
||||||
|
|
||||||
self._attr_name = f"{name} {entity_description.name}"
|
self._attr_name = f"{name} {entity_description.name}"
|
||||||
@ -192,6 +193,9 @@ class AmcrestBinarySensor(BinarySensorEntity):
|
|||||||
if not (self._api.available or self.is_on):
|
if not (self._api.available or self.is_on):
|
||||||
return
|
return
|
||||||
_LOGGER.debug(_UPDATE_MSG, self.name)
|
_LOGGER.debug(_UPDATE_MSG, self.name)
|
||||||
|
|
||||||
|
self._update_unique_id()
|
||||||
|
|
||||||
if self._api.available:
|
if self._api.available:
|
||||||
# Send a command to the camera to test if we can still communicate with it.
|
# 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
|
# Override of Http.command() in __init__.py will set self._api.available
|
||||||
@ -205,6 +209,8 @@ class AmcrestBinarySensor(BinarySensorEntity):
|
|||||||
return
|
return
|
||||||
_LOGGER.debug(_UPDATE_MSG, self.name)
|
_LOGGER.debug(_UPDATE_MSG, self.name)
|
||||||
|
|
||||||
|
self._update_unique_id()
|
||||||
|
|
||||||
event_code = self.entity_description.event_code
|
event_code = self.entity_description.event_code
|
||||||
if event_code is None:
|
if event_code is None:
|
||||||
_LOGGER.error("Binary sensor %s event code not set", self.name)
|
_LOGGER.error("Binary sensor %s event code not set", self.name)
|
||||||
@ -215,6 +221,15 @@ class AmcrestBinarySensor(BinarySensorEntity):
|
|||||||
except AmcrestError as error:
|
except AmcrestError as error:
|
||||||
log_update_error(_LOGGER, "update", self.name, "binary sensor", 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:
|
async def async_on_demand_update(self) -> None:
|
||||||
"""Update state."""
|
"""Update state."""
|
||||||
if self.entity_description.key == _ONLINE_KEY:
|
if self.entity_description.key == _ONLINE_KEY:
|
||||||
|
@ -77,6 +77,7 @@ class AmcrestSensor(SensorEntity):
|
|||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self._signal_name = name
|
self._signal_name = name
|
||||||
self._api = device.api
|
self._api = device.api
|
||||||
|
self._channel = 0 # Used in unique id, reserved for future use
|
||||||
self._unsub_dispatcher: Callable[[], None] | None = None
|
self._unsub_dispatcher: Callable[[], None] | None = None
|
||||||
|
|
||||||
self._attr_name = f"{name} {description.name}"
|
self._attr_name = f"{name} {description.name}"
|
||||||
@ -94,6 +95,11 @@ class AmcrestSensor(SensorEntity):
|
|||||||
_LOGGER.debug("Updating %s sensor", self.name)
|
_LOGGER.debug("Updating %s sensor", self.name)
|
||||||
|
|
||||||
sensor_type = self.entity_description.key
|
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:
|
try:
|
||||||
if sensor_type == SENSOR_PTZ_PRESET:
|
if sensor_type == SENSOR_PTZ_PRESET:
|
||||||
self._attr_native_value = self._api.ptz_presets_count
|
self._attr_native_value = self._api.ptz_presets_count
|
||||||
|
Loading…
x
Reference in New Issue
Block a user