Skip to content

Commit

Permalink
Update from source (#4)
Browse files Browse the repository at this point in the history
* Added the option to quickly reload it from dev tools

* Update climate.py

replaced deprecated consts

* Update climate.py

* Update climate.py

Remove long text from log

* Add asyncio

* Make unit tests pass

* Remove custom_components climate.py

* Bump version number

---------

Co-authored-by: Dan Timu <dan.timu@gmail.com>
Co-authored-by: aver-ua <andy.versal@gmail.com>
Co-authored-by: floringhimie <33951255+floringhimie@users.noreply.github.com>
  • Loading branch information
4 people authored Feb 16, 2024
1 parent abb9b1b commit c9d1e32
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
1 change: 1 addition & 0 deletions requirements.test.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest
pytest-mock
pytest-asyncio
pytest-homeassistant-custom-component
14 changes: 10 additions & 4 deletions salusfy/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@

from homeassistant.components.climate import PLATFORM_SCHEMA

__version__ = "0.1.0"
from homeassistant.helpers.reload import async_setup_reload_service

__version__ = "0.3.0"

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = "Salus Thermostat"

CONF_NAME = "name"

DOMAIN = "salusfy"
PLATFORMS = ["climate"]

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
Expand All @@ -44,8 +48,10 @@
)


def setup_platform(hass, config, add_entities, discovery_info=None):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the E-Thermostat platform."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)

name = config.get(CONF_NAME)
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
Expand All @@ -57,14 +63,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):

if (simulator):
_LOGGER.info('Registering Salus simulator...')
add_entities(
async_add_entities(
[ThermostatEntity(name, MockWebClient(), MockHaWebClient())]
)
else:
_LOGGER.info('Registering Salus Thermostat climate entity...')
web_client = WebClient(username, password, id)
ha_client = HaWebClient(host, entity_id, access_token)

add_entities(
async_add_entities(
[ThermostatEntity(name, web_client, ha_client)]
)
34 changes: 16 additions & 18 deletions salusfy/thermostat_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@
)

from homeassistant.components.climate.const import (
CURRENT_HVAC_HEAT,
CURRENT_HVAC_IDLE,
HVAC_MODE_HEAT,
HVAC_MODE_OFF,
SUPPORT_PRESET_MODE,
SUPPORT_TARGET_TEMPERATURE
HVACAction,
HVACMode,
ClimateEntityFeature,
SUPPORT_PRESET_MODE
)

from homeassistant.const import (
ATTR_TEMPERATURE,
TEMP_CELSIUS,
UnitOfTemperature,
)

try:
from homeassistant.components.climate import ClimateEntity
except ImportError:
from homeassistant.components.climate import ClimateDevice as ClimateEntity

SUPPORT_FLAGS = SUPPORT_TARGET_TEMPERATURE
SUPPORT_FLAGS = ClimateEntityFeature.TARGET_TEMPERATURE

class ThermostatEntity(ClimateEntity):
"""Representation of a Salus Thermostat device."""
Expand Down Expand Up @@ -73,7 +71,7 @@ def max_temp(self):
@property
def temperature_unit(self):
"""Return the unit of measurement."""
return TEMP_CELSIUS
return UnitOfTemperature.CELSIUS

@property
def current_temperature(self):
Expand All @@ -91,26 +89,26 @@ def hvac_mode(self):
"""Return hvac operation ie. heat, cool mode."""
try:
climate_mode = self._state.current_operation_mode
curr_hvac_mode = HVAC_MODE_OFF
curr_hvac_mode = HVACMode.OFF
if climate_mode == STATE_ON:
curr_hvac_mode = HVAC_MODE_HEAT
curr_hvac_mode = HVACMode.HEAT
else:
curr_hvac_mode = HVAC_MODE_OFF
curr_hvac_mode = HVACMode.OFF
except KeyError:
return HVAC_MODE_OFF
return HVACMode.OFF
return curr_hvac_mode

@property
def hvac_modes(self):
"""HVAC modes."""
return [HVAC_MODE_HEAT, HVAC_MODE_OFF]
return [HVACMode.HEAT, HVACMode.OFF]

@property
def hvac_action(self):
"""Return the current running hvac operation."""
if self._state.status == STATE_ON:
return CURRENT_HVAC_HEAT
return CURRENT_HVAC_IDLE
return HVACAction.HEATING
return HVACAction.IDLE


@property
Expand Down Expand Up @@ -142,10 +140,10 @@ def set_hvac_mode(self, hvac_mode):

self._client.set_hvac_mode(hvac_mode)

if hvac_mode == HVAC_MODE_OFF:
if hvac_mode == HVACMode.OFF:
self._state.current_operation_mode = STATE_OFF
self._state.status = STATE_OFF
elif hvac_mode == HVAC_MODE_HEAT:
elif hvac_mode == HVACMode.HEAT:
self._state.current_operation_mode = STATE_ON
self._state.status = STATE_ON

Expand Down
30 changes: 22 additions & 8 deletions tests/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,44 @@

from . import mock_config

class MockHass:
@property
def services(self):
return self

def has_service(self, domain, service):
return False

def async_register(self, domain, service, admin_handler, schema):
pass

def setup_climate_platform():
@pytest.mark.asyncio
async def setup_climate_platform():
registry = EntityRegistry()
config_adapter = ConfigAdapter(mock_config)
climate.setup_platform(None, config_adapter, add_entities=registry.register, discovery_info=None)
await climate.async_setup_platform(MockHass(), config_adapter, async_add_entities=registry.register, discovery_info=None)
return registry


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

assert len(registry.entities) == 1


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

thermostat = registry.first

assert thermostat.current_temperature == 15.9


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

thermostat = registry.first

Expand Down

0 comments on commit c9d1e32

Please sign in to comment.