Coverage for custom_components/appliance_autonotify/platforms.py: 100%

53 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-28 12:19 +0000

1"""What's known of each integration an appliance can come through. 

2 

3Integrations are taken to behave alike for the same type of appliance, so an appliance of an integration that isn't 

4known here is still found and watched by what its entities do. A profile is only for where an integration names 

5things its own way, or has more to it than others, and to say whether it's been tried on a real appliance yet.""" 

6 

7from __future__ import annotations 

8 

9from collections.abc import Iterable, Mapping 

10from dataclasses import dataclass, field 

11from typing import Final, Literal 

12 

13from .const import ( 

14 APPLIANCE_TYPES, 

15 CYCLE_KEY, 

16 FINISH_TIME_KEY, 

17 KEY_TYPE_PREFIXES, 

18 PROGRESS_KEY, 

19 REMAINING_TIME_KEY, 

20 ROLE_KEY_PREFIXES, 

21 ROLE_KEYS, 

22 STAGE_KEY, 

23 TOTAL_TIME_KEY, 

24) 

25 

26# supported once it's been seen to work with a real appliance in a home, beta when written from its source alone 

27type Status = Literal["supported", "beta"] 

28STATUS_SUPPORTED: Final[Status] = "supported" 

29STATUS_BETA: Final[Status] = "beta" 

30 

31 

32@dataclass(frozen=True, eq=False) 

33class PlatformProfile: 

34 """An integration, by its domain, as the platform of its entities, None for any not known here""" 

35 

36 platform: str | None 

37 status: Status = STATUS_SUPPORTED 

38 # what an entity does, by the translation key the integration gives it, ahead of the ones of every integration 

39 role_keys: Mapping[str, str] = field(default_factory=dict) 

40 role_key_prefixes: Mapping[str, str] = field(default_factory=dict) 

41 # the type of appliance, by how the integration names its programs, or else its entities 

42 program_types: Mapping[str, str] = field(default_factory=dict) 

43 key_types: Mapping[str, str] = field(default_factory=dict) 

44 # the states of a cycle as the integration has them, by the states of every integration they mean, for one 

45 # whose cycle state says more than whether it's running, and states it doesn't know left as they are 

46 cycle_states: Mapping[str, str] = field(default_factory=dict) 

47 # whether the cycle state also says what it's doing within its cycle, for an integration with no stage of its own 

48 stage_from_cycle: bool = False 

49 # what else an appliance must have for its cycle state to be one, for an integration that gives every device the 

50 # same cycle state, a fridge's as much as a washer's 

51 cycle_needs: tuple[str, ...] = () 

52 # energy sensors, by translation key, that count only the program under way, going back to nothing as the next 

53 # one starts, so what one says at the end is what the cycle used, rather than how far it has gone up since it began 

54 program_meter_keys: frozenset[str] = frozenset() 

55 # the types of appliance seen to work, for one in beta, its others still yet to be tried 

56 supported_types: frozenset[str] = frozenset() 

57 # where to tell of how it went, for one in beta 

58 issue_url: str | None = None 

59 

60 @property 

61 def beta(self) -> bool: 

62 """Whether the integration is in beta, though some types of appliance through it may have been seen to work""" 

63 return self.status == STATUS_BETA 

64 

65 def beta_for(self, appliance_type: str | None) -> bool: 

66 """Whether an appliance of this type, through this integration, is yet to be seen to work""" 

67 return self.status == STATUS_BETA and appliance_type not in self.supported_types 

68 

69 def role_key(self, translation_key: str | None) -> str | None: 

70 """What an entity does, as the translation key Home Connect gives an entity that does the same""" 

71 if translation_key is None: 

72 return None 

73 if translation_key in self.role_keys: 

74 return self.role_keys[translation_key] 

75 if role := _by_prefix(self.role_key_prefixes, translation_key): 

76 return role 

77 if translation_key in ROLE_KEYS: 

78 return ROLE_KEYS[translation_key] 

79 return _by_prefix(ROLE_KEY_PREFIXES, translation_key) or translation_key 

80 

81 def cycle_state(self, state: str) -> str: 

82 """A state of the cycle as every integration has it""" 

83 return self.cycle_states.get(state, state) 

84 

85 def appliance_type(self, programs: Iterable[str], translation_keys: Iterable[str]) -> str | None: 

86 """The type of appliance by its programs, or failing that, for an integration with several types, its 

87 entities, by this integration's names before those of every integration""" 

88 programs = list(programs) 

89 translation_keys = list(translation_keys) 

90 for prefixes, names in ( 

91 (self.program_types, programs), 

92 (APPLIANCE_TYPES, programs), 

93 (self.key_types, translation_keys), 

94 (KEY_TYPE_PREFIXES, translation_keys), 

95 ): 

96 if kind := next((kind for prefix, kind in prefixes.items() if any(n.startswith(prefix) for n in names)), None): 

97 return kind 

98 return None 

99 

100 

101def _by_prefix(prefixes: Mapping[str, str], key: str) -> str | None: 

102 return next((role for prefix, role in prefixes.items() if key.startswith(prefix)), None) 

103 

104 

105GENERIC: Final[PlatformProfile] = PlatformProfile(None) 

106 

107# the states of an LG ThinQ washer, dryer or dishwasher going through its cycle, each of which is its stage too, up to 

108# a dryer's wrinkle care, tumbling its load now and then once it's done, which is over by its stage as for others 

109THINQ_RUNNING: Final[tuple[str, ...]] = ( 

110 "running", 

111 "detecting", 

112 "checking_turbidity", 

113 "display_loadsize", 

114 "detergent_amount", 

115 "dispensing", 

116 "prewash", 

117 "soaking", 

118 "add_drain", 

119 "rinsing", 

120 "spinning", 

121 "softening", 

122 "steam_softening", 

123 "drying", 

124 "night_dry", 

125 "cooling", 

126 "cool_down", 

127 "end_cooling", 

128 "refreshing", 

129 "steam", 

130 "sterilize", 

131 "wrinkle_care", 

132) 

133 

134PROFILES: Final[dict[str, PlatformProfile]] = { 

135 profile.platform: profile 

136 for profile in ( 

137 PlatformProfile("home_connect"), 

138 PlatformProfile("smartthings"), 

139 PlatformProfile("localthings"), 

140 # Home Connect Local, the same appliances as Home Connect, on the local network rather than through the 

141 # cloud, with its entities named for their platform, and its programs named for the BSH keys without the 

142 # underscores Home Connect puts between words 

143 PlatformProfile( 

144 "homeconnect_ws", 

145 status=STATUS_BETA, 

146 role_keys={ 

147 "sensor_operation_state": CYCLE_KEY, 

148 "sensor_program_progress": PROGRESS_KEY, 

149 "sensor_remaining_program_time": REMAINING_TIME_KEY, 

150 "sensor_active_program": "active_program", 

151 "select_program": "selected_program", 

152 # what a dishwasher is doing within its cycle, as Home Connect doesn't report it 

153 "sensor_program_phase": STAGE_KEY, 

154 }, 

155 program_types={ 

156 "laundrycare_washer_program_": "washer", 

157 "laundrycare_washerdryer_program_": "washer_dryer", 

158 "laundrycare_dryer_program_": "dryer", 

159 "consumerproducts_coffeemaker_program_": "coffee_maker", 

160 "consumerproducts_cleaningrobot_program_": "cleaning_robot", 

161 "heatingventilationairconditioning_airconditioner_program_": "air_conditioner", 

162 }, 

163 # a dishwasher seen through a whole cycle, its washers, dryers and others yet to be 

164 supported_types=frozenset({"dishwasher"}), 

165 issue_url="https://github.com/rhizomatics/appliance_autonotify/issues/28", 

166 ), 

167 # LG ThinQ, whose one state says both whether a washer, dryer or dishwasher is running and what it's doing, 

168 # with when it will finish and how long it takes, but nothing of how far along it is 

169 PlatformProfile( 

170 "lg_thinq", 

171 status=STATUS_BETA, 

172 role_keys={ 

173 "current_state": CYCLE_KEY, 

174 "remain": FINISH_TIME_KEY, 

175 "total": TOTAL_TIME_KEY, 

176 "current_dish_washing_course": "active_program", 

177 }, 

178 cycle_states={ 

179 **dict.fromkeys(THINQ_RUNNING, "run"), 

180 "pause": "pause", 

181 "paused": "pause", 

182 # waiting for the rinse to be let go on with 

183 "rinse_hold": "actionrequired", 

184 **dict.fromkeys(("end", "complete", "done", "running_end", "cleaning_is_done"), "finished"), 

185 **dict.fromkeys(("power_off", "initial", "initializing", "standby", "sleep"), "inactive"), 

186 # switched on and waiting, as an appliance just finished may be 

187 "power_on": "ready", 

188 **dict.fromkeys(("reserved", "reservation"), "delayedstart"), 

189 **dict.fromkeys(("error", "power_fail"), "error"), 

190 }, 

191 stage_from_cycle=True, 

192 # a washer and a dryer seen through whole cycles, its dishwasher yet to be 

193 supported_types=frozenset({"washer", "dryer"}), 

194 issue_url="https://github.com/rhizomatics/appliance_autonotify/issues/27", 

195 ), 

196 # Miele, whose status every appliance has, with the same states, so an appliance has a cycle only where it has 

197 # a program phase too, which fridges, hoods and hobs don't 

198 PlatformProfile( 

199 "miele", 

200 status=STATUS_BETA, 

201 role_keys={ 

202 "status": CYCLE_KEY, 

203 "program_phase": STAGE_KEY, 

204 "program_id": "active_program", 

205 "finish": FINISH_TIME_KEY, 

206 }, 

207 cycle_states={ 

208 "in_use": "run", 

209 "pause": "pause", 

210 "rinse_hold": "actionrequired", 

211 "program_ended": "finished", 

212 "off": "inactive", 

213 "on": "ready", 

214 "programmed": "ready", 

215 "waiting_to_start": "delayedstart", 

216 "failure": "error", 

217 # stopped part way, so no end to tell of 

218 "program_interrupted": "aborting", 

219 }, 

220 cycle_needs=(STAGE_KEY,), 

221 # what the program under way has used, which may still say what the one before did as the next starts 

222 program_meter_keys=frozenset({"energy_consumption"}), 

223 ), 

224 ) 

225 if profile.platform is not None 

226} 

227 

228 

229def profile_for(platform: str | None) -> PlatformProfile: 

230 """The profile of an integration, or the one for any integration not known here""" 

231 return PROFILES.get(platform or "", GENERIC)