Skip to content

Commit

Permalink
support for renaming entity id (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-or authored Oct 20, 2024
1 parent 2648587 commit fe8b1b5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
12 changes: 6 additions & 6 deletions custom_components/hikvision_next/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ async def _async_update_data(self):
if event.disabled:
continue
try:
entity_id = ENTITY_ID_FORMAT.format(event.unique_id)
data[entity_id] = await self.isapi.get_event_enabled_state(event)
_id = ENTITY_ID_FORMAT.format(event.unique_id)
data[_id] = await self.isapi.get_event_enabled_state(event)
except Exception as ex: # pylint: disable=broad-except
self.isapi.handle_exception(ex, f"Cannot fetch state for {event.id}")

Expand All @@ -55,18 +55,18 @@ async def _async_update_data(self):
if event.disabled:
continue
try:
entity_id = ENTITY_ID_FORMAT.format(event.unique_id)
data[entity_id] = await self.isapi.get_event_enabled_state(event)
_id = ENTITY_ID_FORMAT.format(event.unique_id)
data[_id] = await self.isapi.get_event_enabled_state(event)
except Exception as ex: # pylint: disable=broad-except
self.isapi.handle_exception(ex, f"Cannot fetch state for {event.id}")

# Get output port(s) status
for i in range(1, self.isapi.device_info.output_ports + 1):
try:
entity_id = ENTITY_ID_FORMAT.format(
_id = ENTITY_ID_FORMAT.format(
f"{slugify(self.isapi.device_info.serial_no.lower())}_{i}_alarm_output"
)
data[entity_id] = await self.isapi.get_port_status("output", i)
data[_id] = await self.isapi.get_port_status("output", i)
except Exception as ex: # pylint: disable=broad-except
self.isapi.handle_exception(ex, f"Cannot fetch state for alarm output {i}")

Expand Down
7 changes: 4 additions & 3 deletions custom_components/hikvision_next/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

from .const import (
DOMAIN,
EVENT_IO,
EVENTS_COORDINATOR,
HOLIDAY_MODE,
SECONDARY_COORDINATOR,
EVENT_IO,
)
from .isapi import EventInfo

Expand Down Expand Up @@ -73,7 +73,7 @@ def __init__(self, device_id: int, event: EventInfo, coordinator) -> None:
@property
def is_on(self) -> bool | None:
"""Return True if the binary sensor is on."""
return self.coordinator.data.get(self.entity_id)
return self.coordinator.data.get(self.unique_id)

async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on."""
Expand All @@ -93,6 +93,7 @@ async def async_turn_off(self, **kwargs: Any) -> None:
finally:
await self.coordinator.async_request_refresh()


class NVROutputSwitch(CoordinatorEntity, SwitchEntity):
"""Detection events switch."""

Expand All @@ -114,7 +115,7 @@ def __init__(self, coordinator, port_no: int) -> None:
@property
def is_on(self) -> bool | None:
"""Turn on."""
return self.coordinator.data.get(self.entity_id) == "active"
return self.coordinator.data.get(self.unique_id) == "active"

async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on."""
Expand Down

0 comments on commit fe8b1b5

Please sign in to comment.