Skip to content

Commit 44f8e3b

Browse files
created generic power_on/power_off functions
added optimistic_state configuration parameter added generic update_entity callback fixed light class logging messages
1 parent 64bd8e8 commit 44f8e3b

File tree

5 files changed

+92
-186
lines changed

5 files changed

+92
-186
lines changed

custom_components/smartir/climate.py

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ def __init__(self, hass: HomeAssistant, config: ConfigType, device_data):
177177
self._support_flags = self._support_flags | ClimateEntityFeature.SWING_MODE
178178
self._swing_mode = self._swing_modes[0]
179179

180+
self.__update_entity = self._async_update_hvac_action
181+
180182
async def async_added_to_hass(self):
181183
"""Run when entity about to be added."""
182184
await super().async_added_to_hass()
@@ -493,52 +495,10 @@ async def _send_command(
493495
_LOGGER.debug("Found '%s' operation mode command.", off_mode)
494496
await self._controller.send(self._commands[off_mode])
495497
await asyncio.sleep(self._delay)
496-
elif "off" in self._commands.keys() and isinstance(
497-
self._commands["off"], str
498-
):
499-
if (
500-
"on" in self._commands.keys()
501-
and isinstance(self._commands["on"], str)
502-
and self._commands["on"] == self._commands["off"]
503-
and self._state == STATE_OFF
504-
):
505-
# prevent to resend 'off' command if same as 'on' and device is already off
506-
_LOGGER.debug(
507-
"As 'on' and 'off' commands are identical and device is already in requested '%s' state, skipping sending '%s' command",
508-
self._state,
509-
"off",
510-
)
511-
else:
512-
_LOGGER.debug("Found 'off' operation mode command.")
513-
await self._controller.send(self._commands["off"])
514-
await asyncio.sleep(self._delay)
515498
else:
516-
_LOGGER.error(
517-
"Missing device IR code for 'off' or '%s' operation mode.",
518-
off_mode,
519-
)
520-
return
499+
await self._async_power_off()
521500
else:
522-
if "on" in self._commands.keys() and isinstance(
523-
self._commands["on"], str
524-
):
525-
if (
526-
"off" in self._commands.keys()
527-
and isinstance(self._commands["off"], str)
528-
and self._commands["off"] == self._commands["on"]
529-
and self._state == STATE_ON
530-
):
531-
# prevent to resend 'on' command if same as 'off' and device is already on
532-
_LOGGER.debug(
533-
"As 'on' and 'off' commands are identical and device is already in requested '%s' state, skipping sending '%s' command",
534-
self._state,
535-
"on",
536-
)
537-
else:
538-
# if on code is not present, the on bit can be still set later in the all operation codes
539-
_LOGGER.debug("Found 'on' operation mode command.")
540-
await self._controller.send(self._commands["on"])
541-
await asyncio.sleep(self._delay)
501+
await self._async_power_on()
542502

543503
commands = self._commands
544504
if hvac_mode in commands.keys():
@@ -703,9 +663,7 @@ async def _send_command(
703663
self.async_write_ha_state()
704664

705665
except Exception as e:
706-
_LOGGER.exception(
707-
"Exception raised in the in the send_command '%s'", e
708-
)
666+
_LOGGER.exception("Exception raised in the in the send_command '%s'", e)
709667

710668
async def _async_temp_sensor_changed(
711669
self, event: Event[EventStateChangedData]

custom_components/smartir/fan.py

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -202,49 +202,9 @@ async def send_command(self, state, speed, direction, oscillate):
202202

203203
try:
204204
if state == STATE_OFF:
205-
if "off" in self._commands.keys() and isinstance(
206-
self._commands["off"], str
207-
):
208-
if (
209-
"on" in self._commands.keys()
210-
and isinstance(self._commands["on"], str)
211-
and self._commands["on"] == self._commands["off"]
212-
and self._state == STATE_OFF
213-
):
214-
# prevent to resend 'off' command if same as 'on' and device is already off
215-
_LOGGER.debug(
216-
"As 'on' and 'off' commands are identical and device is already in requested '%s' state, skipping sending '%s' command",
217-
self._state,
218-
"off",
219-
)
220-
else:
221-
_LOGGER.debug("Found 'off' operation mode command.")
222-
await self._controller.send(self._commands["off"])
223-
await asyncio.sleep(self._delay)
224-
else:
225-
_LOGGER.error("Missing device IR code for 'off' mode.")
226-
return
205+
await self._async_power_off()
227206
else:
228-
if "on" in self._commands.keys() and isinstance(
229-
self._commands["on"], str
230-
):
231-
if (
232-
"off" in self._commands.keys()
233-
and isinstance(self._commands["off"], str)
234-
and self._commands["off"] == self._commands["on"]
235-
and self._state == STATE_ON
236-
):
237-
# prevent to resend 'on' command if same as 'off' and device is already on
238-
_LOGGER.debug(
239-
"As 'on' and 'off' commands are identical and device is already in requested '%s' state, skipping sending '%s' command",
240-
self._state,
241-
"on",
242-
)
243-
else:
244-
# if on code is not present, the on bit can be still set later in the all operation codes
245-
_LOGGER.debug("Found 'on' operation mode command.")
246-
await self._controller.send(self._commands["on"])
247-
await asyncio.sleep(self._delay)
207+
await self._async_power_on()
248208

249209
if oscillate:
250210
if "oscillate" in self._commands:

custom_components/smartir/light.py

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -169,49 +169,9 @@ async def send_command(self, state, brightness, color_temp):
169169

170170
try:
171171
if state == STATE_OFF:
172-
if "off" in self._commands.keys() and isinstance(
173-
self._commands["off"], str
174-
):
175-
if (
176-
"on" in self._commands.keys()
177-
and isinstance(self._commands["on"], str)
178-
and self._commands["on"] == self._commands["off"]
179-
and self._state == STATE_OFF
180-
):
181-
# prevent to resend 'off' command if same as 'on' and device is already off
182-
_LOGGER.debug(
183-
"As 'on' and 'off' commands are identical and device is already in requested '%s' state, skipping sending '%s' command",
184-
self._state,
185-
"off",
186-
)
187-
else:
188-
_LOGGER.debug("Found 'off' operation mode command.")
189-
await self._controller.send(self._commands["off"])
190-
await asyncio.sleep(self._delay)
191-
else:
192-
_LOGGER.error("Missing device IR code for 'off' mode.")
193-
return
172+
await self._async_power_off()
194173
else:
195-
if "on" in self._commands.keys() and isinstance(
196-
self._commands["on"], str
197-
):
198-
if (
199-
"off" in self._commands.keys()
200-
and isinstance(self._commands["off"], str)
201-
and self._commands["off"] == self._commands["on"]
202-
and self._state == STATE_ON
203-
):
204-
# prevent to resend 'on' command if same as 'off' and device is already on
205-
_LOGGER.debug(
206-
"As 'on' and 'off' commands are identical and device is already in requested '%s' state, skipping sending '%s' command",
207-
self._state,
208-
"on",
209-
)
210-
else:
211-
# if on code is not present, the on bit can be still set later in the all operation codes
212-
_LOGGER.debug("Found 'on' operation mode command.")
213-
await self._controller.send(self._commands["on"])
214-
await asyncio.sleep(self._delay)
174+
await self._async_power_on()
215175

216176
if color_temp is not None:
217177
if "colorTemperature" in self._commands and isinstance(
@@ -221,7 +181,8 @@ async def send_command(self, state, brightness, color_temp):
221181
color_temp, self._color_temp_list
222182
)
223183
_LOGGER.debug(
224-
"Changing color temp from '%s'K to '%s'K using found remote command for {final_color_temp}K",
184+
"Changing color temp from '%s'K to '%s'K using command found in 'colorTemperature' commands",
185+
self._color_temp,
225186
color_temp,
226187
)
227188
await self._controller.send(
@@ -253,7 +214,12 @@ async def send_command(self, state, brightness, color_temp):
253214
steps = len(self._color_temp_list)
254215

255216
_LOGGER.debug(
256-
"Changing color temp from '%s'K index {old_color_temp} to {target}K index {new_color_temp}"
217+
"Changing color temp from '%s'K index '%s' to '%s'K index '%s' using command '%s'",
218+
self._color_temp,
219+
old_color_temp_index,
220+
color_temp,
221+
new_color_temp_index,
222+
cmd,
257223
)
258224
while steps > 0:
259225
steps -= 1
@@ -273,8 +239,9 @@ async def send_command(self, state, brightness, color_temp):
273239
brightness, self._brightness_list
274240
)
275241
_LOGGER.debug(
276-
"Changing color temp from '%s'K to '%s'K using found remote command for {final_color_temp}K",
277-
color_temp,
242+
"Changing brightness from '%s' to '%s' using command found in 'brightness' commands",
243+
self._brightness,
244+
brightness,
278245
)
279246
await self._controller.send(
280247
self._commands["brightness"][str(brightness)]
@@ -305,7 +272,12 @@ async def send_command(self, state, brightness, color_temp):
305272
steps = len(self._brightness_list)
306273

307274
_LOGGER.debug(
308-
"Changing color temp from '%s'K index {old_color_temp} to {target}K index {new_color_temp}"
275+
"Changing brightness from '%s'K index '%s' to '%s'K index '%s' using command '%s'",
276+
self._brightness,
277+
old_brightness_index,
278+
brightness,
279+
new_brightness_index,
280+
cmd,
309281
)
310282
while steps > 0:
311283
steps -= 1

custom_components/smartir/media_player.py

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -223,49 +223,9 @@ async def send_command(self, state, commands):
223223

224224
try:
225225
if state == STATE_OFF:
226-
if "off" in self._commands.keys() and isinstance(
227-
self._commands["off"], str
228-
):
229-
if (
230-
"on" in self._commands.keys()
231-
and isinstance(self._commands["on"], str)
232-
and self._commands["on"] == self._commands["off"]
233-
and self._state == STATE_OFF
234-
):
235-
# prevent to resend 'off' command if same as 'on' and device is already off
236-
_LOGGER.debug(
237-
"As 'on' and 'off' commands are identical and device is already in requested '%s' state, skipping sending '%s' command",
238-
self._state,
239-
"off",
240-
)
241-
else:
242-
_LOGGER.debug("Found 'off' operation mode command.")
243-
await self._controller.send(self._commands["off"])
244-
await asyncio.sleep(self._delay)
245-
else:
246-
_LOGGER.error("Missing device IR code for 'off' mode.")
247-
return
226+
await self._async_power_off()
248227
else:
249-
if "on" in self._commands.keys() and isinstance(
250-
self._commands["on"], str
251-
):
252-
if (
253-
"off" in self._commands.keys()
254-
and isinstance(self._commands["off"], str)
255-
and self._commands["off"] == self._commands["on"]
256-
and self._state == STATE_ON
257-
):
258-
# prevent to resend 'on' command if same as 'off' and device is already on
259-
_LOGGER.debug(
260-
"As 'on' and 'off' commands are identical and device is already in requested '%s' state, skipping sending '%s' command",
261-
self._state,
262-
"on",
263-
)
264-
else:
265-
# if on code is not present, the on bit can be still set later in the all operation codes
266-
_LOGGER.debug("Found 'on' operation mode command.")
267-
await self._controller.send(self._commands["on"])
268-
await asyncio.sleep(self._delay)
228+
await self._async_power_on()
269229

270230
for keys in commands:
271231
data = self._commands

0 commit comments

Comments
 (0)