Home Assistant Unofficial Reference 2024.12.1
model.py
Go to the documentation of this file.
1 """Data representation for fitbit API responses."""
2 
3 from collections.abc import Mapping
4 from dataclasses import dataclass
5 from typing import Any
6 
7 from .const import CONF_CLOCK_FORMAT, CONF_MONITORED_RESOURCES, FitbitScope
8 
9 
10 @dataclass
12  """User profile from the Fitbit API response."""
13 
14  encoded_id: str
15  """The ID representing the Fitbit user."""
16 
17  display_name: str
18  """The name shown when the user's friends look at their Fitbit profile."""
19 
20  locale: str | None
21  """The locale defined in the user's Fitbit account settings."""
22 
23 
24 @dataclass
26  """Device from the Fitbit API response."""
27 
28  id: str
29  """The device ID."""
30 
31  device_version: str
32  """The product name of the device."""
33 
34  battery_level: int
35  """The battery level as a percentage."""
36 
37  battery: str
38  """Returns the battery level of the device."""
39 
40  type: str
41  """The type of the device such as TRACKER or SCALE."""
42 
43 
44 @dataclass
46  """Information from the fitbit ConfigEntry data."""
47 
48  clock_format: str | None
49  monitored_resources: set[str] | None
50  scopes: set[FitbitScope]
51 
52  def is_explicit_enable(self, key: str) -> bool:
53  """Determine if entity is enabled by default."""
54  if self.monitored_resources is not None:
55  return key in self.monitored_resources
56  return False
57 
58  def is_allowed_resource(self, scope: FitbitScope | None, key: str) -> bool:
59  """Determine if an entity is allowed to be created."""
60  if self.is_explicit_enableis_explicit_enable(key):
61  return True
62  return scope in self.scopes
63 
64 
65 def config_from_entry_data(data: Mapping[str, Any]) -> FitbitConfig:
66  """Parse the integration config entry into a FitbitConfig."""
67 
68  clock_format = data.get(CONF_CLOCK_FORMAT)
69 
70  # Originally entities were configured explicitly from yaml config. Newer
71  # configurations will infer which entities to enable based on the allowed
72  # scopes the user selected during OAuth. When creating entities based on
73  # scopes, some entities are disabled by default.
74  monitored_resources = data.get(CONF_MONITORED_RESOURCES)
75  fitbit_scopes: set[FitbitScope] = set({})
76  if scopes := data["token"].get("scope"):
77  fitbit_scopes = set({FitbitScope(scope) for scope in scopes.split(" ")})
78  return FitbitConfig(clock_format, monitored_resources, fitbit_scopes)
bool is_allowed_resource(self, FitbitScope|None scope, str key)
Definition: model.py:58
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
FitbitConfig config_from_entry_data(Mapping[str, Any] data)
Definition: model.py:65