Skip to content

Commit

Permalink
Getting rid of unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
rrooggiieerr committed Dec 20, 2023
1 parent 38a426a commit 07f78ee
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 118 deletions.
31 changes: 7 additions & 24 deletions custom_components/benqprojector/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,62 +86,45 @@ async def async_added_to_hass(self) -> None:
self._attr_available = True

self._attr_source_list = self.coordinator.projector.video_sources

self.async_write_ha_state()
else:
_LOGGER.debug("Projector is not available")
self._attr_available = False

self.async_write_ha_state()

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
updated = False

if self.coordinator.power_status in [
BenQProjector.POWERSTATUS_POWERINGON,
BenQProjector.POWERSTATUS_ON,
]:
volume_level = None
if self.coordinator.volume is not None:
volume_level = self.coordinator.volume
if self._attr_volume_level != volume_level:
self._attr_volume_level = volume_level
updated = True

if self._attr_is_volume_muted != self.coordinator.muted:
self._attr_is_volume_muted = self.coordinator.muted
updated = True

if self._attr_source != self.coordinator.video_source:
self._attr_source = self.coordinator.video_source
updated = True
self._attr_volume_level = self.coordinator.volume
self._attr_is_volume_muted = self.coordinator.muted
self._attr_source = self.coordinator.video_source

if (
self._attr_state != MediaPlayerState.ON
or self._attr_available is not True
):
self._attr_state = MediaPlayerState.ON
self._attr_available = True
updated = True
elif self.coordinator.power_status == BenQProjector.POWERSTATUS_POWERINGOFF:
if (
self._attr_state != MediaPlayerState.OFF
or self._attr_available is not False
):
self._attr_state = MediaPlayerState.OFF
self._attr_available = False
updated = True
elif self.coordinator.power_status == BenQProjector.POWERSTATUS_OFF:
if (
self._attr_state != MediaPlayerState.OFF
or self._attr_available is not True
):
self._attr_state = MediaPlayerState.OFF
self._attr_available = True
updated = True

# Only update the HA state if state has updated.
if updated:
self.async_write_ha_state()
self.async_write_ha_state()

async def async_turn_on(self) -> None:
"""Turn projector on."""
Expand Down
28 changes: 7 additions & 21 deletions custom_components/benqprojector/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,42 +115,28 @@ def _handle_coordinator_update(self) -> None:
new_value := self.coordinator.data.get(self.entity_description.key)
):
try:
new_value = float(new_value)
if self._attr_native_value != new_value:
self._attr_native_value = new_value
updated = True

if self._attr_available is not True:
self._attr_available = True
updated = True
self._attr_native_value = float(new_value)
self._attr_available = True
except ValueError as ex:
_LOGGER.error(
"ValueError for %s = %s, %s",
self.entity_description.key,
self.coordinator.data.get(self.entity_description.key),
ex,
)
if self._attr_available is not False:
self._attr_available = False
updated = True
self._attr_available = False
except TypeError as ex:
_LOGGER.error(
"TypeError for %s, %s", self.entity_description.key, ex
)
if self._attr_available is not False:
self._attr_available = False
updated = True
elif self._attr_available is not False:
self._attr_available = False
else:
self._attr_available = False
updated = True
elif self._attr_available is not False:
else:
_LOGGER.debug("%s is not available", self.entity_description.key)
self._attr_available = False
updated = True

# Only update the HA state if state has updated.
if updated:
self.async_write_ha_state()
self.async_write_ha_state()

async def async_set_native_value(self, value: float) -> None:
_LOGGER.debug("async_set_native_value")
Expand Down
25 changes: 8 additions & 17 deletions custom_components/benqprojector/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ async def async_added_to_hass(self) -> None:
):
self._attr_current_option = current_option
self._attr_available = True
self.async_write_ha_state()
else:
_LOGGER.debug("%s is not available", self.entity_description.key)
self._attr_available = False

self.async_write_ha_state()

@property
def available(self) -> bool:
Expand All @@ -121,33 +123,22 @@ def available(self) -> bool:
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
updated = False

if self.coordinator.power_status in [
BenQProjector.POWERSTATUS_POWERINGON,
BenQProjector.POWERSTATUS_ON,
]:
if self.coordinator.data and (
new_state := self.coordinator.data.get(self.entity_description.key)
):
if self._attr_current_option != new_state:
self._attr_current_option = new_state
updated = True

if self._attr_available is not True:
self._attr_available = True
updated = True
elif self._attr_available is not False:
self._attr_current_option = new_state
self._attr_available = True
else:
self._attr_available = False
updated = True
elif self._attr_available is not False:
else:
_LOGGER.debug("%s is not available", self.entity_description.key)
self._attr_available = False
updated = True

# Only update the HA state if state has updated.
if updated:
self.async_write_ha_state()
self.async_write_ha_state()

async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
Expand Down
48 changes: 13 additions & 35 deletions custom_components/benqprojector/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ async def async_added_to_hass(self) -> None:
):
self._attr_native_value = native_value
self._attr_available = True
self.async_write_ha_state()
else:
_LOGGER.debug("%s is not available", self.entity_description.key)
self._attr_available = False

self.async_write_ha_state()

@property
def available(self) -> bool:
Expand All @@ -99,8 +101,6 @@ def available(self) -> bool:
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
updated = False

if self.coordinator.power_status in [
BenQProjector.POWERSTATUS_POWERINGON,
BenQProjector.POWERSTATUS_ON,
Expand All @@ -110,23 +110,14 @@ def _handle_coordinator_update(self) -> None:
if self.coordinator.data and (
new_value := self.coordinator.data.get(self.entity_description.key)
):
if self._attr_native_value != new_value:
self._attr_native_value = new_value
updated = True

if self._attr_available is not True:
self._attr_available = True
updated = True
elif self._attr_available is not False:
self._attr_native_value = new_value
self._attr_available = True
else:
self._attr_available = False
updated = True
elif self._attr_available is not False:
else:
self._attr_available = False
updated = True

# Only update the HA state if state has updated.
if updated:
self.async_write_ha_state()
self.async_write_ha_state()


class BenQProjectorLampTimeSensor(BenQProjectorSensor):
Expand All @@ -138,29 +129,16 @@ class BenQProjectorLampTimeSensor(BenQProjectorSensor):
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
updated = False

if self.coordinator.data and (
new_value := self.coordinator.data.get(self.entity_description.key)
):
try:
new_value = int(new_value)
if self._attr_native_value != new_value:
self._attr_native_value = new_value
updated = True

if self._attr_available is not True:
self._attr_available = True
updated = True
self._attr_native_value = int(new_value)
self._attr_available = True
except ValueError as ex:
_LOGGER.error(ex)
if self._attr_available is not False:
self._attr_available = False
updated = True
elif self._attr_available is not False:
self._attr_available = False
else:
self._attr_available = False
updated = True

# Only update the HA state if state has updated.
if updated:
self.async_write_ha_state()
self.async_write_ha_state()
30 changes: 9 additions & 21 deletions custom_components/benqprojector/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,13 @@ async def async_added_to_hass(self) -> None:
if self.coordinator.data and (
new_state := self.coordinator.data.get(self.entity_description.key)
):
if new_state == "on":
self._attr_is_on = True
else:
self._attr_is_on = False
self._attr_is_on = new_state == "on"
self._attr_available = True
self.async_write_ha_state()
else:
_LOGGER.debug("%s is not available", self.entity_description.key)
self._attr_available = False

self.async_write_ha_state()

@property
def available(self) -> bool:
Expand All @@ -169,7 +168,6 @@ def available(self) -> bool:
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
updated = False

if self.coordinator.power_status in [
BenQProjector.POWERSTATUS_POWERINGON,
Expand All @@ -178,25 +176,15 @@ def _handle_coordinator_update(self) -> None:
if self.coordinator.data and (
new_state := self.coordinator.data.get(self.entity_description.key)
):
new_state = new_state == "on"
if self._attr_is_on != new_state:
self._attr_is_on = new_state
updated = True

if self._attr_available is not True:
self._attr_available = True
updated = True
elif self._attr_available is not False:
self._attr_is_on = new_state == "on"
self._attr_available = True
else:
self._attr_available = False
updated = True
elif self._attr_available is not False:
else:
_LOGGER.debug("%s is not available", self.entity_description.key)
self._attr_available = False
updated = True

# Only update the HA state if state has updated.
if updated:
self.async_write_ha_state()
self.async_write_ha_state()

async def async_turn_on(self, **kwargs) -> None:
"""Turn the entity on."""
Expand Down

0 comments on commit 07f78ee

Please sign in to comment.