Skip to content

Commit

Permalink
Change state to dataclass (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturner authored Mar 1, 2024
1 parent 6ccd7df commit 36337a5
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 38 deletions.
24 changes: 19 additions & 5 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Exercises the Salus client as if it was run through Home Assistant"""

# To run this script to test the component:
# 1 Copy config.sample.py to config.py
# 2 Replace the username/password/deviceid (don't worry, this file will be ignored by git)
Expand All @@ -6,6 +8,8 @@
import asyncio
import logging

import argparse

from homeassistant.components.climate.const import HVACMode

from salusfy import climate
Expand All @@ -19,19 +23,23 @@
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)


async def main():
async def main(set_temp: bool) -> None:
"""Exercises the Salus client"""

registry = EntityRegistry()
config_adapter = ConfigAdapter(config)

await climate.async_setup_platform(MockHass(), config_adapter, async_add_entities=registry.register, discovery_info=None)
await climate.async_setup_platform(
MockHass(), config_adapter, async_add_entities=registry.register, discovery_info=None)

thermostat = registry.first

await thermostat.async_update()
await thermostat.async_update()

await thermostat.async_set_hvac_mode(HVACMode.HEAT)
await thermostat.async_set_temperature(temperature=9.8)
if set_temp:
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 All @@ -40,6 +48,12 @@ async def main():


if __name__ == '__main__':
parser = argparse.ArgumentParser("Salus Client Simulator")
parser.add_argument("--set_temp", default=False, required=False,
help="Determines whether to set the temp",
type=bool)
args = parser.parse_args()

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(main())
loop.run_until_complete(main(args.set_temp))
11 changes: 6 additions & 5 deletions salusfy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
a specialized client.
"""
import logging
from . import (
WebClient,
HaTemperatureClient,
State,
)

from homeassistant.components.climate.const import (
HVACMode,
HVACAction,
)

from . import (
WebClient,
HaTemperatureClient,
State,
)

_LOGGER = logging.getLogger(__name__)


Expand Down
28 changes: 17 additions & 11 deletions salusfy/climate.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
"""
Adds support for the Salus Thermostat units.
"""
from . import (
ThermostatEntity,
Client,
WebClient,
HaTemperatureClient,
)
import logging
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.components.climate import PLATFORM_SCHEMA
from . import simulator
import logging
import voluptuous as vol

import homeassistant.helpers.config_validation as cv
import voluptuous as vol

from homeassistant.const import (
CONF_PASSWORD,
Expand All @@ -24,6 +17,14 @@
CONF_HOST
)

from . import simulator
from . import (
ThermostatEntity,
Client,
WebClient,
HaTemperatureClient,
)

CONF_SIMULATOR = 'simulator'
CONF_ENABLE_TEMPERATURE_CLIENT = 'enable_temperature_client'

Expand Down Expand Up @@ -66,6 +67,9 @@

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the E-Thermostat platform."""

_LOGGER.info("Discovery info: %s", discovery_info)

await async_setup_reload_service(hass, DOMAIN, PLATFORMS)

client = create_client_from(config)
Expand All @@ -77,17 +81,19 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=


def create_client_from(config) -> Client:
"""Creates a client object based on the specified configuration"""

username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
id = config.get(CONF_ID)
device_id = config.get(CONF_ID)
enable_simulator = config.get(CONF_SIMULATOR)

if enable_simulator:
_LOGGER.info('Registering Salus Thermostat client simulator...')

return Client(simulator.WebClient(), simulator.TemperatureClient())

web_client = WebClient(username, password, id)
web_client = WebClient(username, password, device_id)

enable_temperature_client = config.get(CONF_ENABLE_TEMPERATURE_CLIENT)

Expand Down
10 changes: 5 additions & 5 deletions salusfy/ha_temperature_client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""
Retrieves the current temperature from
another entity from the Home Assistant API
"""
"""Reduces reliance on the Salus API"""
import aiohttp


class HaTemperatureClient:
"""
Retrieves the current temperature from
another entity from the Home Assistant API
"""
def __init__(self, host, entity_id, access_token):
self._entity_id = entity_id
self._host = host
Expand Down
17 changes: 10 additions & 7 deletions salusfy/state.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""Exposes state of the thermostat."""

import dataclasses

@dataclasses.dataclass
class State:
"""The state of the thermostat."""

def __init__(self):
self.current_temperature = None
self.target_temperature = None
self.frost = None
self.action = None
self.mode = None
current_temperature = None
target_temperature = None
frost = None
action = None
mode = None
4 changes: 3 additions & 1 deletion salusfy/thermostat_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def __init__(self, name, client):
@property
def supported_features(self) -> ClimateEntityFeature:
"""Return the list of supported features."""
return ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.TURN_ON | ClimateEntityFeature.TURN_OFF
return (ClimateEntityFeature.TARGET_TEMPERATURE
| ClimateEntityFeature.TURN_ON
| ClimateEntityFeature.TURN_OFF)

@property
def name(self) -> str:
Expand Down
6 changes: 2 additions & 4 deletions tests/test_thermostat_entity.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import pytest
from unittest.mock import Mock
from homeassistant.components.climate.const import HVACMode
import pytest

from salusfy import (ThermostatEntity, State, WebClient)
from homeassistant.components.climate.const import (
HVACMode
)


@pytest.fixture
Expand Down

0 comments on commit 36337a5

Please sign in to comment.