Skip to content

Commit

Permalink
Connect to Z-Wave and move to config_entry_id as key
Browse files Browse the repository at this point in the history
Rename KeymasterLock entities from lock to kmlock
Rename KeymasterLock parent to parent_name
  • Loading branch information
Snuffy2 committed Nov 14, 2024
1 parent ee64808 commit 5aad934
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 328 deletions.
32 changes: 15 additions & 17 deletions custom_components/keymaster/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
config_entry.data[CONF_START],
config_entry.data[CONF_START] + config_entry.data[CONF_SLOTS],
):
code_slots[x] = KeymasterCodeSlot(number=x)
dow_slots: Mapping[int, KeymasterCodeSlotDayOfWeek] = {}
for i, dow in enumerate(
[
Expand All @@ -117,26 +116,26 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
]
):
dow_slots[i] = KeymasterCodeSlotDayOfWeek(
day_of_week_num=1, day_of_week_name=dow
day_of_week_num=i, day_of_week_name=dow
)
code_slots[x] = KeymasterCodeSlot(number=x, accesslimit_day_of_week=dow_slots)

lock = KeymasterLock(
kmlock = KeymasterLock(
lock_name=config_entry.data[CONF_LOCK_NAME],
lock_entity_id=config_entry.data[CONF_LOCK_ENTITY_ID],
keymaster_device_id=device.id,
keymaster_config_entry_id=config_entry.entry_id,
alarm_level_or_user_code_entity_id=config_entry.data[
CONF_ALARM_LEVEL_OR_USER_CODE_ENTITY_ID
],
alarm_type_or_access_control_entity_id=config_entry.data[
CONF_ALARM_TYPE_OR_ACCESS_CONTROL_ENTITY_ID
],
door_sensor_entity_id=config_entry.data[CONF_SENSOR_NAME],
# zwave_js_lock_node = config_entry.data[
# zwave_js_lock_device = config_entry.data[
number_of_code_slots=config_entry.data[CONF_SLOTS],
starting_code_slot=config_entry.data[CONF_START],
code_slots={},
parent=config_entry.data[CONF_PARENT],
code_slots=code_slots,
parent_name=config_entry.data[CONF_PARENT],
)
hass.data[DOMAIN][config_entry.entry_id] = device.id

Expand All @@ -146,7 +145,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
else:
coordinator = hass.data[DOMAIN][COORDINATOR]

await coordinator.add_lock(lock=lock)
await coordinator.add_lock(kmlock=kmlock)

# await coordinator.async_config_entry_first_refresh()
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
Expand All @@ -161,11 +160,11 @@ async def system_health_check(hass: HomeAssistant, config_entry: ConfigEntry) ->
_LOGGER.debug(
f"[system_health_check] hass.data[DOMAIN][config_entry.entry_id]: {hass.data[DOMAIN][config_entry.entry_id]}"
)
lock: KeymasterLock = await coordinator.get_lock_by_device_id(
hass.data[DOMAIN][config_entry.entry_id]
kmlock: KeymasterLock = await coordinator.get_lock_by_config_entry_id(
config_entry.entry_id
)

if async_using_zwave_js(hass=hass, lock=lock):
if async_using_zwave_js(hass=hass, kmlock=kmlock):
hass.data[DOMAIN][INTEGRATION] = "zwave_js"
else:
hass.data[DOMAIN][INTEGRATION] = "unknown"
Expand Down Expand Up @@ -204,9 +203,7 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->

# await async_reload_package_platforms(hass)

await coordinator.delete_lock_by_device_id(
hass.data[DOMAIN][config_entry.entry_id]
)
await coordinator.delete_lock_by_config_entry_id(config_entry.entry_id)

hass.data[DOMAIN].pop(config_entry.entry_id, None)

Expand Down Expand Up @@ -305,10 +302,11 @@ async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> Non
day_of_week_num=1, day_of_week_name=dow
)

lock = KeymasterLock(
kmlock = KeymasterLock(
lock_name=config_entry.data[CONF_LOCK_NAME],
lock_entity_id=config_entry.data[CONF_LOCK_ENTITY_ID],
keymaster_device_id=device.id,
keymaster_config_entry_id=config_entry.entry_id,
alarm_level_or_user_code_entity_id=config_entry.data[
CONF_ALARM_LEVEL_OR_USER_CODE_ENTITY_ID
],
Expand All @@ -321,7 +319,7 @@ async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> Non
number_of_code_slots=config_entry.data[CONF_SLOTS],
starting_code_slot=config_entry.data[CONF_START],
code_slots={},
parent=config_entry.data[CONF_PARENT],
parent_name=config_entry.data[CONF_PARENT],
)
hass.data[DOMAIN][config_entry.entry_id] = device.id

Expand All @@ -331,7 +329,7 @@ async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> Non
else:
coordinator = hass.data[DOMAIN][COORDINATOR]

await coordinator.update_lock(lock=lock)
await coordinator.update_lock(kmlock=kmlock)

if old_slots != new_slots:
async_dispatcher_send(
Expand Down
69 changes: 4 additions & 65 deletions custom_components/keymaster/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Setup config entry."""
coordinator = hass.data[DOMAIN][COORDINATOR]
lock = await coordinator.get_lock_by_config_entry_id(config_entry.entry_id)
if async_using_zwave_js(hass=hass, lock=lock):
kmlock = await coordinator.get_lock_by_config_entry_id(config_entry.entry_id)
if async_using_zwave_js(hass=hass, kmlock=kmlock):
entity = ZwaveJSNetworkReadySensor(
entity_description=KeymasterBinarySensorEntityDescription(
key="binary_sensor.connected",
Expand Down Expand Up @@ -68,14 +68,11 @@ def __init__(
)
self.integration_name = integration_name
self._attr_is_on = False
self._attr_should_poll = False

@callback
def _handle_coordinator_update(self) -> None:
_LOGGER.debug(
f"[Binary Sensor handle_coordinator_update] self.coordinator.data: {self.coordinator.data}"
)

# _LOGGER.debug(f"[Binary Sensor handle_coordinator_update] self.coordinator.data: {self.coordinator.data}")
self._attr_is_on = self._get_property_value()
self.async_write_ha_state()

# @callback
Expand Down Expand Up @@ -108,61 +105,3 @@ def __init__(
integration_name=ZWAVE_JS_DOMAIN,
entity_description=entity_description,
)
self.lock_config_entry_id = None
self._lock_found = True
self._attr_should_poll = True

# async def async_update(self) -> None:
# """Update sensor."""
# if not self.ent_reg:
# self.ent_reg = async_get_entity_registry(self.hass)

# if (
# not self.lock_config_entry_id
# or not self.hass.config_entries.async_get_entry(self.lock_config_entry_id)
# ):
# entity_id = self.primary_lock.lock_entity_id
# lock_ent_reg_entry = self.ent_reg.async_get(entity_id)

# if not lock_ent_reg_entry:
# if self._lock_found:
# self._lock_found = False
# _LOGGER.warning("Can't find your lock %s.", entity_id)
# return

# self.lock_config_entry_id = lock_ent_reg_entry.config_entry_id

# if not self._lock_found:
# _LOGGER.info("Found your lock %s", entity_id)
# self._lock_found = True

# try:
# zwave_entry = self.hass.config_entries.async_get_entry(
# self.lock_config_entry_id
# )
# client = zwave_entry.runtime_data[ZWAVE_JS_DATA_CLIENT]
# except:
# _LOGGER.exception("Can't access Z-Wave JS client.")
# self._attr_is_on = False
# return

# network_ready = bool(
# client.connected and client.driver and client.driver.controller
# )

# # If network_ready and self._attr_is_on are both true or both false, we don't need
# # to do anything since there is nothing to update.
# if not network_ready ^ self.is_on:
# return

# self.async_set_is_on_property(network_ready, False)

# # If we just turned the sensor on, we need to get the latest lock
# # nodes and devices
# if self.is_on:
# await async_update_zwave_js_nodes_and_devices(
# self.hass,
# self.lock_config_entry_id,
# self.primary_lock,
# self.child_locks,
# )
8 changes: 4 additions & 4 deletions custom_components/keymaster/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ class KeyMasterOptionsFlow(config_entries.OptionsFlow):

def __init__(self, config_entry: config_entries.ConfigEntry):
"""Initialize."""
self.config_entry = config_entry
self._config_entry = config_entry

def _get_unique_name_error(self, user_input):
"""Check if name is unique, returning dictionary error if so."""
# If lock name has changed, make sure new name isn't already being used
# otherwise show an error
if self.config_entry.unique_id != user_input[CONF_LOCK_NAME]:
if self._config_entry.unique_id != user_input[CONF_LOCK_NAME]:
for entry in self.hass.config_entries.async_entries(DOMAIN):
if entry.unique_id == user_input[CONF_LOCK_NAME]:
return {CONF_LOCK_NAME: "same_name"}
Expand All @@ -123,8 +123,8 @@ async def async_step_init(
"init",
"",
user_input,
self.config_entry.data,
self.config_entry.entry_id,
self._config_entry.data,
self._config_entry.entry_id,
)


Expand Down
Loading

0 comments on commit 5aad934

Please sign in to comment.