Skip to content

Commit

Permalink
Restructured configuration to support multiple doors
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Dec 14, 2023
1 parent 117d84d commit b23cef9
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 44 deletions.
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ KeyboardInterrupt
- [x] Find controllers with `get-all-controllers`
- [ ] Rework to configure multiple controllers, doors, etc
- [x] Restructure configuration for list of controllers
- [ ] Restructure configuration for list of doors
- [ ] Flatten and validate list
- [x] Restructure configuration for list of doors
- [ ] Flatten and validate lists
- [ ] Select controller/door to configure
- [ ] Loop until done
- [ ] Use mapping for controller and door selectors
Expand Down
13 changes: 12 additions & 1 deletion custom_components/uhppoted/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
from .const import CONF_BROADCAST_ADDR
from .const import CONF_LISTEN_ADDR
from .const import CONF_DEBUG

from .const import CONF_CONTROLLERS
from .const import CONF_CONTROLLER_ID
from .const import CONF_CONTROLLER_SERIAL_NUMBER
from .const import CONF_CONTROLLER_ADDR

from .const import CONF_DOORS
from .const import CONF_DOOR_ID
from .const import CONF_DOOR_CONTROLLER
from .const import CONF_DOOR_NUMBER
Expand Down Expand Up @@ -175,7 +178,15 @@ async def async_step_door(self, user_input: Optional[Dict[str, Any]] = None):
errors["base"] = f'Invalid door number ({user_input[CONF_DOOR_NUMBER]})'

if not errors:
self.options.update(user_input)
v = []
v.append({
CONF_DOOR_ID: user_input[CONF_DOOR_ID],
CONF_DOOR_CONTROLLER: user_input[CONF_DOOR_CONTROLLER],
CONF_DOOR_NUMBER: user_input[CONF_DOOR_NUMBER],
})

self.options.update({CONF_DOORS: v})

return self.async_create_entry(title="uhppoted", data=self.data, options=self.options)

return self.async_show_form(step_id="door", data_schema=schema, errors=errors)
1 change: 1 addition & 0 deletions custom_components/uhppoted/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
CONF_CONTROLLER_SERIAL_NUMBER = 'controller_serial_number'
CONF_CONTROLLER_ADDR = 'controller_address'

CONF_DOORS = 'doors'
CONF_DOOR_ID = 'door_id'
CONF_DOOR_CONTROLLER = 'door_controller'
CONF_DOOR_NUMBER = 'door_number'
Expand Down
1 change: 1 addition & 0 deletions custom_components/uhppoted/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .const import CONF_BROADCAST_ADDR
from .const import CONF_LISTEN_ADDR
from .const import CONF_DEBUG

from .const import CONF_CONTROLLERS
from .const import CONF_CONTROLLER_ID
from .const import CONF_CONTROLLER_SERIAL_NUMBER
Expand Down
27 changes: 15 additions & 12 deletions custom_components/uhppoted/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
from .const import CONF_BROADCAST_ADDR
from .const import CONF_LISTEN_ADDR
from .const import CONF_DEBUG

from .const import CONF_CONTROLLERS
from .const import CONF_CONTROLLER_ID
from .const import CONF_CONTROLLER_SERIAL_NUMBER
from .const import CONF_CONTROLLER_ADDR

from .const import CONF_DOORS
from .const import CONF_DOOR_ID
from .const import CONF_DOOR_CONTROLLER
from .const import CONF_DOOR_NUMBER
Expand All @@ -38,7 +41,6 @@


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback):
config = entry.data
options = entry.options

bind = options[CONF_BIND_ADDR]
Expand All @@ -50,19 +52,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e

entities = []
controllers = options[CONF_CONTROLLERS]
doors = options[CONF_DOORS]

# FIXME
door = options[CONF_DOOR_ID]
# door_controller = options[CONF_DOOR_CONTROLLER] // FIXME
door_no = options[CONF_DOOR_NUMBER]
for c in controllers:
controller = c[CONF_CONTROLLER_ID].strip()
serial_no = c[CONF_CONTROLLER_SERIAL_NUMBER].strip()

for v in controllers:
controller = v[CONF_CONTROLLER_ID].strip()
serial_no = v[CONF_CONTROLLER_SERIAL_NUMBER].strip()
address = v[CONF_CONTROLLER_ADDR].strip()
for d in doors:
door = d[CONF_DOOR_ID].strip()
door_no = d[CONF_DOOR_NUMBER].strip()
door_controller = d[CONF_DOOR_CONTROLLER].strip()

entities.extend([
ControllerDoorDelay(u, controller, serial_no, door, door_no),
])
if door_controller == controller:
entities.extend([
ControllerDoorDelay(u, controller, serial_no, door, door_no),
])

async_add_entities(entities, update_before_add=True)
28 changes: 23 additions & 5 deletions custom_components/uhppoted/options_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
from .const import CONF_BROADCAST_ADDR
from .const import CONF_LISTEN_ADDR
from .const import CONF_DEBUG

from .const import CONF_CONTROLLERS
from .const import CONF_CONTROLLER_ID
from .const import CONF_CONTROLLER_SERIAL_NUMBER
from .const import CONF_CONTROLLER_ADDR

from .const import CONF_DOORS
from .const import CONF_DOOR_ID
from .const import CONF_DOOR_CONTROLLER
from .const import CONF_DOOR_NUMBER
Expand Down Expand Up @@ -118,9 +121,16 @@ async def async_step_controller(self, user_input: Optional[Dict[str, Any]] = Non
return self.async_show_form(step_id="controller", data_schema=schema, errors=errors)

async def async_step_door(self, user_input: Optional[Dict[str, Any]] = None):
name = self.options[CONF_DOOR_ID]
controller = self.options[CONF_DOOR_CONTROLLER]
door = self.options[CONF_DOOR_NUMBER]
name = ''
controller = ''
door_no = ''

if CONF_DOORS in self.options:
for v in self.options[CONF_DOORS]:
name = v[CONF_DOOR_ID]
controller = v[CONF_DOOR_CONTROLLER]
door_no = v[CONF_DOOR_NUMBER]
break

controllers = selector.SelectSelector(
selector.SelectSelectorConfig(options=['Alpha', 'Beta', 'Gamma', 'Delta'],
Expand All @@ -135,7 +145,7 @@ async def async_step_door(self, user_input: Optional[Dict[str, Any]] = None):
schema = vol.Schema({
vol.Required(CONF_DOOR_ID, default=name): str,
vol.Required(CONF_DOOR_CONTROLLER, default=controller): controllers,
vol.Required(CONF_DOOR_NUMBER, default=door): doors,
vol.Required(CONF_DOOR_NUMBER, default=door_no): doors,
})

errors: Dict[str, str] = {}
Expand All @@ -157,7 +167,15 @@ async def async_step_door(self, user_input: Optional[Dict[str, Any]] = None):
errors["base"] = f'Invalid door number ({user_input[CONF_DOOR_NUMBER]})'

if not errors:
self.options.update(user_input)
v = []
v.append({
CONF_DOOR_ID: user_input[CONF_DOOR_ID],
CONF_DOOR_CONTROLLER: user_input[CONF_DOOR_CONTROLLER],
CONF_DOOR_NUMBER: user_input[CONF_DOOR_NUMBER],
})

self.options.update({CONF_DOORS: v})

return self.async_create_entry(title="uhppoted", data=self.options)

return self.async_show_form(step_id="door", data_schema=schema, errors=errors)
26 changes: 15 additions & 11 deletions custom_components/uhppoted/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
from .const import CONF_BROADCAST_ADDR
from .const import CONF_LISTEN_ADDR
from .const import CONF_DEBUG

from .const import CONF_CONTROLLERS
from .const import CONF_CONTROLLER_ID
from .const import CONF_CONTROLLER_SERIAL_NUMBER
from .const import CONF_CONTROLLER_ADDR

from .const import CONF_DOORS
from .const import CONF_DOOR_ID
from .const import CONF_DOOR_CONTROLLER
from .const import CONF_DOOR_NUMBER
Expand All @@ -49,19 +52,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e

entities = []
controllers = options[CONF_CONTROLLERS]
doors = options[CONF_DOORS]

# FIXME
door = options[CONF_DOOR_ID]
# door_controller = options[CONF_DOOR_CONTROLLER] // FIXME
door_no = options[CONF_DOOR_NUMBER]
for c in controllers:
controller = c[CONF_CONTROLLER_ID].strip()
serial_no = c[CONF_CONTROLLER_SERIAL_NUMBER].strip()

for v in controllers:
controller = v[CONF_CONTROLLER_ID].strip()
serial_no = v[CONF_CONTROLLER_SERIAL_NUMBER].strip()
address = v[CONF_CONTROLLER_ADDR].strip()
for d in doors:
door = d[CONF_DOOR_ID].strip()
door_no = d[CONF_DOOR_NUMBER].strip()
door_controller = d[CONF_DOOR_CONTROLLER].strip()

entities.extend([
ControllerDoorMode(u, controller, serial_no, door, door_no),
])
if door_controller == controller:
entities.extend([
ControllerDoorMode(u, controller, serial_no, door, door_no),
])

async_add_entities(entities, update_before_add=True)
34 changes: 21 additions & 13 deletions custom_components/uhppoted/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
from .const import CONF_BROADCAST_ADDR
from .const import CONF_LISTEN_ADDR
from .const import CONF_DEBUG

from .const import CONF_CONTROLLERS
from .const import CONF_CONTROLLER_ID
from .const import CONF_CONTROLLER_SERIAL_NUMBER
from .const import CONF_CONTROLLER_ADDR

from .const import CONF_DOORS
from .const import CONF_DOOR_ID
from .const import CONF_DOOR_CONTROLLER
from .const import CONF_DOOR_NUMBER
Expand Down Expand Up @@ -56,23 +59,28 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e

entities = []
controllers = options[CONF_CONTROLLERS]
doors = options[CONF_DOORS]

# FIXME
door = options[CONF_DOOR_ID]
# door_controller = options[CONF_DOOR_CONTROLLER] // FIXME
door_no = options[CONF_DOOR_NUMBER]

for v in controllers:
controller = v[CONF_CONTROLLER_ID].strip()
serial_no = v[CONF_CONTROLLER_SERIAL_NUMBER].strip()
address = v[CONF_CONTROLLER_ADDR].strip()
for c in controllers:
controller = c[CONF_CONTROLLER_ID].strip()
serial_no = c[CONF_CONTROLLER_SERIAL_NUMBER].strip()
address = c[CONF_CONTROLLER_ADDR].strip()

entities.extend([
ControllerInfo(u, controller, serial_no),
ControllerDoor(u, controller, serial_no, door, door_no),
ControllerDoorOpen(u, controller, serial_no, door, door_no),
ControllerDoorLock(u, controller, serial_no, door, door_no),
ControllerDoorButton(u, controller, serial_no, door, door_no),
])

for d in doors:
door = d[CONF_DOOR_ID].strip()
door_no = d[CONF_DOOR_NUMBER].strip()
door_controller = d[CONF_DOOR_CONTROLLER].strip()

if door_controller == controller:
entities.extend([
ControllerDoor(u, controller, serial_no, door, door_no),
ControllerDoorOpen(u, controller, serial_no, door, door_no),
ControllerDoorLock(u, controller, serial_no, door, door_no),
ControllerDoorButton(u, controller, serial_no, door, door_no),
])

async_add_entities(entities, update_before_add=True)

0 comments on commit b23cef9

Please sign in to comment.