Skip to content

Commit 87fd45d

Browse files
authored
Add device_id parameter to ESPHome command calls for sub-device support (home-assistant#148667)
1 parent 1c35aff commit 87fd45d

34 files changed

+458
-135
lines changed

homeassistant/components/esphome/alarm_control_panel.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,49 +100,70 @@ def alarm_state(self) -> AlarmControlPanelState | None:
100100
async def async_alarm_disarm(self, code: str | None = None) -> None:
101101
"""Send disarm command."""
102102
self._client.alarm_control_panel_command(
103-
self._key, AlarmControlPanelCommand.DISARM, code
103+
self._key,
104+
AlarmControlPanelCommand.DISARM,
105+
code,
106+
device_id=self._static_info.device_id,
104107
)
105108

106109
@convert_api_error_ha_error
107110
async def async_alarm_arm_home(self, code: str | None = None) -> None:
108111
"""Send arm home command."""
109112
self._client.alarm_control_panel_command(
110-
self._key, AlarmControlPanelCommand.ARM_HOME, code
113+
self._key,
114+
AlarmControlPanelCommand.ARM_HOME,
115+
code,
116+
device_id=self._static_info.device_id,
111117
)
112118

113119
@convert_api_error_ha_error
114120
async def async_alarm_arm_away(self, code: str | None = None) -> None:
115121
"""Send arm away command."""
116122
self._client.alarm_control_panel_command(
117-
self._key, AlarmControlPanelCommand.ARM_AWAY, code
123+
self._key,
124+
AlarmControlPanelCommand.ARM_AWAY,
125+
code,
126+
device_id=self._static_info.device_id,
118127
)
119128

120129
@convert_api_error_ha_error
121130
async def async_alarm_arm_night(self, code: str | None = None) -> None:
122131
"""Send arm away command."""
123132
self._client.alarm_control_panel_command(
124-
self._key, AlarmControlPanelCommand.ARM_NIGHT, code
133+
self._key,
134+
AlarmControlPanelCommand.ARM_NIGHT,
135+
code,
136+
device_id=self._static_info.device_id,
125137
)
126138

127139
@convert_api_error_ha_error
128140
async def async_alarm_arm_custom_bypass(self, code: str | None = None) -> None:
129141
"""Send arm away command."""
130142
self._client.alarm_control_panel_command(
131-
self._key, AlarmControlPanelCommand.ARM_CUSTOM_BYPASS, code
143+
self._key,
144+
AlarmControlPanelCommand.ARM_CUSTOM_BYPASS,
145+
code,
146+
device_id=self._static_info.device_id,
132147
)
133148

134149
@convert_api_error_ha_error
135150
async def async_alarm_arm_vacation(self, code: str | None = None) -> None:
136151
"""Send arm away command."""
137152
self._client.alarm_control_panel_command(
138-
self._key, AlarmControlPanelCommand.ARM_VACATION, code
153+
self._key,
154+
AlarmControlPanelCommand.ARM_VACATION,
155+
code,
156+
device_id=self._static_info.device_id,
139157
)
140158

141159
@convert_api_error_ha_error
142160
async def async_alarm_trigger(self, code: str | None = None) -> None:
143161
"""Send alarm trigger command."""
144162
self._client.alarm_control_panel_command(
145-
self._key, AlarmControlPanelCommand.TRIGGER, code
163+
self._key,
164+
AlarmControlPanelCommand.TRIGGER,
165+
code,
166+
device_id=self._static_info.device_id,
146167
)
147168

148169

homeassistant/components/esphome/button.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def _on_device_update(self) -> None:
4848
@convert_api_error_ha_error
4949
async def async_press(self) -> None:
5050
"""Press the button."""
51-
self._client.button_command(self._key)
51+
self._client.button_command(self._key, device_id=self._static_info.device_id)
5252

5353

5454
async_setup_entry = partial(

homeassistant/components/esphome/climate.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,24 @@ async def async_set_temperature(self, **kwargs: Any) -> None:
287287
data["target_temperature_low"] = kwargs[ATTR_TARGET_TEMP_LOW]
288288
if ATTR_TARGET_TEMP_HIGH in kwargs:
289289
data["target_temperature_high"] = kwargs[ATTR_TARGET_TEMP_HIGH]
290-
self._client.climate_command(**data)
290+
self._client.climate_command(**data, device_id=self._static_info.device_id)
291291

292292
@convert_api_error_ha_error
293293
async def async_set_humidity(self, humidity: int) -> None:
294294
"""Set new target humidity."""
295-
self._client.climate_command(key=self._key, target_humidity=humidity)
295+
self._client.climate_command(
296+
key=self._key,
297+
target_humidity=humidity,
298+
device_id=self._static_info.device_id,
299+
)
296300

297301
@convert_api_error_ha_error
298302
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
299303
"""Set new target operation mode."""
300304
self._client.climate_command(
301-
key=self._key, mode=_CLIMATE_MODES.from_hass(hvac_mode)
305+
key=self._key,
306+
mode=_CLIMATE_MODES.from_hass(hvac_mode),
307+
device_id=self._static_info.device_id,
302308
)
303309

304310
@convert_api_error_ha_error
@@ -309,7 +315,7 @@ async def async_set_preset_mode(self, preset_mode: str) -> None:
309315
kwargs["custom_preset"] = preset_mode
310316
else:
311317
kwargs["preset"] = _PRESETS.from_hass(preset_mode)
312-
self._client.climate_command(**kwargs)
318+
self._client.climate_command(**kwargs, device_id=self._static_info.device_id)
313319

314320
@convert_api_error_ha_error
315321
async def async_set_fan_mode(self, fan_mode: str) -> None:
@@ -319,13 +325,15 @@ async def async_set_fan_mode(self, fan_mode: str) -> None:
319325
kwargs["custom_fan_mode"] = fan_mode
320326
else:
321327
kwargs["fan_mode"] = _FAN_MODES.from_hass(fan_mode)
322-
self._client.climate_command(**kwargs)
328+
self._client.climate_command(**kwargs, device_id=self._static_info.device_id)
323329

324330
@convert_api_error_ha_error
325331
async def async_set_swing_mode(self, swing_mode: str) -> None:
326332
"""Set new swing mode."""
327333
self._client.climate_command(
328-
key=self._key, swing_mode=_SWING_MODES.from_hass(swing_mode)
334+
key=self._key,
335+
swing_mode=_SWING_MODES.from_hass(swing_mode),
336+
device_id=self._static_info.device_id,
329337
)
330338

331339

homeassistant/components/esphome/cover.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,38 +90,56 @@ def current_cover_tilt_position(self) -> int | None:
9090
@convert_api_error_ha_error
9191
async def async_open_cover(self, **kwargs: Any) -> None:
9292
"""Open the cover."""
93-
self._client.cover_command(key=self._key, position=1.0)
93+
self._client.cover_command(
94+
key=self._key, position=1.0, device_id=self._static_info.device_id
95+
)
9496

9597
@convert_api_error_ha_error
9698
async def async_close_cover(self, **kwargs: Any) -> None:
9799
"""Close cover."""
98-
self._client.cover_command(key=self._key, position=0.0)
100+
self._client.cover_command(
101+
key=self._key, position=0.0, device_id=self._static_info.device_id
102+
)
99103

100104
@convert_api_error_ha_error
101105
async def async_stop_cover(self, **kwargs: Any) -> None:
102106
"""Stop the cover."""
103-
self._client.cover_command(key=self._key, stop=True)
107+
self._client.cover_command(
108+
key=self._key, stop=True, device_id=self._static_info.device_id
109+
)
104110

105111
@convert_api_error_ha_error
106112
async def async_set_cover_position(self, **kwargs: Any) -> None:
107113
"""Move the cover to a specific position."""
108-
self._client.cover_command(key=self._key, position=kwargs[ATTR_POSITION] / 100)
114+
self._client.cover_command(
115+
key=self._key,
116+
position=kwargs[ATTR_POSITION] / 100,
117+
device_id=self._static_info.device_id,
118+
)
109119

110120
@convert_api_error_ha_error
111121
async def async_open_cover_tilt(self, **kwargs: Any) -> None:
112122
"""Open the cover tilt."""
113-
self._client.cover_command(key=self._key, tilt=1.0)
123+
self._client.cover_command(
124+
key=self._key, tilt=1.0, device_id=self._static_info.device_id
125+
)
114126

115127
@convert_api_error_ha_error
116128
async def async_close_cover_tilt(self, **kwargs: Any) -> None:
117129
"""Close the cover tilt."""
118-
self._client.cover_command(key=self._key, tilt=0.0)
130+
self._client.cover_command(
131+
key=self._key, tilt=0.0, device_id=self._static_info.device_id
132+
)
119133

120134
@convert_api_error_ha_error
121135
async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
122136
"""Move the cover tilt to a specific position."""
123137
tilt_position: int = kwargs[ATTR_TILT_POSITION]
124-
self._client.cover_command(key=self._key, tilt=tilt_position / 100)
138+
self._client.cover_command(
139+
key=self._key,
140+
tilt=tilt_position / 100,
141+
device_id=self._static_info.device_id,
142+
)
125143

126144

127145
async_setup_entry = partial(

homeassistant/components/esphome/date.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ def native_value(self) -> date | None:
2828

2929
async def async_set_value(self, value: date) -> None:
3030
"""Update the current date."""
31-
self._client.date_command(self._key, value.year, value.month, value.day)
31+
self._client.date_command(
32+
self._key,
33+
value.year,
34+
value.month,
35+
value.day,
36+
device_id=self._static_info.device_id,
37+
)
3238

3339

3440
async_setup_entry = partial(

homeassistant/components/esphome/datetime.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ def native_value(self) -> datetime | None:
2929

3030
async def async_set_value(self, value: datetime) -> None:
3131
"""Update the current datetime."""
32-
self._client.datetime_command(self._key, int(value.timestamp()))
32+
self._client.datetime_command(
33+
self._key, int(value.timestamp()), device_id=self._static_info.device_id
34+
)
3335

3436

3537
async_setup_entry = partial(

homeassistant/components/esphome/fan.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async def _async_set_percentage(self, percentage: int | None) -> None:
7171
ORDERED_NAMED_FAN_SPEEDS, percentage
7272
)
7373
data["speed"] = named_speed
74-
self._client.fan_command(**data)
74+
self._client.fan_command(**data, device_id=self._static_info.device_id)
7575

7676
async def async_turn_on(
7777
self,
@@ -85,24 +85,36 @@ async def async_turn_on(
8585
@convert_api_error_ha_error
8686
async def async_turn_off(self, **kwargs: Any) -> None:
8787
"""Turn off the fan."""
88-
self._client.fan_command(key=self._key, state=False)
88+
self._client.fan_command(
89+
key=self._key, state=False, device_id=self._static_info.device_id
90+
)
8991

9092
@convert_api_error_ha_error
9193
async def async_oscillate(self, oscillating: bool) -> None:
9294
"""Oscillate the fan."""
93-
self._client.fan_command(key=self._key, oscillating=oscillating)
95+
self._client.fan_command(
96+
key=self._key,
97+
oscillating=oscillating,
98+
device_id=self._static_info.device_id,
99+
)
94100

95101
@convert_api_error_ha_error
96102
async def async_set_direction(self, direction: str) -> None:
97103
"""Set direction of the fan."""
98104
self._client.fan_command(
99-
key=self._key, direction=_FAN_DIRECTIONS.from_hass(direction)
105+
key=self._key,
106+
direction=_FAN_DIRECTIONS.from_hass(direction),
107+
device_id=self._static_info.device_id,
100108
)
101109

102110
@convert_api_error_ha_error
103111
async def async_set_preset_mode(self, preset_mode: str) -> None:
104112
"""Set the preset mode of the fan."""
105-
self._client.fan_command(key=self._key, preset_mode=preset_mode)
113+
self._client.fan_command(
114+
key=self._key,
115+
preset_mode=preset_mode,
116+
device_id=self._static_info.device_id,
117+
)
106118

107119
@property
108120
@esphome_state_property

homeassistant/components/esphome/light.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ async def async_turn_on(self, **kwargs: Any) -> None:
280280
# (fewest capabilities set)
281281
data["color_mode"] = _least_complex_color_mode(color_modes)
282282

283-
self._client.light_command(**data)
283+
self._client.light_command(**data, device_id=self._static_info.device_id)
284284

285285
@convert_api_error_ha_error
286286
async def async_turn_off(self, **kwargs: Any) -> None:
@@ -290,7 +290,7 @@ async def async_turn_off(self, **kwargs: Any) -> None:
290290
data["flash_length"] = FLASH_LENGTHS[kwargs[ATTR_FLASH]]
291291
if ATTR_TRANSITION in kwargs:
292292
data["transition_length"] = kwargs[ATTR_TRANSITION]
293-
self._client.light_command(**data)
293+
self._client.light_command(**data, device_id=self._static_info.device_id)
294294

295295
@property
296296
@esphome_state_property

homeassistant/components/esphome/lock.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,24 @@ def is_jammed(self) -> bool:
6565
@convert_api_error_ha_error
6666
async def async_lock(self, **kwargs: Any) -> None:
6767
"""Lock the lock."""
68-
self._client.lock_command(self._key, LockCommand.LOCK)
68+
self._client.lock_command(
69+
self._key, LockCommand.LOCK, device_id=self._static_info.device_id
70+
)
6971

7072
@convert_api_error_ha_error
7173
async def async_unlock(self, **kwargs: Any) -> None:
7274
"""Unlock the lock."""
7375
code = kwargs.get(ATTR_CODE)
74-
self._client.lock_command(self._key, LockCommand.UNLOCK, code)
76+
self._client.lock_command(
77+
self._key, LockCommand.UNLOCK, code, device_id=self._static_info.device_id
78+
)
7579

7680
@convert_api_error_ha_error
7781
async def async_open(self, **kwargs: Any) -> None:
7882
"""Open the door latch."""
79-
self._client.lock_command(self._key, LockCommand.OPEN)
83+
self._client.lock_command(
84+
self._key, LockCommand.OPEN, device_id=self._static_info.device_id
85+
)
8086

8187

8288
async_setup_entry = partial(

homeassistant/components/esphome/media_player.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ async def async_play_media(
132132
media_id = proxy_url
133133

134134
self._client.media_player_command(
135-
self._key, media_url=media_id, announcement=announcement
135+
self._key,
136+
media_url=media_id,
137+
announcement=announcement,
138+
device_id=self._static_info.device_id,
136139
)
137140

138141
async def async_will_remove_from_hass(self) -> None:
@@ -214,29 +217,44 @@ async def async_browse_media(
214217
@convert_api_error_ha_error
215218
async def async_set_volume_level(self, volume: float) -> None:
216219
"""Set volume level, range 0..1."""
217-
self._client.media_player_command(self._key, volume=volume)
220+
self._client.media_player_command(
221+
self._key, volume=volume, device_id=self._static_info.device_id
222+
)
218223

219224
@convert_api_error_ha_error
220225
async def async_media_pause(self) -> None:
221226
"""Send pause command."""
222-
self._client.media_player_command(self._key, command=MediaPlayerCommand.PAUSE)
227+
self._client.media_player_command(
228+
self._key,
229+
command=MediaPlayerCommand.PAUSE,
230+
device_id=self._static_info.device_id,
231+
)
223232

224233
@convert_api_error_ha_error
225234
async def async_media_play(self) -> None:
226235
"""Send play command."""
227-
self._client.media_player_command(self._key, command=MediaPlayerCommand.PLAY)
236+
self._client.media_player_command(
237+
self._key,
238+
command=MediaPlayerCommand.PLAY,
239+
device_id=self._static_info.device_id,
240+
)
228241

229242
@convert_api_error_ha_error
230243
async def async_media_stop(self) -> None:
231244
"""Send stop command."""
232-
self._client.media_player_command(self._key, command=MediaPlayerCommand.STOP)
245+
self._client.media_player_command(
246+
self._key,
247+
command=MediaPlayerCommand.STOP,
248+
device_id=self._static_info.device_id,
249+
)
233250

234251
@convert_api_error_ha_error
235252
async def async_mute_volume(self, mute: bool) -> None:
236253
"""Mute the volume."""
237254
self._client.media_player_command(
238255
self._key,
239256
command=MediaPlayerCommand.MUTE if mute else MediaPlayerCommand.UNMUTE,
257+
device_id=self._static_info.device_id,
240258
)
241259

242260

0 commit comments

Comments
 (0)