def load(config_raw: t.Dict[str, t.Any] = {}) -> bool: """ Initializes the module """ conf_options = updateConfOptions(CONF_OPTIONS) conf_options.move_to_end("regex:.+") config_data = parse_config(config_raw, conf_options, log) del config_raw if config_data: log.debug("Config loaded") CONFIG.update(config_data) del config_data for id in [key for key in CONFIG if isinstance(CONFIG[key], dict)]: array_config = CONFIG.pop(id) if not validate_id(id): log.warn("'%s' is not a valid ID and will be ignored", id) else: array_object = getArray(id, array_config, log) if array_object: arrays[id] = array_object nodes[id] = array_object.get_node() else: log.error("Failed to configure LED array '%s'", id) return True else: log.error("Error loading config") return False
def build_pin(id: str, pin_config: t.Dict[str, t.Any], index: t.Optional[int] = None) -> t.Union[Pin, None]: if not validate_id(id): log.warn("'%s' is not a valid ID and will be ignored", id) return None clazz = getPin(pin_config[CONF_KEY_PIN_MODE]) if clazz: if index is None: pin = pin_config[CONF_KEY_PIN] name = pin_config[CONF_KEY_NAME].format(pin=pin, pin_id=id, index="") else: if isinstance(pin_config[CONF_KEY_NAME], list): name = pin_config[CONF_KEY_NAME][index] elif "{index}" in pin_config[CONF_KEY_NAME]: name = pin_config[CONF_KEY_NAME] else: name = f"{pin_config[CONF_KEY_NAME]} {index + pin_config[CONF_KEY_FIRST_INDEX]}" pin = pin_config[CONF_KEY_PIN][index] name = name.format(pin=pin, pin_id=id, index=index + pin_config[CONF_KEY_FIRST_INDEX]) id = f"{id}-{index + pin_config[CONF_KEY_FIRST_INDEX]}" clazz = clazz(pin, CONFIG[CONF_KEY_MODE], id, name, pin_config) else: log.error( "Pin mode %s in '%s' is not supported", pin_config[CONF_KEY_PIN_MODE].name, id, ) return clazz
def _setup_pin( self, pin: int, id: str, name: str, pin_config: t.Dict[str, t.Any], ) -> None: """ Setup pin for given options """ if not validate_id(id): self._log.warn("'%s' is not a valid ID and will be ignored", id) elif self._pins[pin] is not None: self._log.warn( "Duplicate configuration for GP%02d '%s' found in '%s' for %s '%s' " "will be ignored, pin already configured under '%s'", pin, id, self._id, self._device, self._name, self._pins[pin].name, ) elif pin > self._pin_max: self._log.warn( "Found pin GP%02d in '%s' for %s '%s' but highest pin for device is GP%02d", pin, id, self._id, self._name, self._pin_max, ) else: self._pins[pin] = Pin( pin=pin, id=id, name=name, direction=pin_config[CONF_KEY_DIRECTION], resistor=pin_config[CONF_KEY_RESISTOR], interrupt=pin_config[CONF_KEY_INTERRUPT], invert=pin_config[CONF_KEY_INVERT], initial=pin_config[CONF_KEY_INITIAL], device=self, ) self._pin_from_path[id] = pin self._log.debug( "Configured pin GP%02d '%s' on %s '%s' with options: %s", pin, self._pins[pin].name, self.device, self.name, { "ID": id, CONF_KEY_DIRECTION: self._pins[pin].direction.name, CONF_KEY_RESISTOR: self._pins[pin].resistor.name, CONF_KEY_INVERT: self._pins[pin].invert, CONF_KEY_INITIAL: TEXT_STATE[pin_config[CONF_KEY_INITIAL]], }, )
def build_device( device_id: str, device_config: t.Dict[str, t.Any] = {}, device_name: t.Optional[str] = None, address: t.Optional[str] = None, index: t.Optional[int] = None, ) -> t.Union[OneWireDevice, None]: if not validate_id(device_id): log.warn("'%s' is not a valid ID and will be ignored", device_id) return None valid_address = ow_bus.validateAddress( address or device_config.get(CONF_KEY_ADDRESS, None) ) if valid_address: device_type = device.getDeviceTypeByFamily(valid_address) clazz = device.getDevice(valid_address) if clazz: device_id = ( device_id if index is None else f"{device_id}-{index + device_config[CONF_KEY_FIRST_INDEX]}" ) device_name = ( device_name # use address for devices discovered on bus scan or f"{device_config.get(CONF_KEY_NAME, valid_address)}" f"{'' if index is None else f' {index + device_config[CONF_KEY_FIRST_INDEX]}'}" ) device_name = device_name.format( device_id=device_id, device_type=device_type, address=valid_address ) log.debug( "Configuring %s '%s' at address '%s'", device_type, device_name, valid_address, ) return clazz( id=device_id, name=device_name, device=device_type, address=valid_address, bus=ow_bus, device_config=device_config, ) else: log.warn("%s '%s' is not a supported device", device_type, device_name) else: log.warn( "Device '%s' has an invalid address '%s'", device_name, address or device_config.get(CONF_KEY_ADDRESS, ""), ) return None
def build_device( device_id: str, device_config: t.Dict[str, t.Any]) -> t.Union[I2CDevice, None]: from smbus2.smbus2 import SMBus if not validate_id(device_id): log.warn("'%s' is not a valid ID and will be ignored", device_id) return None device: str = device_config[CONF_KEY_DEVICE] bus = validateBus(device_config[CONF_KEY_BUS]) address = validateAddress(device_config[CONF_KEY_ADDRESS]) name: str = device_config[CONF_KEY_NAME].format(device_id=device_id, address=address, device=device.upper()) clazz = getDeviceClass(device) if bus is not None: if bus not in buses: buses[bus] = SMBus() else: log.error("Failed to configure %s '%s', bus is invalid.", device.upper(), name) return None if clazz: if address is None: log.warn( "%s '%s' has an invalid address '%s'", device.upper(), name, device_config[CONF_KEY_ADDRESS], ) return None log.debug( "Configuring %s '%s' at address 0x%02x on bus '%s'", device.upper(), name, address, bus, ) return clazz( id=device_id, name=name, device=device, address=address, bus=buses[bus], bus_path=bus, device_config=device_config, ) else: log.warn("%s '%s' is not a supported device", device.upper(), name) return None