Skip to content

Commit

Permalink
Fix fans showing unavailable (#95)
Browse files Browse the repository at this point in the history
Fix fans showing unavailable

 * Fix an issue where fans were not properly consuming the availability
   state
 * Fix an issue where geo-coordinates were not set to 0

Sem-Ver: bugfix
  • Loading branch information
Expl0dingBanana authored Aug 2, 2024
1 parent 0b8b369 commit c7f0e37
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Configuration is done through the `Add Integrations` rather than configuration.y
Some devices may not work after moving the configuration to the integration. Please review
the docs on how to gather the device data to send to the developer.

No longer supports services. Many often used services are incorperated into the device types. Hopefully will be added back
No longer supports services. Many often used services are incorperated into the device types. Hopefully will be added back

Thanks to @dloveall and now @Expl0dingBanana this release will automatically discover most devices. Post an issue, if your device is no longer found.

Expand Down
2 changes: 2 additions & 0 deletions custom_components/hubspace/anonomyize_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def anonymize_state(state: HubSpaceState):
fake_state["lastUpdateTime"] = 0
if fake_state["functionClass"] == "wifi-ssid":
fake_state["value"] = str(uuid4())
elif fake_state["functionClass"] == "geo-coordinates":
fake_state["value"] = {"geo-coordinates": {"latitude": "0", "longitude": "0"}}
elif isinstance(state.value, str):
if "mac" in state.functionClass:
fake_state["value"] = str(uuid4())
Expand Down
23 changes: 23 additions & 0 deletions custom_components/hubspace/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,26 @@ async def get_sensors(
)
required_sensors.append(sensor)
return required_sensors


async def create_devices_from_data(
file_name: str,
) -> list[hubspace_async.HubSpaceDevice]:
"""Generate devices from a data dump
:param file_name: Name of the file to load
"""
current_path: str = os.path.dirname(os.path.realpath(__file__))
async with aiofiles.open(os.path.join(current_path, file_name), "r") as fh:
data = await fh.read()
devices = json.loads(data)
processed = []
for device in devices:
processed_states = []
for state in device["states"]:
processed_states.append(hubspace_async.HubSpaceState(**state))
device["states"] = processed_states
if "children" not in device:
device["children"] = []
processed.append(hubspace_async.HubSpaceDevice(**device))
return processed
2 changes: 2 additions & 0 deletions custom_components/hubspace/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ def update_states(self) -> None:
self._current_direction = state.value
elif state.functionClass == "power":
self._state = state.value
elif state.functionClass == "available":
self._availability = state.value
elif state.functionClass in additional_attrs:
self._bonus_attrs[state.functionClass] = state.value

Expand Down
1 change: 1 addition & 0 deletions tests/test_fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def test_process_functions(self, functions, expected_attrs, empty_fan):
"_fan_speed": "fan-speed-6-050",
"_current_direction": "reverse",
"_state": "on",
"_availability": True,
},
{
"model": None,
Expand Down
1 change: 1 addition & 0 deletions tests/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def test_process_functions(functions, expected_attrs, empty_light):
"_state": "on",
"_color_temp": 3000,
"_brightness": 114,
"_availability": True,
},
{
"Child ID": None,
Expand Down
1 change: 1 addition & 0 deletions tests/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def empty_lock(mocked_coordinator):
lock_tbd_instance.states,
{
"_current_position": "locked",
"_availability": True,
},
{
"Child ID": None,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_valve.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def empty_valve(mocked_coordinator):
@pytest.mark.parametrize(
"instance,states,expected_attrs",
[
("spigot-1", spigot.states, {"_state": "off"}),
("spigot-2", spigot.states, {"_state": "on"}),
("spigot-1", spigot.states, {"_state": "off", "_availability": True}),
("spigot-2", spigot.states, {"_state": "on", "_availability": True}),
],
)
async def test_update_states(instance, states, expected_attrs, empty_valve):
Expand Down
16 changes: 15 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import asyncio
import json
import os
from typing import Any

from hubspace_async import HubSpaceDevice, HubSpaceState
from hubspace_async import HubSpaceConnection, HubSpaceDevice, HubSpaceState

from custom_components.hubspace import anonomyize_data

current_path: str = os.path.dirname(os.path.realpath(__file__))

Expand Down Expand Up @@ -32,3 +35,14 @@ def create_devices_from_data(file_name: str) -> list[HubSpaceDevice]:
device["children"] = []
processed.append(HubSpaceDevice(**device))
return processed


def convert_hs_raw(data):
"""Used for converting old data-dumps to new data dumps"""
loop = asyncio.get_event_loop()
conn = HubSpaceConnection(None, None)
loop.run_until_complete(conn._process_api_results(data))
devs = loop.run_until_complete(anonomyize_data.generate_anon_data(conn))
with open("converted.json", "w") as fh:
json.dump(devs, fh, indent=4)
return devs

0 comments on commit c7f0e37

Please sign in to comment.