Skip to content

Commit

Permalink
tetienne review
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarec committed Mar 3, 2022
1 parent 1a45060 commit 0336ac9
Showing 1 changed file with 17 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
from typing import List, Optional

from pyoverkiz.enums import OverkizState

from homeassistant.components.climate import (
SUPPORT_PRESET_MODE,
SUPPORT_TARGET_TEMPERATURE,
Expand All @@ -15,15 +17,7 @@
PRESET_COMFORT,
PRESET_ECO,
)
from homeassistant.const import (
ATTR_TEMPERATURE,
EVENT_HOMEASSISTANT_START,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
TEMP_CELSIUS,
)
from homeassistant.core import callback
from homeassistant.helpers.event import async_track_state_change
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS

from ..coordinator import OverkizDataUpdateCoordinator
from ..entity import OverkizEntity
Expand Down Expand Up @@ -92,15 +86,15 @@ class AtlanticPassAPCHeatingZone(OverkizEntity, ClimateEntity):
"""Representation of Atlantic Pass APC Heating and Cooling Zone."""

_attr_hvac_modes = [HVAC_MODE_OFF, HVAC_MODE_HEAT, HVAC_MODE_AUTO]
_attr_max_temp = 30
_attr_min_temp = 5
_attr_supported_features = SUPPORT_PRESET_MODE | SUPPORT_TARGET_TEMPERATURE
_attr_temperature_unit = TEMP_CELSIUS

def __init__(self, device_url: str, coordinator: OverkizDataUpdateCoordinator):
"""Init method."""
super().__init__(device_url, coordinator)

self._temp_sensor_entity_id = None
self._current_temperature = None
self.temperature_device = self.executor.linked_device(8)

@property
def preset_modes(self) -> Optional[List[str]]:
Expand All @@ -118,14 +112,14 @@ def preset_modes(self) -> Optional[List[str]]:
CUSTOM_PRESET_STOP,
PRESET_AWAY,
]
else:
return [
PRESET_COMFORT,
PRESET_ECO,
CUSTOM_PRESET_AUTO,
CUSTOM_PRESET_STOP,
PRESET_AWAY,
]

return [
PRESET_COMFORT,
PRESET_ECO,
CUSTOM_PRESET_AUTO,
CUSTOM_PRESET_STOP,
PRESET_AWAY,
]

@property
def preset_mode(self) -> Optional[str]:
Expand Down Expand Up @@ -168,80 +162,12 @@ async def async_set_preset_mode(self, preset_mode: str) -> None:
)
await self.refresh_values()

async def async_added_to_hass(self):
"""Register temperature sensor after added to hass."""

await super().async_added_to_hass()
entity_registry = await self.hass.helpers.entity_registry.async_get_registry()

# The linked temperature sensor uses subsystem_id + 1
new_subsystem_id = int(self.device_url.split("#", 1)[1]) + 1
self._temp_sensor_entity_id = next(
(
entity_id
for entity_id, entry in entity_registry.entities.items()
if entry.unique_id
== f"{self.base_device_url}#{str(new_subsystem_id)}-core:TemperatureState"
),
None,
)

if self._temp_sensor_entity_id:
async_track_state_change(
self.hass, self._temp_sensor_entity_id, self._async_temp_sensor_changed
)
else:
_LOGGER.warning(
"Temperature sensor could not be found for entity %s %s %s",
self.name,
self.device_url,
new_subsystem_id,
)

@callback
def _async_startup(event):
"""Init on startup."""
if self._temp_sensor_entity_id:
temp_sensor_state = self.hass.states.get(self._temp_sensor_entity_id)
if temp_sensor_state and temp_sensor_state.state != STATE_UNKNOWN:
self.update_temp(temp_sensor_state)

self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _async_startup)
self.schedule_update_ha_state(True)

async def _async_temp_sensor_changed(self, entity_id, old_state, new_state) -> None:
"""Handle temperature changes."""
if new_state is None or old_state == new_state:
return

self.update_temp(new_state)
self.schedule_update_ha_state()

@callback
def update_temp(self, state):
"""Update thermostat with latest state from sensor."""
if state is None or state.state in [STATE_UNKNOWN, STATE_UNAVAILABLE]:
return

try:
self._current_temperature = float(state.state)
except ValueError as ex:
_LOGGER.error("Unable to update from sensor: %s", ex)

@property
def min_temp(self) -> float:
"""Return the minimum temperature."""
return 5

@property
def max_temp(self) -> float:
"""Return the maximum temperature."""
return 30

@property
def current_temperature(self) -> Optional[float]:
"""Return the current temperature."""
return self._current_temperature
return float(
self.temperature_device.states.get(OverkizState.CORE_TEMPERATURE).value
)

@property
def hvac_mode(self) -> str:
Expand Down

0 comments on commit 0336ac9

Please sign in to comment.