power: store device classes in a dict
This provides a cleaner way of adding new types. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
3d01b0b9da
commit
06bea715a3
|
@ -24,24 +24,25 @@ class PrinterPower:
|
||||||
self.devices = {}
|
self.devices = {}
|
||||||
prefix_sections = config.get_prefix_sections("power")
|
prefix_sections = config.get_prefix_sections("power")
|
||||||
logging.info(f"Power component loading devices: {prefix_sections}")
|
logging.info(f"Power component loading devices: {prefix_sections}")
|
||||||
|
dev_types = {
|
||||||
|
"gpio": GpioDevice,
|
||||||
|
"tplink_smartplug": TPLinkSmartPlug,
|
||||||
|
"tasmota": Tasmota,
|
||||||
|
"shelly": Shelly,
|
||||||
|
"homeseer": HomeSeer,
|
||||||
|
"homeassistant": HomeAssistant,
|
||||||
|
}
|
||||||
try:
|
try:
|
||||||
for section in prefix_sections:
|
for section in prefix_sections:
|
||||||
cfg = config[section]
|
cfg = config[section]
|
||||||
dev_type = cfg.get("type")
|
dev_type = cfg.get("type")
|
||||||
if dev_type == "gpio":
|
dev_class = dev_types.get(dev_type)
|
||||||
dev = GpioDevice(cfg, self.chip_factory)
|
if dev_class is None:
|
||||||
elif dev_type == "tplink_smartplug":
|
|
||||||
dev = TPLinkSmartPlug(cfg)
|
|
||||||
elif dev_type == "tasmota":
|
|
||||||
dev = Tasmota(cfg)
|
|
||||||
elif dev_type == "shelly":
|
|
||||||
dev = Shelly(cfg)
|
|
||||||
elif dev_type == "homeseer":
|
|
||||||
dev = HomeSeer(cfg)
|
|
||||||
elif dev_type == "homeassistant":
|
|
||||||
dev = HomeAssistant(cfg)
|
|
||||||
else:
|
|
||||||
raise config.error(f"Unsupported Device Type: {dev_type}")
|
raise config.error(f"Unsupported Device Type: {dev_type}")
|
||||||
|
elif dev_type == "gpio":
|
||||||
|
dev = dev_class(cfg, self.chip_factory)
|
||||||
|
else:
|
||||||
|
dev = dev_class(cfg)
|
||||||
self.devices[dev.get_name()] = dev
|
self.devices[dev.get_name()] = dev
|
||||||
except Exception:
|
except Exception:
|
||||||
self.chip_factory.close()
|
self.chip_factory.close()
|
||||||
|
|
Loading…
Reference in New Issue