Skip to content

Commit

Permalink
Fix availability (#92)
Browse files Browse the repository at this point in the history
* Report availability based on HubSpace

Sem-Ver: bugfix
  • Loading branch information
Expl0dingBanana authored Aug 1, 2024
1 parent a0fed27 commit fbbfcbb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
7 changes: 6 additions & 1 deletion custom_components/hubspace/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class HubspaceFan(CoordinatorEntity, FanEntity):
:ivar _preset_modes: List of available preset modes for the device
:ivar _supported_features: Features that the fan supports, where each
feature is an Enum from FanEntityFeature.
:ivar _availability: If the device is available within HubSpace
:ivar _fan_speeds: List of available fan speeds for the device from HubSpace
:ivar _bonus_attrs: Attributes relayed to Home Assistant that do not need to be
tracked in their own class variables
Expand Down Expand Up @@ -76,6 +77,7 @@ def __init__(
self._preset_mode: Optional[str] = None
self._preset_modes: set[str] = set()
self._supported_features: FanEntityFeature = FanEntityFeature(0)
self._availability: Optional[bool] = None
self._fan_speeds: list[Union[str, int]] = []
self._fan_speed: Optional[str] = None
self._bonus_attrs = {
Expand Down Expand Up @@ -141,7 +143,6 @@ def update_states(self) -> None:
additional_attrs = [
"wifi-ssid",
"wifi-mac-address",
"available",
"ble-mac-address",
]
# functionClass -> internal attribute
Expand Down Expand Up @@ -173,6 +174,10 @@ def unique_id(self) -> str:
"""Return the display name of this light."""
return self._child_id

@property
def available(self) -> bool:
return self._availability is True

@property
def extra_state_attributes(self):
"""Return the state attributes."""
Expand Down
8 changes: 8 additions & 0 deletions custom_components/hubspace/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class HubspaceLight(CoordinatorEntity, LightEntity):
:ivar _state: If the device is on / off
:ivar _bonus_attrs: Attributes relayed to Home Assistant that do not need to be
tracked in their own class variables
:ivar _availability: If the device is available within HubSpace
:ivar _instance_attrs: Additional attributes that are required when
POSTing to HubSpace
:ivar _color_modes: Supported options for the light
Expand Down Expand Up @@ -137,6 +138,7 @@ def __init__(
"deviceId": device_id,
"Child ID": self._child_id,
}
self._availability: Optional[bool] = None
self._instance_attrs: dict[str, str] = {}
# Entity-specific
self._color_modes: set[ColorMode] = set()
Expand Down Expand Up @@ -273,6 +275,8 @@ def update_states(self) -> None:
self._current_effect = state.value
elif state.functionClass in additional_attrs:
self._bonus_attrs[state.functionClass] = state.value
elif state.functionClass == "available":
self._availability = state.value

@property
def should_poll(self):
Expand All @@ -289,6 +293,10 @@ def unique_id(self) -> str:
"""Return the display name of this light."""
return self._child_id

@property
def available(self) -> bool:
return self._availability is True

@property
def extra_state_attributes(self):
"""Return the state attributes."""
Expand Down
11 changes: 9 additions & 2 deletions custom_components/hubspace/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class HubSpaceLock(CoordinatorEntity, LockEntity):
tracked in their own class variables
:ivar _current_position: Current position of the device
:ivar _supported_features: Supported features of the device
:ivar _availability: If the device is available within HubSpace
"""

def __init__(
Expand All @@ -47,6 +48,7 @@ def __init__(
"deviceId": device_id,
"Child ID": self._child_id,
}
self._availability: Optional[bool] = None
# Entity-specific
self._current_position: Optional[str] = None
self._supported_features: Optional[LockEntityFeature] = LockEntityFeature(0)
Expand All @@ -56,7 +58,6 @@ def __init__(
def process_functions(self, functions: list[dict]) -> None:
"""Process available functions
:param functions: Functions that are supported from the API
"""
for function in functions:
Expand Down Expand Up @@ -84,7 +85,9 @@ def update_states(self) -> None:
_LOGGER.debug("About to update using %s", states)
# functionClass -> internal attribute
for state in states:
if state.functionClass == "lock-control":
if state.functionClass == "available":
self._availability = state.value
elif state.functionClass == "lock-control":
_LOGGER.debug("Found lock-control and setting to %s", state.value)
self._current_position = state.value

Expand All @@ -98,6 +101,10 @@ def unique_id(self) -> str:
"""Return the HubSpace ID"""
return self._child_id

@property
def available(self) -> bool:
return self._availability is True

@property
def extra_state_attributes(self):
"""Return the state attributes."""
Expand Down
10 changes: 9 additions & 1 deletion custom_components/hubspace/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class HubSpaceSwitch(CoordinatorEntity, SwitchEntity):
:ivar _state: If the device is on / off
:ivar _bonus_attrs: Attributes relayed to Home Assistant that do not need to be
tracked in their own class variables
:ivar _availability: If the device is available within HubSpace
:ivar _device_class: Device class used during lookup
:ivar _instance: functionInstance within the HS device
"""
Expand All @@ -50,6 +51,7 @@ def __init__(
"deviceId": device_id,
"Child ID": self._child_id,
}
self._availability: Optional[bool] = None
# Entity-specific
self._device_class = device_class
self._instance = instance
Expand All @@ -71,7 +73,9 @@ def update_states(self) -> None:
)
# functionClass -> internal attribute
for state in states:
if state.functionClass != "power":
if state.functionClass == "available":
self._availability = state.value
elif state.functionClass != "power":
continue
if not self._instance:
self._state = state.value
Expand All @@ -98,6 +102,10 @@ def unique_id(self) -> str:
else:
return self._child_id

@property
def available(self) -> bool:
return self._availability is True

@property
def extra_state_attributes(self):
"""Return the state attributes."""
Expand Down
10 changes: 9 additions & 1 deletion custom_components/hubspace/valve.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class HubSpaceValve(CoordinatorEntity, ValveEntity):
:ivar _state: If the device is on / off
:ivar _bonus_attrs: Attributes relayed to Home Assistant that do not need to be
tracked in their own class variables
:ivar _availability: If the device is available within HubSpace
:ivar _instance: functionInstance within the HS device
:ivar _current_valve_position: Current position of the valve
:ivar _reports_position: Reports position of the valve
Expand All @@ -54,6 +55,7 @@ def __init__(
"deviceId": device_id,
"Child ID": self._child_id,
}
self._availability: Optional[bool] = None
# Entity-specific
# Assume that all HubSpace devices allow for open / close
self._supported_features: ValveEntityFeature = (
Expand All @@ -80,7 +82,9 @@ def update_states(self) -> None:
)
# functionClass -> internal attribute
for state in states:
if state.functionClass != "toggle":
if state.functionClass == "available":
self._availability = state.value
elif state.functionClass != "toggle":
continue
if not self._instance:
self._state = state.value
Expand All @@ -107,6 +111,10 @@ def unique_id(self) -> str:
else:
return self._child_id

@property
def available(self) -> bool:
return self._availability is True

@property
def supported_features(self) -> ValveEntityFeature:
return self._supported_features
Expand Down
1 change: 0 additions & 1 deletion tests/test_fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def test_process_functions(self, functions, expected_attrs, empty_fan):
"Child ID": None,
"wifi-ssid": "71e7209f-b932-44b9-ba2f-a8179f68c3ac",
"wifi-mac-address": "e1119e0a-688d-45df-9882-a76549db9bc3",
"available": True,
"ble-mac-address": "07346a23-350b-4606-8d86-67217ec7a688",
},
),
Expand Down

0 comments on commit fbbfcbb

Please sign in to comment.