|
| 1 | +import importlib |
| 2 | + |
| 3 | +from homeassistant.components.zha.core.const import RadioType |
| 4 | + |
| 5 | + |
| 6 | +DOMAIN = "custom_zha_radios" |
| 7 | + |
| 8 | + |
| 9 | +def patch_enum_member(target_enum, name, value): |
| 10 | + """ |
| 11 | + Don't tell anybody you called this function. Every line is a hack. |
| 12 | + """ |
| 13 | + |
| 14 | + member = target_enum._member_type_.__new__(target_enum) |
| 15 | + member._name_ = name |
| 16 | + member._value_ = value |
| 17 | + |
| 18 | + if not isinstance(value, tuple): |
| 19 | + args = (value,) |
| 20 | + else: |
| 21 | + args = value |
| 22 | + |
| 23 | + target_enum.__init__(member, *args) |
| 24 | + |
| 25 | + target_enum._member_names_.append(name) |
| 26 | + target_enum._member_map_[name] = member |
| 27 | + target_enum._value2member_map_[value] = member |
| 28 | + type.__setattr__(target_enum, name, member) |
| 29 | + |
| 30 | + |
| 31 | +def setup(hass, config): |
| 32 | + """ |
| 33 | + Injects modules from `custom_zha_radios` into `zha`. For example: |
| 34 | +
|
| 35 | + custom_zha_radios: |
| 36 | + znp: |
| 37 | + module: zigpy_znp.zigbee.application |
| 38 | + description: TI CC13x2, CC26x2, and ZZH |
| 39 | +
|
| 40 | + """ |
| 41 | + |
| 42 | + custom_names = list(config[DOMAIN].keys()) |
| 43 | + original_names = list(RadioType._member_names_) |
| 44 | + |
| 45 | + for name, obj in config[DOMAIN].items(): |
| 46 | + module = importlib.import_module(obj["module"]) |
| 47 | + app = module.ControllerApplication |
| 48 | + description = obj["description"] |
| 49 | + |
| 50 | + patch_enum_member(RadioType, name, (description, app)) |
| 51 | + |
| 52 | + # New keys are moved up top |
| 53 | + RadioType._member_names_ = custom_names + original_names |
| 54 | + |
| 55 | + return True |
0 commit comments