Skip to content

Commit

Permalink
Reworked CardInfo entity to use DataCoordinator
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Jan 23, 2024
1 parent 50ca981 commit 79c6f45
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 139 deletions.
20 changes: 10 additions & 10 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@ KeyboardInterrupt
- [ ] Integration icon
- https://developers.home-assistant.io/docs/dev_101_states/

- [ ] Options flow
- [x] Options flow
- [x] ~~Use unique_id to delete controller~~
- [ ] Use unique_id to delete door
- [x] ~~Use unique_id to delete door~~

- [ ] DataCoordinator
- [x] Fetch data on initialisation
- [ ] parallelize requests
- [ ] store data in self.data
- [x] contexts + idx
- coordinator.async_set_updated_data(data)
- https://developers.home-assistant.io/docs/integration_fetching_data/

- [x] Fetch data on initialisation
- [ ] get controllers from ConfigEntry
- [x] store data in self.data
- [x] async_write_ha_state
- [x] ControllerInfo
- [ ] ControllerDateTime
Expand All @@ -40,9 +37,12 @@ KeyboardInterrupt
- [x] DoorOpen
- [x] DoorLock
- [x] DoorButton
- [ ] CardInfo
- [x] CardInfo
- [ ] CardHolder
- [ ] etc.
- [ ] CardStartDate
- [ ] CardEndDate
- [ ] CardPermission
- [ ] CardPIN
- [ ] events

- [ ] Controller
Expand Down
43 changes: 24 additions & 19 deletions custom_components/uhppoted/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
from datetime import date
import logging

from homeassistant.core import callback
from homeassistant.components.sensor import SensorEntity
from homeassistant.components.date import DateEntity
from homeassistant.components.switch import SwitchEntity
from homeassistant.components.text import TextEntity
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import ATTR_AVAILABLE
from .const import ATTR_CONTROLLER_SERIAL_NUMBER
from .const import ATTR_DOOR_CONTROLLER
from .const import ATTR_DOOR_NUMBER
Expand All @@ -28,16 +31,15 @@
_LOGGER = logging.getLogger(__name__)


class CardInfo(SensorEntity):
class CardInfo(CoordinatorEntity, SensorEntity):
_attr_icon = 'mdi:card-account-details'
_attr_has_entity_name: True

def __init__(self, u, unique_id, card, name):
super().__init__()
def __init__(self, coordinator, unique_id, card, name):
super().__init__(coordinator, context=int(f'{card}'))

_LOGGER.debug(f'card {card}')

self.driver = u
self.card = int(f'{card}')

self._unique_id = unique_id
Expand Down Expand Up @@ -85,32 +87,35 @@ def state(self) -> Optional[str]:

@property
def extra_state_attributes(self) -> Dict[str, Any]:
permissions = f"','.join(self._permissions)" if self._permissions else None
permissions = ','.join(self._permissions) if self._permissions else None
return {
ATTR_CARD_HOLDER: self._cardholder,
ATTR_CARD_STARTDATE: self._start_date,
ATTR_CARD_ENDDATE: self._end_date,
ATTR_CARD_PERMISSIONS: permissions,
}

@callback
def _handle_coordinator_update(self) -> None:
self._update()
self.async_write_ha_state()

async def async_update(self):
self._update()

def _update(self):
_LOGGER.debug(f'card:{self.card} state')
try:
start_date = None
end_date = None
for controller in self.driver['controllers']:
response = self.driver['api'].get_card(controller, self.card)
idx = self.card

if response.controller == controller and response.card_number == self.card:
if not start_date or response.start_date < start_date:
start_date = response.start_date

if not end_date or response.end_date > end_date:
end_date = response.end_date

self._start_date = start_date
self._end_date = end_date
self._available = True
if idx not in self.coordinator.data:
self._available = False
else:
state = self.coordinator.data[idx]
self._start_date = state[ATTR_CARD_STARTDATE]
self._end_date = state[ATTR_CARD_ENDDATE]
self._permissions = state[ATTR_CARD_PERMISSIONS]
self._available = state[ATTR_AVAILABLE]

except (Exception):
self._available = False
Expand Down
19 changes: 19 additions & 0 deletions custom_components/uhppoted/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from .const import CONF_DOOR_ID
from .const import CONF_DOOR_CONTROLLER
from .const import CONF_DOOR_NUMBER

from .const import CONF_CARDS
from .const import CONF_CARD_UNIQUE_ID
from .const import CONF_CARD_NUMBER
Expand Down Expand Up @@ -341,3 +342,21 @@ def default_card_end_date():
day = calendar.monthrange(end_date.year, end_date.month)[1]

return datetime.date(year, month, day)


def resolve(options, acl):
controllers = options[CONF_CONTROLLERS]
doors = options[CONF_DOORS]
permissions = set()

for u in controllers:
controller = u[CONF_CONTROLLER_ID]
serial_no = int(f'{u[CONF_CONTROLLER_SERIAL_NUMBER]}')
if serial_no in acl:
for d in doors:
door = d[CONF_DOOR_ID]
door_no = int(f'{d[CONF_DOOR_NUMBER]}')
if d[CONF_DOOR_CONTROLLER] == controller and door_no in acl[serial_no]:
permissions.add(door)

return sorted(list(permissions))
1 change: 1 addition & 0 deletions custom_components/uhppoted/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
ATTR_DOOR_BUTTON = 'door_button'
ATTR_DOOR_LOCK = 'door_lock'

ATTR_CARD = 'card'
ATTR_CARD_HOLDER = 'cardholder'
ATTR_CARD_STARTDATE = 'start_date'
ATTR_CARD_ENDDATE = 'end_date'
Expand Down
Loading

0 comments on commit 79c6f45

Please sign in to comment.