Skip to content

Commit 54d20bc

Browse files
committed
Handle status updates for switches
1 parent d958c56 commit 54d20bc

5 files changed

Lines changed: 15 additions & 13 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ Additional the setup flow will ask you for your username (i.e. e-mail address) a
2828

2929
In the current state it retrieves `bike`, `status` and `position` from the API every 10 minutes.
3030

31-
There is an early implementation on toggling data on your bike, `light` and `lock` can be adjusted.
31+
**BETA** There is an early implementation on toggling data on your bike, `light` and `lock` can be adjusted.
32+
Do note that the switches do not immediately reflect the status (i.e. they will when you toggle them, but switch back quickly).
33+
After your 'switch' command we do request an API update to check on the status, pending that update the switch might toggle back-n-forth some time.
3234

3335
## If you want more frequent updates
3436

custom_components/stromer/binary_sensor.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ class StromerBinarySensor(StromerEntity, BinarySensorEntity):
6969
"""Representation of a Binary Sensor."""
7070

7171
_attr_has_entity_name = True
72-
_attr_name = None
73-
_attr_translation_key = DOMAIN
7472

7573
entity_description = StromerBinarySensorEntityDescription
7674

custom_components/stromer/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"iot_class": "cloud_polling",
99
"issue_tracker": "https://github.com/CoMPaTech/stromer/issues",
1010
"requirements": [],
11-
"version": "0.3.0a6"
11+
"version": "0.3.0a7"
1212
}

custom_components/stromer/sensor.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,6 @@ class StromerSensor(StromerEntity, SensorEntity):
187187
"""Representation of a Sensor."""
188188

189189
_attr_has_entity_name = True
190-
_attr_name = None
191-
_attr_translation_key = DOMAIN
192190

193191
entity_description = SensorEntityDescription
194192

custom_components/stromer/switch.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
SwitchEntity,
1111
SwitchEntityDescription,
1212
)
13+
from homeassistant.core import callback
1314

1415
from .const import DOMAIN, LOGGER
1516
from .entity import StromerEntity
@@ -48,8 +49,6 @@ class StromerSwitch(StromerEntity, SwitchEntity):
4849
"""Representation of a Switch."""
4950

5051
_attr_has_entity_name = True
51-
_attr_name = None
52-
_attr_translation_key = DOMAIN
5352

5453
entity_description: SwitchEntityDescription
5554

@@ -68,23 +67,28 @@ def __init__(
6867
device_id = coordinator.data.bike_id
6968

7069
self.entity_description = description
71-
self._attr_unique_id = f"{device_id}-{description.key}"
70+
self._attr_unique_id = f"{device_id}-{description.key}-sw"
7271

73-
@property
74-
def is_on(self) -> bool:
75-
"""Return True if entity is on."""
76-
return self._coordinator.data.bikedata.get(self._ent)
72+
@callback
73+
def _handle_coordinator_update(self) -> None:
74+
"""Handle updated data from the coordinator."""
75+
self._attr_is_on = self._coordinator.data.bikedata.get(self._ent)
76+
self.async_write_ha_state()
7777

7878
async def async_turn_on(self, **kwargs: Any) -> None:
7979
"""Turn the device on."""
8080
if self.entity_description.key == "lock_flag":
8181
await self._coordinator.stromer.stromer_call_lock(True)
8282
if self.entity_description.key == "light_on":
8383
await self._coordinator.stromer.stromer_call_light("on")
84+
# Call update on the bike so `is_on` correctly reflects status
85+
await self._coordinator.async_request_refresh()
8486

8587
async def async_turn_off(self, **kwargs: Any) -> None:
8688
"""Turn the device off."""
8789
if self.entity_description.key == "lock_flag":
8890
await self._coordinator.stromer.stromer_call_lock(False)
8991
if self.entity_description.key == "light_on":
9092
await self._coordinator.stromer.stromer_call_light("off")
93+
# Call update on the bike so `is_on` correctly reflects status
94+
await self._coordinator.async_request_refresh()

0 commit comments

Comments
 (0)