Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: Using ENUMs #222

Open
fuatakgun opened this issue Dec 29, 2022 · 3 comments
Open

Discussion: Using ENUMs #222

fuatakgun opened this issue Dec 29, 2022 · 3 comments

Comments

@fuatakgun
Copy link
Member

let's create an ENUM to map simple fields (non property) to respective key.

Current code;

vehicle.car_battery_percentage = get_child_value(state, "vehicleStatus.battery.batSoc")
vehicle.engine_is_running = get_child_value(state, "vehicleStatus.engine")

New Code

class FieldDefinition(Enum):
  car_battery_percentage= "vehicleStatus.battery.batSoc"
  engine_is_running="vehicleStatus.engine"

for key, value in FieldDefinition:
  setattr(vehicle, key, get_child_value(value))
@jwillemsen
Copy link
Contributor

Now this integration looks for specific keys and uses those, what about using an opposite approach, loop through the returned data and for each field look for the key to use, if there is no mapping for a specific field a message can be logged that a not supported field was found which can be added pretty easily.

@cdnninja
Copy link
Collaborator

cdnninja commented Feb 1, 2024

Would that not mean Home assistant items would need to exist in the API like state classes?

@jwillemsen
Copy link
Contributor

Not sure exactly, but when the lower layers just store the raw data the higher layer sensor can just retrieve the value he is associated with. I am using this for the daikin integration, see for example https://github.com/jwillemsen/daikin_residential_altherma/blob/master/custom_components/daikin_residential_altherma/sensor.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants