1 """Provides a switch for Home Connect."""
7 from homeconnect.api
import HomeConnectError
14 from .
import HomeConnectConfigEntry, get_dict_from_home_connect_error
16 APPLIANCES_WITH_PROGRAMS,
28 REFRIGERATION_DISPENSER,
29 REFRIGERATION_SUPERMODEFREEZER,
30 REFRIGERATION_SUPERMODEREFRIGERATOR,
31 SVE_TRANSLATION_PLACEHOLDER_APPLIANCE_NAME,
32 SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID,
33 SVE_TRANSLATION_PLACEHOLDER_SETTING_KEY,
34 SVE_TRANSLATION_PLACEHOLDER_VALUE,
36 from .entity
import HomeConnectDevice, HomeConnectEntity
38 _LOGGER = logging.getLogger(__name__)
43 key=BSH_CHILD_LOCK_STATE,
44 translation_key=
"child_lock",
47 key=
"ConsumerProducts.CoffeeMaker.Setting.CupWarmer",
48 translation_key=
"cup_warmer",
51 key=REFRIGERATION_SUPERMODEFREEZER,
52 translation_key=
"freezer_super_mode",
55 key=REFRIGERATION_SUPERMODEREFRIGERATOR,
56 translation_key=
"refrigerator_super_mode",
59 key=
"Refrigeration.Common.Setting.EcoMode",
60 translation_key=
"eco_mode",
63 key=
"Cooking.Oven.Setting.SabbathMode",
64 translation_key=
"sabbath_mode",
67 key=
"Refrigeration.Common.Setting.SabbathMode",
68 translation_key=
"sabbath_mode",
71 key=
"Refrigeration.Common.Setting.VacationMode",
72 translation_key=
"vacation_mode",
75 key=
"Refrigeration.Common.Setting.FreshMode",
76 translation_key=
"fresh_mode",
79 key=REFRIGERATION_DISPENSER,
80 translation_key=
"dispenser_enabled",
83 key=
"Refrigeration.Common.Setting.Door.AssistantFridge",
84 translation_key=
"door_assistant_fridge",
87 key=
"Refrigeration.Common.Setting.Door.AssistantFreezer",
88 translation_key=
"door_assistant_freezer",
95 entry: HomeConnectConfigEntry,
96 async_add_entities: AddEntitiesCallback,
98 """Set up the Home Connect switch."""
101 """Get a list of entities."""
102 entities: list[SwitchEntity] = []
103 for device
in entry.runtime_data.devices:
104 if device.appliance.type
in APPLIANCES_WITH_PROGRAMS:
105 with contextlib.suppress(HomeConnectError):
106 programs = device.appliance.get_programs_available()
110 for program
in programs
115 for description
in SWITCHES
116 if description.key
in device.appliance.status
125 """Generic switch class for Home Connect Binary Settings."""
128 """Turn on setting."""
132 await self.
hasshass.async_add_executor_job(
135 except HomeConnectError
as err:
138 translation_domain=DOMAIN,
139 translation_key=
"turn_on",
140 translation_placeholders={
142 SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID: self.
entity_identity_id,
143 SVE_TRANSLATION_PLACEHOLDER_SETTING_KEY: self.
bsh_keybsh_key,
151 """Turn off setting."""
155 await self.
hasshass.async_add_executor_job(
158 except HomeConnectError
as err:
159 _LOGGER.error(
"Error while trying to turn off: %s", err)
162 translation_domain=DOMAIN,
163 translation_key=
"turn_off",
164 translation_placeholders={
166 SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID: self.
entity_identity_id,
167 SVE_TRANSLATION_PLACEHOLDER_SETTING_KEY: self.
bsh_keybsh_key,
175 """Update the switch's status."""
182 "Updated %s, new state: %s",
189 """Switch class for Home Connect."""
191 def __init__(self, device: HomeConnectDevice, program_name: str) ->
None:
192 """Initialize the entity."""
193 desc =
" ".join([
"Program", program_name.split(
".")[-1]])
194 if device.appliance.type ==
"WasherDryer":
196 [
"Program", program_name.split(
".")[-3], program_name.split(
".")[-1]]
199 self.
_attr_name_attr_name = f
"{device.appliance.name} {desc}"
205 """Start the program."""
206 _LOGGER.debug(
"Tried to turn on program %s", self.
program_nameprogram_name)
208 await self.
hasshass.async_add_executor_job(
211 except HomeConnectError
as err:
213 translation_domain=DOMAIN,
214 translation_key=
"start_program",
215 translation_placeholders={
223 """Stop the program."""
224 _LOGGER.debug(
"Tried to stop program %s", self.
program_nameprogram_name)
226 await self.
hasshass.async_add_executor_job(self.
devicedevice.appliance.stop_program)
227 except HomeConnectError
as err:
229 translation_domain=DOMAIN,
230 translation_key=
"stop_program",
231 translation_placeholders={
239 """Update the switch's status."""
240 state = self.
devicedevice.appliance.status.get(BSH_ACTIVE_PROGRAM, {})
241 if state.get(ATTR_VALUE) == self.
program_nameprogram_name:
245 _LOGGER.debug(
"Updated, new state: %s", self.
_attr_is_on_attr_is_on)
249 """Power switch class for Home Connect."""
251 power_off_state: str |
None
253 def __init__(self, device: HomeConnectDevice) ->
None:
254 """Initialize the entity."""
260 power_state := device.appliance.status.get(BSH_POWER_STATE, {}).
get(
263 )
and power_state
in [BSH_POWER_OFF, BSH_POWER_STANDBY]:
267 """Add the entity to the hass instance."""
269 if not hasattr(self,
"power_off_state"):
273 """Switch the device on."""
274 _LOGGER.debug(
"Tried to switch on %s", self.
namename)
276 await self.
hasshass.async_add_executor_job(
277 self.
devicedevice.appliance.set_setting, BSH_POWER_STATE, BSH_POWER_ON
279 except HomeConnectError
as err:
282 translation_domain=DOMAIN,
283 translation_key=
"power_on",
284 translation_placeholders={
286 SVE_TRANSLATION_PLACEHOLDER_APPLIANCE_NAME: self.
devicedevice.appliance.name,
292 """Switch the device off."""
293 if not hasattr(self,
"power_off_state"):
295 translation_domain=DOMAIN,
296 translation_key=
"unable_to_retrieve_turn_off",
297 translation_placeholders={
298 SVE_TRANSLATION_PLACEHOLDER_APPLIANCE_NAME: self.
devicedevice.appliance.name
304 translation_domain=DOMAIN,
305 translation_key=
"turn_off_not_supported",
306 translation_placeholders={
307 SVE_TRANSLATION_PLACEHOLDER_APPLIANCE_NAME: self.
devicedevice.appliance.name
310 _LOGGER.debug(
"tried to switch off %s", self.
namename)
312 await self.
hasshass.async_add_executor_job(
313 self.
devicedevice.appliance.set_setting,
317 except HomeConnectError
as err:
320 translation_domain=DOMAIN,
321 translation_key=
"power_off",
322 translation_placeholders={
324 SVE_TRANSLATION_PLACEHOLDER_APPLIANCE_NAME: self.
devicedevice.appliance.name,
325 SVE_TRANSLATION_PLACEHOLDER_VALUE: self.
power_off_statepower_off_state,
331 """Update the switch's status."""
333 self.
devicedevice.appliance.status.get(BSH_POWER_STATE, {}).
get(ATTR_VALUE)
338 hasattr(self,
"power_off_state")
339 and self.
devicedevice.appliance.status.get(BSH_POWER_STATE, {}).
get(ATTR_VALUE)
343 elif self.
devicedevice.appliance.status.get(BSH_OPERATION_STATE, {}).
get(
346 "BSH.Common.EnumType.OperationState.Ready",
347 "BSH.Common.EnumType.OperationState.DelayedStart",
348 "BSH.Common.EnumType.OperationState.Run",
349 "BSH.Common.EnumType.OperationState.Pause",
350 "BSH.Common.EnumType.OperationState.ActionRequired",
351 "BSH.Common.EnumType.OperationState.Aborting",
352 "BSH.Common.EnumType.OperationState.Finished",
356 self.
devicedevice.appliance.status.get(BSH_OPERATION_STATE, {}).
get(ATTR_VALUE)
357 ==
"BSH.Common.EnumType.OperationState.Inactive"
362 _LOGGER.debug(
"Updated, new state: %s", self.
_attr_is_on_attr_is_on)
365 """Fetch the power off state."""
367 data = await self.
hasshass.async_add_executor_job(
368 self.
devicedevice.appliance.get, f
"/settings/{self.bsh_key}"
370 except HomeConnectError
as err:
371 _LOGGER.error(
"An error occurred: %s", err)
374 allowed_values := data.get(ATTR_CONSTRAINTS, {}).
get(ATTR_ALLOWED_VALUES)
378 if BSH_POWER_OFF
in allowed_values:
380 elif BSH_POWER_STANDBY
in allowed_values:
None async_entity_update(self)
bool _attr_has_entity_name
None async_turn_on(self, **Any kwargs)
None async_fetch_power_off_state(self)
None async_turn_off(self, **Any kwargs)
None __init__(self, HomeConnectDevice device)
None async_added_to_hass(self)
None __init__(self, HomeConnectDevice device, str program_name)
None async_turn_on(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
str|UndefinedType|None name(self)
None async_turn_off(self, **Any kwargs)
web.Response get(self, web.Request request, str config_key)
None async_setup_entry(HomeAssistant hass, HomeConnectConfigEntry entry, AddEntitiesCallback async_add_entities)
dict[str, Any] get_dict_from_home_connect_error(api.HomeConnectError err)
list[OneWireSwitch] get_entities(OneWireHub onewire_hub)