Skip to content

Commit

Permalink
Ensure async methods are called (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturner authored Feb 25, 2024
1 parent c0518e5 commit 8c57a85
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
4 changes: 2 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ async def main():
await thermostat.async_update()
await thermostat.async_update()

await thermostat.set_hvac_mode(HVACMode.HEAT)
await thermostat.set_temperature(temperature=9.8)
await thermostat.async_set_hvac_mode(HVACMode.HEAT)
await thermostat.async_set_temperature(temperature=9.8)

print("Current: " + str(thermostat.current_temperature))
print("Target: " + str(thermostat.target_temperature))
Expand Down
2 changes: 1 addition & 1 deletion salusfy/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=

name = config.get(CONF_NAME)
async_add_entities(
[ThermostatEntity(name, client)]
[ThermostatEntity(name, client)], update_before_add=True
)


Expand Down
18 changes: 7 additions & 11 deletions salusfy/thermostat_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
MIN_TEMP
)

from .state import State

from homeassistant.components.climate.const import (
HVACAction,
HVACMode,
Expand All @@ -25,7 +23,6 @@
except ImportError:
from homeassistant.components.climate import ClimateDevice as ClimateEntity

SUPPORT_FLAGS = ClimateEntityFeature.TARGET_TEMPERATURE

class ThermostatEntity(ClimateEntity):
"""Representation of a Salus Thermostat device."""
Expand All @@ -34,13 +31,12 @@ def __init__(self, name, client):
"""Initialize the thermostat."""
self._name = name
self._client = client
self._state = State()


@property
def supported_features(self):
"""Return the list of supported features."""
return SUPPORT_FLAGS
return ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.TURN_ON | ClimateEntityFeature.TURN_OFF

@property
def name(self):
Expand Down Expand Up @@ -121,7 +117,7 @@ def preset_modes(self):
return ClimateEntityFeature.PRESET_MODE


async def set_temperature(self, **kwargs):
async def async_set_temperature(self, **kwargs) -> None:
"""Set new target temperature."""

temperature = kwargs.get(ATTR_TEMPERATURE)
Expand All @@ -134,7 +130,7 @@ async def set_temperature(self, **kwargs):
self._state.target_temperature = temperature


async def set_hvac_mode(self, hvac_mode):
async def async_set_hvac_mode(self, hvac_mode) -> None:
"""Set HVAC mode, via URL commands."""

await self._client.set_hvac_mode(hvac_mode)
Expand All @@ -147,12 +143,12 @@ async def set_hvac_mode(self, hvac_mode):
self._state.status = STATE_ON


async def turn_off(self) -> None:
await self.set_hvac_mode(HVACAction.OFF)
async def async_turn_off(self) -> None:
await self.async_set_hvac_mode(HVACMode.OFF)


async def turn_on(self) -> None:
await self.set_hvac_mode(HVACAction.HEATING)
async def async_turn_on(self) -> None:
await self.async_set_hvac_mode(HVACMode.HEAT)


async def async_update(self):
Expand Down
14 changes: 12 additions & 2 deletions tests/entity_registry.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
class EntityRegistry:
def __init__(self):
self._entities = []
self._update_before_add = False

def register(self, list):

def register(self, list, **kwargs):
self._update_before_add = kwargs.get('update_before_add')
self._entities.extend(list)


@property
def entities(self):
return self._entities


@property
def first(self):
return self._entities[0]
return self._entities[0]


@property
def update_before_add(self):
return self._update_before_add
7 changes: 7 additions & 0 deletions tests/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ async def test_entity_is_registered():
assert len(registry.entities) == 1


@pytest.mark.asyncio
async def test_entity_is_updated_before_added():
registry = await setup_climate_platform()

assert registry.update_before_add == True


@pytest.mark.asyncio
async def test_entity_returns_mock_temperature():
registry = await setup_climate_platform()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_thermostat_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def test_entity_delegates_set_temperature_web_client(mock_client):

await target.async_update()

await target.set_temperature(temperature=29.9)
await target.async_set_temperature(temperature=29.9)

mock_client.set_temperature.assert_called_once_with(29.9)
assert target.target_temperature == 29.9
Expand All @@ -45,7 +45,7 @@ async def test_entity_delegates_set_hvac_mode_to_web_client(mock_client):

await target.async_update()

await target.set_hvac_mode(hvac_mode=HVACMode.HEAT)
await target.async_set_hvac_mode(hvac_mode=HVACMode.HEAT)

mock_client.set_hvac_mode.assert_called_once_with(HVACMode.HEAT)
assert target.hvac_mode == HVACMode.HEAT

0 comments on commit 8c57a85

Please sign in to comment.