Skip to content

Commit

Permalink
Fix linting errors (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturner authored Mar 1, 2024
1 parent 5fb9041 commit 205a5da
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 138 deletions.
2 changes: 1 addition & 1 deletion config.sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
ENABLE_TEMPERATURE_CLIENT = False
HOST = "your-home-assistant-ip-address"
ENTITY_ID = "sensor.your-temperature-sensor"
ACCESS_TOKEN = "your-HA-access-token"
ACCESS_TOKEN = "your-HA-access-token"
5 changes: 3 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import logging
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)


async def main():
registry = EntityRegistry()
config_adapter = ConfigAdapter(config)
Expand All @@ -36,7 +37,7 @@ async def main():
print("HVAC Mode: " + thermostat.hvac_mode)


if __name__ == '__main__':
if __name__ == '__main__':
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(main())
loop.run_until_complete(main())
31 changes: 17 additions & 14 deletions salusfy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,33 @@

_LOGGER = logging.getLogger(__name__)


class Client:
"""Mocks requests to Salus web application"""

def __init__(self, web_client : WebClient, temperature_client : HaTemperatureClient):
def __init__(
self,
web_client: WebClient,
temperature_client: HaTemperatureClient):
"""Initialize the client."""
self._state = None
self._web_client = web_client
self._temperature_client = temperature_client


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

_LOGGER.info("Delegating set_temperature to web client...")

await self._web_client.set_temperature(temperature)

self._state.target_temperature = temperature

self.assume_hvac_action()

self.assume_hvac_action()

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

_LOGGER.info("Delegating set_hvac_mode to web client...")

await self._web_client.set_hvac_mode(hvac_mode)
Expand All @@ -50,30 +52,31 @@ async def set_hvac_mode(self, hvac_mode : HVACMode) -> None:

self.assume_hvac_action()


def assume_hvac_action(self) -> None:
"""Assumes what the hvac action is based on
the mode and current/target temperatures"""
if self._state.mode == HVACMode.OFF:
_LOGGER.info("Assuming action is IDLE...")
self._state.action = HVACAction.IDLE
return

if self._state.target_temperature > self._state.current_temperature:
_LOGGER.info("Assuming action is HEATING based on target temperature...")
_LOGGER.info(
"Assuming action is HEATING based on target temperature...")
self._state.action = HVACAction.HEATING
return

_LOGGER.info("Assuming action is IDLE based on target temperature...")
self._state.action = HVACAction.IDLE


async def get_state(self) -> State:
"""Retrieves the status"""

if self._state is None:
_LOGGER.info("Delegating get_state to web client...")
self._state = await self._web_client.get_state()

_LOGGER.info("Updating current temperature from temperature client...")
self._state.current_temperature = await self._temperature_client.current_temperature()

return self._state
return self._state
58 changes: 33 additions & 25 deletions salusfy/climate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
"""
Adds support for the Salus Thermostat units.
"""
from . import (
ThermostatEntity,
Client,
WebClient,
HaTemperatureClient,
)
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.components.climate import PLATFORM_SCHEMA
from . import simulator
import logging

import homeassistant.helpers.config_validation as cv
Expand All @@ -18,18 +27,6 @@
CONF_SIMULATOR = 'simulator'
CONF_ENABLE_TEMPERATURE_CLIENT = 'enable_temperature_client'

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

from . import simulator

from homeassistant.components.climate import PLATFORM_SCHEMA

from homeassistant.helpers.reload import async_setup_reload_service

__version__ = "0.3.0"

Expand All @@ -44,25 +41,35 @@

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(
CONF_NAME,
default=DEFAULT_NAME): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_ID): cv.string,
vol.Optional(CONF_SIMULATOR, default=False): cv.boolean,
vol.Optional(CONF_ENABLE_TEMPERATURE_CLIENT, default=False): cv.boolean,
vol.Optional(CONF_ENTITY_ID, default=''): cv.string,
vol.Optional(CONF_ACCESS_TOKEN, default=''): cv.string,
vol.Optional(CONF_HOST, default='localhost'): cv.string
}
)
vol.Optional(
CONF_SIMULATOR,
default=False): cv.boolean,
vol.Optional(
CONF_ENABLE_TEMPERATURE_CLIENT,
default=False): cv.boolean,
vol.Optional(
CONF_ENTITY_ID,
default=''): cv.string,
vol.Optional(
CONF_ACCESS_TOKEN,
default=''): cv.string,
vol.Optional(
CONF_HOST,
default='localhost'): cv.string})


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)

client = create_client_from(config)

name = config.get(CONF_NAME)
async_add_entities(
[ThermostatEntity(name, client)], update_before_add=True
Expand All @@ -81,19 +88,20 @@ def create_client_from(config) -> Client:
return Client(simulator.WebClient(), simulator.TemperatureClient())

web_client = WebClient(username, password, id)

enable_temperature_client = config.get(CONF_ENABLE_TEMPERATURE_CLIENT)

if not enable_temperature_client:
_LOGGER.info('Registering Salus Thermostat client...')

return web_client

entity_id = config.get(CONF_ENTITY_ID)
host = config.get(CONF_HOST)
access_token = config.get(CONF_ACCESS_TOKEN)

_LOGGER.info('Registering Salus Thermostat client with Temperature client...')
_LOGGER.info(
'Registering Salus Thermostat client with Temperature client...')

ha_client = HaTemperatureClient(host, entity_id, access_token)
return Client(web_client, ha_client)
13 changes: 6 additions & 7 deletions salusfy/ha_temperature_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,33 @@
"""
import aiohttp


class HaTemperatureClient:
def __init__(self, host, entity_id, access_token):
self._entity_id = entity_id
self._host = host
self._access_token = access_token


async def current_temperature(self) -> float:
"""Gets the current temperature from HA"""

url = F"http://{self._host}:8123/api/states/{self._entity_id}"

headers = {
"Authorization": F"Bearer {self._access_token}",
"Content-Type": "application/json",
}


async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as response:

body = await response.json()

if 'state' not in body:
return None

state = body['state']
if state == 'unavailable':
return None

return float(state)
return float(state)
2 changes: 1 addition & 1 deletion salusfy/simulator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .temperature_client import TemperatureClient
from .web_client import WebClient
from .web_client import WebClient
4 changes: 3 additions & 1 deletion salusfy/simulator/temperature_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""
Adds support for simulating the Salus Thermostats.
"""


class TemperatureClient:
def __init__(self):
pass

async def current_temperature(self) -> float:
return 15.9
return 15.9
9 changes: 3 additions & 6 deletions salusfy/simulator/web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,20 @@ def __init__(self):
self._state.current_temperature = 15.1
self._state.frost = 10.1


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

_LOGGER.info("Setting temperature to %.1f...", temperature)

self._state.target_temperature = temperature

self._state.target_temperature = temperature

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

_LOGGER.info("Setting the HVAC mode to %s...", hvac_mode)

self._state.mode = hvac_mode


async def get_state(self) -> State:
"""Retrieves the mock status"""
return self._state
3 changes: 2 additions & 1 deletion salusfy/state.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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
self.mode = None
12 changes: 1 addition & 11 deletions salusfy/thermostat_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,38 +78,32 @@ def target_temperature(self) -> float:
"""Return the temperature we try to reach."""
return self._state.target_temperature


@property
def hvac_mode(self) -> HVACMode:
"""Return hvac operation ie. heat, cool mode."""

return self._state.mode


@property
def hvac_modes(self) -> list[HVACMode]:
"""HVAC modes."""
return [HVACMode.HEAT, HVACMode.OFF]


@property
def hvac_action(self) -> HVACAction:
"""Return the current running hvac operation."""
return self._state.action


@property
def preset_mode(self) -> str:
"""Return the current preset mode, e.g., home, away, temp."""
return PRESET_NONE


@property
def preset_modes(self) -> list[str]:
"""Return a list of available preset modes."""
return [PRESET_NONE]


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

Expand All @@ -122,23 +116,19 @@ async def async_set_temperature(self, **kwargs) -> None:

self._state.target_temperature = temperature


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

await self._client.set_hvac_mode(hvac_mode)

self._state.mode = hvac_mode


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


async def async_turn_on(self) -> None:
await self.async_set_hvac_mode(HVACMode.HEAT)


async def async_update(self) -> None:
"""Retrieve latest state data."""
self._state = await self._client.get_state()
Loading

0 comments on commit 205a5da

Please sign in to comment.