Skip to content

Commit

Permalink
Merge pull request #52 from DasBasti/precondition-switch
Browse files Browse the repository at this point in the history
Climate senors and precondition entity
  • Loading branch information
DasBasti authored Feb 19, 2024
2 parents 9bd80ac + 1c468e7 commit 08046be
Show file tree
Hide file tree
Showing 7 changed files with 666 additions and 3 deletions.
1 change: 1 addition & 0 deletions custom_components/smarthashtag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Platform.DEVICE_TRACKER,
# Platform.BINARY_SENSOR,
# Platform.SWITCH,
Platform.CLIMATE,
]


Expand Down
147 changes: 147 additions & 0 deletions custom_components/smarthashtag/climate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
"""Support for Smart #1 / #3 switches."""

from functools import cached_property
from typing import Any
from homeassistant.components.climate import ClimateEntity, ClimateEntityFeature
from homeassistant.components.climate.const import HVACMode
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature

from .coordinator import SmartHashtagDataUpdateCoordinator

from .const import (
CONF_CONDITIONING_TEMP,
CONF_VEHICLE,
DEFAULT_CONDITIONING_TEMP,
DOMAIN,
LOGGER,
)


async def async_setup_entry(hass: HomeAssistant, entry, async_add_entities):
"""Set up the Smart switches by config_entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]
vehicle = hass.data[DOMAIN][CONF_VEHICLE]
entities = []

entities.append(SmartConditioningMode(coordinator, vehicle))

async_add_entities(entities, update_before_add=True)


class SmartConditioningMode(ClimateEntity):
"""Representation of a Smart vehicle location device tracker."""

type = "conditioning mode"
_attr_max_temp = 30
_attr_min_temp = 16
_attr_hvac_modes = [HVACMode.HEAT, HVACMode.OFF]
_attr_hvac_mode = HVACMode.OFF
_attr_temperature_unit = UnitOfTemperature.CELSIUS
_attr_supported_features = (
ClimateEntityFeature.TARGET_TEMPERATURE
| ClimateEntityFeature.TURN_OFF
| ClimateEntityFeature.TURN_ON
)
_attr_entity_category = EntityCategory.CONFIG
_attr_has_entity_name = True
_attr_should_poll = False
_attr_icon = "mdi:thermostat-auto"
_enable_turn_on_off_backwards_compatibility = False

def __init__(
self,
coordinator: SmartHashtagDataUpdateCoordinator,
vehicle: str,
) -> None:
"""Initialize the Contitioner class."""
super().__init__()
self.coordinator = coordinator
self._vehicle = self.coordinator.account.vehicles[vehicle]
self._attr_name = f"Smart {vehicle} Conditioning"
self._attr_unique_id = f"{self._attr_unique_id}_{vehicle}"
self._temperature = self.coordinator.config_entry.options.get(
CONF_CONDITIONING_TEMP, DEFAULT_CONDITIONING_TEMP
)
self._attr_target_temperature = self._temperature

async def async_turn_on(self) -> None:
"""Turn on the climate system."""
await self._vehicle.climate_control.set_climate_conditioning(
self._temperature, True
)
await self.async_set_hvac_mode(HVACMode.HEAT)

async def async_turn_off(self) -> None:
"""Turn off the climate system."""
await self._vehicle.climate_control.set_climate_conditioning(
self._temperature, False
)
await self.async_set_hvac_mode(HVACMode.OFF)

async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature for the vehicle."""
temperature = kwargs[ATTR_TEMPERATURE]
if temperature is not None:
self._temperature = temperature
self.async_write_ha_state()

async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set new target hvac mode."""
if hvac_mode is not None:
self._attr_hvac_mode = hvac_mode
if hvac_mode == "heat":
await self.async_turn_on()
else:
await self.async_turn_off()
self.async_write_ha_state()

@cached_property
def current_temperature(self) -> float | None:
"""Return the current temperature."""
LOGGER.warning(self._vehicle.climate.interior_temperature.value)
if self._vehicle.climate.interior_temperature.value is not None:
return self._vehicle.climate.interior_temperature.value
else:
return self._attr_current_temperature.value

def set_fan_mode(self, fan_mode: str) -> None:
"""Set the fan mode."""
raise NotImplementedError

def set_humidity(self, humidity: float) -> None:
"""Set the humidity level."""
raise NotImplementedError

def set_hvac_mode(self, hvac_mode: str) -> None:
"""Set the HVAC mode."""
raise NotImplementedError

def set_preset_mode(self, preset_mode: str) -> None:
"""Set the preset mode."""
raise NotImplementedError

def set_swing_mode(self, swing_mode: str) -> None:
"""Set the swing mode."""
raise NotImplementedError

def set_temperature(self, **kwargs: Any) -> None:
"""Set the target temperature."""
raise NotImplementedError

def turn_aux_heat_off(self) -> None:
"""Turn off the auxiliary heat."""
raise NotImplementedError

def turn_aux_heat_on(self) -> None:
"""Turn on the auxiliary heat."""
raise NotImplementedError

def turn_off(self) -> None:
"""Turn off the climate system."""
raise NotImplementedError

def turn_on(self) -> None:
"""Turn on the climate system."""
raise NotImplementedError
8 changes: 7 additions & 1 deletion custom_components/smarthashtag/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
# Platforms
SENSOR = "sensor"
DEVICE_TRACKER = "device_tracker"
PLATFORMS = [SENSOR, DEVICE_TRACKER]
SWITCH = "switch"
CLIMATE = "climate"
PLATFORMS = [SENSOR, DEVICE_TRACKER, CLIMATE, SWITCH]


# Configuration and options
Expand All @@ -29,13 +31,17 @@
CONF_PASSWORD = "password"
CONF_CHARGING_INTERVAL = "charging_interval"
CONF_DRIVING_INTERVAL = "driving_interval"
CONF_CONDITIONING_TEMP = "conditioning_temp"
CONF_SEATHEATING_LEVEL = "seatheating_level"

# Defaults
DEFAULT_NAME = DOMAIN
DEFAULT_SCAN_INTERVAL = 300
DEFAULT_CHARGING_INTERVAL = 30
DEFAULT_DRIVING_INTERVAL = 60
MIN_SCAN_INTERVAL = 10
DEFAULT_CONDITIONING_TEMP = 21
DEFAULT_SEATHEATING_LEVEL = 3


STARTUP_MESSAGE = f"""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/smarthashtag/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"integration_type": "device",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/DasBasti/SmartHashtag/issues",
"requirements": ["pysmarthashtag==0.2.8"],
"requirements": ["pysmarthashtag==0.2.9"],
"version": "0.2.5"
}
Loading

0 comments on commit 08046be

Please sign in to comment.