8000 Make entity available considering device_state · home-assistant/core@374ebd0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 374ebd0

Browse files
Make entity available considering device_state
1 parent 721d6c7 commit 374ebd0

File tree

5 files changed

+52
-31
lines changed

5 files changed

+52
-31
lines changed

homeassistant/components/lg_thinq/entity.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def __init__(
3434
coordinator: DeviceDataUpdateCoordinator,
3535
entity_description: EntityDescription,
3636
property_id: str,
37-
postfix_id: str | None = None,
3837
) -> None:
3938
"""Initialize an entity."""
4039
super().__init__(coordinator)
@@ -49,11 +48,7 @@ def __init__(
4948
model=f"{coordinator.api.device.model_name} ({self.coordinator.api.device.device_type})",
5049
name=coordinator.device_name,
5150
)
52-
self._attr_unique_id = (
53-
f"{coordinator.unique_id}_{self.property_id}"
54-
if postfix_id is None
55-
else f"{coordinator.unique_id}_{self.property_id}_{postfix_id}"
56-
)
51+
self._attr_unique_id = f"{coordinator.unique_id}_{self.property_id}"
5752
if self.location is not None and self.location not in (
5853
Location.MAIN,
5954
Location.OVEN,

homeassistant/components/lg_thinq/number.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ class ThinQNumberEntity(ThinQEntity, NumberEntity):
158158

159159
_attr_mode = NumberMode.BOX
160160

161+
@property
162+
def available(self) -> bool:
163+
"""Return True if entity is available."""
164+
return self.device_state is None or (
165+
self.device_state.device_is_on and self.device_state.remote_control_enabled
166+
)
167+
161168
def _update_status(self) -> None:
162169
"""Update status itself."""
163170
super()._update_status()

homeassistant/components/lg_thinq/select.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ def __init__(
178178

179179
self._attr_options = self.data.options if self.data.options is not None else []
180180

181+
@property
182+
def available(self) -> bool:
183+
"""Return True if entity is available."""
184+
return self.device_state is None or (
185+
self.device_state.device_is_on and self.device_state.remote_control_enabled
186+
)
187+
181188
def _update_status(self) -> None:
182189
"""Update status itself."""
183190
super()._update_status()

homeassistant/components/lg_thinq/sensor.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -564,13 +564,6 @@ def __init__(
564564
if entity_description.device_class == SensorDeviceClass.ENUM:
565565
self._attr_options = self.data.options
566566

567-
self._device_state: str | None = None
568-
self._device_state_id = (
569-
ThinQProperty.CURRENT_STATE
570-
if self.location is None
571-
else f"{self.location}_{ThinQProperty.CURRENT_STATE}"
572-
)
573-
574567
def _update_status(self) -> None:
575568
"""Update status itself."""
576569
super()._update_status()
@@ -581,15 +574,16 @@ def _update_status(self) -> None:
581574
local_now = datetime.now(
582575
tz=dt_util.get_time_zone(self.coordinator.hass.config.time_zone)
583576
)
584-
self._device_state = (
585-
self.coordinator.data[self._device_state_id].value
586-
if self._device_state_id in self.coordinator.data
587-
else None
588-
)
589577
if value in [0, None, time.min] or (
590-
self._device_state == "power_off"
578+
self.device_state is not None
579+
and self.device_state.state in ["initial", "end", "power_off"]
591580
and self.entity_description.key
592-
in [TimerProperty.REMAIN, TimerProperty.TOTAL]
581+
in [
582+
TimerProperty.RELATIVE_TO_START_WM,
583+
TimerProperty.RELATIVE_TO_STOP_WM,
584+
TimerProperty.REMAIN,
585+
TimerProperty.TOTAL,
586+
]
593587
):
594588
# Reset to None when power_off
595589
value = None

homeassistant/components/lg_thinq/switch.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,26 +204,44 @@ def _update_status(self) -> None:
204204
"""Update status itself."""
205205
super()._update_status()
206206

207-
if (key := self.entity_description.on_key) is not None:
208-
self._attr_is_on = self.data.value == key
209-
else:
210-
self._attr_is_on = self.data.is_on
211-
212207
_LOGGER.debug(
213-
"[%s:%s] update status: %s -> %s",
208+
"[%s:%s] update status: value=%s, is_on=%s",
214209
self.coordinator.device_name,
215210
self.property_id,
216-
self.data.is_on,
211+
self.data.value,
217212
self.is_on,
218213
)
219214

215+
@property
216+
def is_on(self) -> bool | None:
217+
"""Return the state of the switch."""
218+
if self.device_state is not None:
219+
return self.device_state.device_is_on
220+
221+
if (key := self.entity_description.on_key) is not None:
222+
return self.data.value == key
223+
224+
return self.data.is_on
225+
226+
@property
227+
def available(self) -> bool:
228+
"""Return True if entity is available."""
229+
return (
230+
self.device_state is None
231+
or self.is_on
232+
or (
233+
self.device_state.remote_control_enabled
234+
and self.device_state.power_on_enabled
235+
)
236+
)
237+
220238
async def async_turn_on(self, **kwargs: Any) -> None:
221239
"""Turn on the switch."""
222240
_LOGGER.debug(
223-
"[%s:%s] async_turn_on id: %s",
241+
"[%s:%s] async_turn_on: on_key=%s",
224242
self.coordinator.device_name,
225-
self.name,
226243
self.property_id,
244+
self.entity_description.on_key,
227245
)
228246
if (on_command := self.entity_description.on_key) is not None:
229247
await self.async_call_api(
@@ -237,10 +255,10 @@ async def async_turn_on(self, **kwargs: Any) -> None:
237255
async def async_turn_off(self, **kwargs: Any) -> None:
238256
"""Turn off the switch."""
239257
_LOGGER.debug(
240-
"[%s:%s] async_turn_off id: %s",
258+
"[%s:%s] async_turn_off: off_key=%s",
241259
self.coordinator.device_name,
242-
self.name,
243260
self.property_id,
261+
self.entity_description.off_key,
244262
)
245263
if (off_command := self.entity_description.off_key) is not None:
246264
await self.async_call_api(

0 commit comments

Comments
 (0)
0