Skip to content

Commit

Permalink
fix: always use api state for dimmer entity
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Jan 1, 2023
1 parent 702b58f commit 4b3a6b6
Showing 1 changed file with 4 additions and 18 deletions.
22 changes: 4 additions & 18 deletions custom_components/nexa_bridge_x/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def __init__(self, coordinator: DataUpdateCoordinator, node: NexaNode):
_LOGGER.info("Found light %s: %s", node.id, node.name)
super().__init__(coordinator)
self.id = node.id
self.switch_to_state = None
self._attr_name = create_friendly_name("Light", node)
self._attr_unique_id = f"dimmer_{node.id}"

Expand All @@ -83,38 +82,25 @@ def _handle_coordinator_update(self) -> None:
value = node.get_value("switchLevel")
value_percentage = int(value * 100)

if self.switch_to_state is not None:
self._attr_is_on = self.switch_to_state
if self.switch_to_state is True and value_percentage == 0:
self.switch_to_state = None
if self.switch_to_state is False and value_percentage > 0:
self.switch_to_state = None
else:
self._attr_is_on = value_percentage > 0

self._attr_is_on = value_percentage > 0
self._attr_brightness = int(value * 255)
self._attr_name = create_friendly_name("Light", node)
self.async_write_ha_state()
else:
self.switch_to_state = False

async def async_turn_on(self, **kwargs) -> None:
if ATTR_BRIGHTNESS in kwargs:
value = kwargs.get(ATTR_BRIGHTNESS, 255)
await self.coordinator.handle_dimmer(self.id, value / 255)
value = kwargs.get(ATTR_BRIGHTNESS, 255) / 255
else:
value = 1.0
await self.coordinator.handle_dimmer(self.id, 1)

self.switch_to_state = True
self._attr_is_on = True
self._attr_brightness = value
self._attr_brightness = int(value * 255)

self.async_write_ha_state()
await self.coordinator.handle_dimmer(self.id, value)
await self.coordinator.async_request_refresh()

async def async_turn_off(self, **kwargs) -> None:
self.switch_to_state = False
self._attr_is_on = False
self._attr_brightness = 0

Expand Down

0 comments on commit 4b3a6b6

Please sign in to comment.