33from collections .abc import Callable
44from dataclasses import dataclass
55
6- from spotifyaio .models import AudioFeatures
6+ from spotifyaio .models import AudioFeatures , Key
77
88from homeassistant .components .sensor import (
99 SensorDeviceClass ,
@@ -25,14 +25,28 @@ class SpotifyAudioFeaturesSensorEntityDescription(SensorEntityDescription):
2525 value_fn : Callable [[AudioFeatures ], float | str | None ]
2626
2727
28+ KEYS : dict [Key , str ] = {
29+ Key .C : "C" ,
30+ Key .C_SHARP_D_FLAT : "C♯/D♭" ,
31+ Key .D : "D" ,
32+ Key .D_SHARP_E_FLAT : "D♯/E♭" ,
33+ Key .E : "E" ,
34+ Key .F : "F" ,
35+ Key .F_SHARP_G_FLAT : "F♯/G♭" ,
36+ Key .G : "G" ,
37+ Key .G_SHARP_A_FLAT : "G♯/A♭" ,
38+ Key .A : "A" ,
39+ Key .A_SHARP_B_FLAT : "A♯/B♭" ,
40+ Key .B : "B" ,
41+ }
42+
43+ KEY_OPTIONS = list (KEYS .values ())
44+
45+
2846def _get_key (audio_features : AudioFeatures ) -> str | None :
2947 if audio_features .key is None :
3048 return None
31- key_name = audio_features .key .name
32- base = key_name [0 ]
33- if len (key_name ) > 1 :
34- base = f"{ base } ♯"
35- return base
49+ return KEYS [audio_features .key ]
3650
3751
3852AUDIO_FEATURE_SENSORS : tuple [SpotifyAudioFeaturesSensorEntityDescription , ...] = (
@@ -119,7 +133,7 @@ def _get_key(audio_features: AudioFeatures) -> str | None:
119133 key = "key" ,
120134 translation_key = "key" ,
121135 device_class = SensorDeviceClass .ENUM ,
122- options = [ "C" , "C♯" , "D" , "D♯" , "E" , "F" , "F♯" , "G" , "G♯" , "A" , "A♯" , "B" ] ,
136+ options = KEY_OPTIONS ,
123137 value_fn = _get_key ,
124138 entity_registry_enabled_default = False ,
125139 ),
0 commit comments