utils.py 701 B

123456789101112131415161718192021222324252627
  1. from decimal import Decimal
  2. from .choices import WirelessChannelChoices
  3. __all__ = (
  4. 'get_channel_attr',
  5. )
  6. def get_channel_attr(channel, attr):
  7. """
  8. Return the specified attribute of a given WirelessChannelChoices value.
  9. """
  10. if channel not in WirelessChannelChoices.values():
  11. raise ValueError(f"Invalid channel value: {channel}")
  12. channel_values = channel.split('-')
  13. attrs = {
  14. 'band': channel_values[0],
  15. 'id': int(channel_values[1]),
  16. 'frequency': Decimal(channel_values[2]),
  17. 'width': Decimal(channel_values[3]),
  18. }
  19. if attr not in attrs:
  20. raise ValueError(f"Invalid channel attribute: {attr}")
  21. return attrs[attr]