Skip to content

Commit b42cdc7

Browse files
authored
Merge pull request #98 from jwillemsen/jwi-testsensors
First draft code for sensor tests
2 parents 3063534 + 92d9b0d commit b42cdc7

24 files changed

+37164
-186
lines changed

.pre-commit-config.yaml

+8-10
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ repos:
66
- id: check-yaml
77
- id: end-of-file-fixer
88
- id: trailing-whitespace
9-
- repo: local
9+
- repo: https://github.com/astral-sh/ruff-pre-commit
10+
rev: v0.1.14
1011
hooks:
11-
- id: black
12-
name: black
13-
entry: black
14-
language: system
15-
types: [python]
16-
require_serial: true
17-
- repo: https://github.com/pre-commit/mirrors-prettier
18-
rev: v3.1.0
12+
- id: ruff
13+
args: [--fix]
14+
- id: ruff-format
15+
- repo: https://github.com/asottile/reorder-python-imports
16+
rev: v3.12.0
1917
hooks:
20-
- id: prettier
18+
- id: reorder-python-imports

custom_components/daikin_onecta/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
"""Platform for the Daikin AC."""
2-
32
import asyncio
43
import logging
54

65
from aiohttp import ClientError
76
from homeassistant.config_entries import ConfigEntry
87
from homeassistant.config_entries import SOURCE_IMPORT
98
from homeassistant.const import SERVICE_RELOAD
9+
from homeassistant.core import HomeAssistant
1010
from homeassistant.exceptions import ConfigEntryNotReady
1111
from homeassistant.helpers import config_entry_oauth2_flow
12-
from homeassistant.core import HomeAssistant
1312

1413
from .const import COORDINATOR
1514
from .const import DAIKIN_API

custom_components/daikin_onecta/application_credentials.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from homeassistant.components.application_credentials import AuthorizationServer
22
from homeassistant.core import HomeAssistant
3+
34
from .const import OAUTH2_AUTHORIZE
45
from .const import OAUTH2_TOKEN
56

custom_components/daikin_onecta/climate.py

+26-76
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""Support for the Daikin HVAC."""
2-
32
import logging
43
import re
54

@@ -9,7 +8,6 @@
98
from homeassistant.components.climate import PLATFORM_SCHEMA
109
from homeassistant.components.climate.const import ATTR_HVAC_MODE
1110
from homeassistant.components.climate.const import ClimateEntityFeature
12-
from homeassistant.components.climate.const import FAN_AUTO
1311
from homeassistant.components.climate.const import HVACMode
1412
from homeassistant.components.climate.const import PRESET_AWAY
1513
from homeassistant.components.climate.const import PRESET_BOOST
@@ -74,9 +72,11 @@ async def async_setup_entry(hass, entry, async_add_entities):
7472
device_model = device.daikin_data["deviceModel"]
7573
supported_management_point_types = {"climateControl"}
7674
managementPoints = device.daikin_data.get("managementPoints", [])
75+
embedded_id = ""
7776
for management_point in managementPoints:
7877
management_point_type = management_point["managementPointType"]
7978
if management_point_type in supported_management_point_types:
79+
embedded_id = management_point.get("embeddedId")
8080
# Check if we have a temperatureControl
8181
temperatureControl = management_point.get("temperatureControl")
8282
if temperatureControl is not None:
@@ -88,7 +88,10 @@ async def async_setup_entry(hass, entry, async_add_entities):
8888
modes = list(dict.fromkeys(modes))
8989
_LOGGER.info("Climate: Device '%s' has modes %s", device_model, modes)
9090
for mode in modes:
91-
async_add_entities([DaikinClimate(device, mode, coordinator)], update_before_add=False)
91+
async_add_entities(
92+
[DaikinClimate(device, mode, coordinator, embedded_id)],
93+
update_before_add=False,
94+
)
9295

9396

9497
class DaikinClimate(CoordinatorEntity, ClimateEntity):
@@ -98,7 +101,7 @@ class DaikinClimate(CoordinatorEntity, ClimateEntity):
98101

99102
# Setpoint is the setpoint string under
100103
# temperatureControl/value/operationsModes/mode/setpoints, for example roomTemperature/leavingWaterOffset
101-
def __init__(self, device, setpoint, coordinator):
104+
def __init__(self, device, setpoint, coordinator, embedded_id):
102105
"""Initialize the climate device."""
103106
super().__init__(coordinator)
104107
_LOGGER.info(
@@ -107,9 +110,13 @@ def __init__(self, device, setpoint, coordinator):
107110
setpoint,
108111
)
109112
self._device = device
113+
self._embedded_id = embedded_id
110114
self._setpoint = setpoint
111-
self._attr_supported_features = self.get_supported_features()
112115
self._attr_temperature_unit = UnitOfTemperature.CELSIUS
116+
self.update_state()
117+
118+
def update_state(self) -> None:
119+
self._attr_supported_features = self.get_supported_features()
113120
self._attr_current_temperature = self.get_current_temperature()
114121
self._attr_max_temp = self.get_max_temp()
115122
self._attr_min_temp = self.get_min_temp()
@@ -126,25 +133,9 @@ def __init__(self, device, setpoint, coordinator):
126133

127134
@callback
128135
def _handle_coordinator_update(self) -> None:
129-
self._attr_supported_features = self.get_supported_features()
130-
self._attr_current_temperature = self.get_current_temperature()
131-
self._attr_max_temp = self.get_max_temp()
132-
self._attr_min_temp = self.get_min_temp()
133-
self._attr_target_temperature_step = self.get_target_temperature_step()
134-
self._attr_target_temperature = self.get_target_temperature()
135-
self._attr_hvac_modes = self.get_hvac_modes()
136-
self._attr_swing_modes = self.get_swing_modes()
137-
self._attr_preset_modes = self.get_preset_modes()
138-
self._attr_fan_modes = self.get_fan_modes()
139-
self._attr_hvac_mode = self.get_hvac_mode()
140-
self._attr_swing_mode = self.get_swing_mode()
141-
self._attr_preset_mode = self.get_preset_mode()
142-
self._attr_fan_mode = self.get_fan_mode()
136+
self.update_state()
143137
self.async_write_ha_state()
144138

145-
async def _set(self, settings):
146-
raise NotImplementedError
147-
148139
def climate_control(self):
149140
cc = None
150141
supported_management_point_types = {"climateControl"}
@@ -179,29 +170,6 @@ def setpoint(self):
179170
)
180171
return setpoint
181172

182-
# Return the dictionary fanControl for the current operationMode
183-
def fan_control(self):
184-
fancontrol = None
185-
supported_management_point_types = {"climateControl"}
186-
if self._device.daikin_data["managementPoints"] is not None:
187-
for management_point in self._device.daikin_data["managementPoints"]:
188-
management_point_type = management_point["managementPointType"]
189-
if management_point_type in supported_management_point_types:
190-
# Check if we have a temperatureControl
191-
temperatureControl = management_point.get("fanControl")
192-
_LOGGER.info("Climate: Device fanControl %s", temperatureControl)
193-
if temperatureControl is not None:
194-
operationMode = management_point.get("operationMode").get("value")
195-
fancontrol = temperatureControl["value"]["operationModes"][operationMode].get(self._setpoint)
196-
_LOGGER.info(
197-
"Device '%s': %s operation mode %s has fanControl %s",
198-
self._device.name,
199-
self._setpoint,
200-
operationMode,
201-
fancontrol,
202-
)
203-
return fancontrol
204-
205173
def sensory_data(self):
206174
sensoryData = None
207175
supported_management_point_types = {"climateControl"}
@@ -226,11 +194,6 @@ def sensory_data(self):
226194
def translation_key(self) -> str:
227195
return "daikin_onecta"
228196

229-
@property
230-
def embedded_id(self):
231-
cc = self.climate_control()
232-
return cc["embeddedId"]
233-
234197
@property
235198
def available(self):
236199
"""Return the availability of the underlying device."""
@@ -355,7 +318,7 @@ async def async_set_temperature(self, **kwargs):
355318
value = kwargs[ATTR_TEMPERATURE]
356319
res = await self._device.set_path(
357320
self._device.getId(),
358-
self.embedded_id,
321+
self._embedded_id,
359322
"temperatureControl",
360323
f"/operationModes/{omv}/setpoints/{self._setpoint}",
361324
value,
@@ -411,7 +374,7 @@ async def async_set_hvac_mode(self, hvac_mode):
411374

412375
# Only set the on/off to Daikin when we need to change it
413376
if on_off_mode is not None:
414-
result &= await self._device.set_path(self._device.getId(), self.embedded_id, "onOffMode", "", on_off_mode)
377+
result &= await self._device.set_path(self._device.getId(), self._embedded_id, "onOffMode", "", on_off_mode)
415378
if result is False:
416379
_LOGGER.warning(
417380
"Device '%s' problem setting onOffMode to %s",
@@ -427,7 +390,7 @@ async def async_set_hvac_mode(self, hvac_mode):
427390
if operation_mode != cc["operationMode"]["value"]:
428391
result &= await self._device.set_path(
429392
self._device.getId(),
430-
self.embedded_id,
393+
self._embedded_id,
431394
"operationMode",
432395
"",
433396
operation_mode,
@@ -442,22 +405,9 @@ async def async_set_hvac_mode(self, hvac_mode):
442405
cc["operationMode"]["value"] = operation_mode
443406

444407
if result is True:
445-
self._attr_hvac_mode = hvac_mode
446408
# When switching hvac mode it could be that we can set min/max/target/etc
447409
# which we couldn't set with a previous hvac mode
448-
self._attr_supported_features = self.get_supported_features()
449-
self._attr_current_temperature = self.get_current_temperature()
450-
self._attr_max_temp = self.get_max_temp()
451-
self._attr_min_temp = self.get_min_temp()
452-
self._attr_target_temperature_step = self.get_target_temperature_step()
453-
self._attr_target_temperature = self.get_target_temperature()
454-
self._attr_swing_modes = self.get_swing_modes()
455-
self._attr_preset_modes = self.get_preset_modes()
456-
self._attr_fan_modes = self.get_fan_modes()
457-
self._attr_swing_mode = self.get_swing_mode()
458-
self._attr_preset_mode = self.get_preset_mode()
459-
self._attr_fan_mode = self.get_fan_mode()
460-
410+
self.update_state()
461411
self.async_write_ha_state()
462412

463413
return result
@@ -522,7 +472,7 @@ async def async_set_fan_mode(self, fan_mode):
522472
if fan_mode.isnumeric():
523473
res = await self._device.set_path(
524474
self._device.getId(),
525-
self.embedded_id,
475+
self._embedded_id,
526476
"fanControl",
527477
f"/operationModes/{operationmode}/fanSpeed/currentMode",
528478
"fixed",
@@ -536,7 +486,7 @@ async def async_set_fan_mode(self, fan_mode):
536486
new_fixed_mode = int(fan_mode)
537487
res &= await self._device.set_path(
538488
self._device.getId(),
539-
self.embedded_id,
489+
self._embedded_id,
540490
"fanControl",
541491
f"/operationModes/{operationmode}/fanSpeed/modes/fixed",
542492
new_fixed_mode,
@@ -550,7 +500,7 @@ async def async_set_fan_mode(self, fan_mode):
550500
else:
551501
res = await self._device.set_path(
552502
self._device.getId(),
553-
self.embedded_id,
503+
self._embedded_id,
554504
"fanControl",
555505
f"/operationModes/{operationmode}/fanSpeed/currentMode",
556506
fan_mode,
@@ -671,7 +621,7 @@ async def async_set_swing_mode(self, swing_mode):
671621
new_h_mode = "swing"
672622
res &= await self._device.set_path(
673623
self._device.getId(),
674-
self.embedded_id,
624+
self._embedded_id,
675625
"fanControl",
676626
f"/operationModes/{operation_mode}/fanDirection/horizontal/currentMode",
677627
new_h_mode,
@@ -699,7 +649,7 @@ async def async_set_swing_mode(self, swing_mode):
699649
new_v_mode = "windNice"
700650
res &= await self._device.set_path(
701651
self._device.getId(),
702-
self.embedded_id,
652+
self._embedded_id,
703653
"fanControl",
704654
f"/operationModes/{operation_mode}/fanDirection/vertical/currentMode",
705655
new_v_mode,
@@ -735,7 +685,7 @@ async def async_set_preset_mode(self, preset_mode):
735685

736686
if self.preset_mode != PRESET_NONE:
737687
current_mode = HA_PRESET_TO_DAIKIN[self.preset_mode]
738-
result &= await self._device.set_path(self._device.getId(), self.embedded_id, current_mode, "", "off")
688+
result &= await self._device.set_path(self._device.getId(), self._embedded_id, current_mode, "", "off")
739689
if result is False:
740690
_LOGGER.warning(
741691
"Device '%s' problem setting %s to off",
@@ -747,7 +697,7 @@ async def async_set_preset_mode(self, preset_mode):
747697
if self.hvac_mode == HVACMode.OFF and preset_mode == PRESET_BOOST:
748698
result &= await self.async_turn_on()
749699

750-
result &= await self._device.set_path(self._device.getId(), self.embedded_id, new_daikin_mode, "", "on")
700+
result &= await self._device.set_path(self._device.getId(), self._embedded_id, new_daikin_mode, "", "on")
751701
if result is False:
752702
_LOGGER.warning(
753703
"Device '%s' problem setting %s to on",
@@ -785,7 +735,7 @@ async def async_turn_on(self):
785735
cc = self.climate_control()
786736
result = True
787737
if cc["onOffMode"]["value"] == "off":
788-
result &= await self._device.set_path(self._device.getId(), self.embedded_id, "onOffMode", "", "on")
738+
result &= await self._device.set_path(self._device.getId(), self._embedded_id, "onOffMode", "", "on")
789739
if result is False:
790740
_LOGGER.error("Device '%s' problem setting onOffMode to on", self._device.name)
791741
else:
@@ -805,7 +755,7 @@ async def async_turn_off(self):
805755
cc = self.climate_control()
806756
result = True
807757
if cc["onOffMode"]["value"] == "on":
808-
result &= await self._device.set_path(self._device.getId(), self.embedded_id, "onOffMode", "", "off")
758+
result &= await self._device.set_path(self._device.getId(), self._embedded_id, "onOffMode", "", "off")
809759
if result is False:
810760
_LOGGER.error("Device '%s' problem setting onOffMode to off", self._device.name)
811761
else:

custom_components/daikin_onecta/config_flow.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""Config flow for the Daikin platform."""
2-
32
import logging
43
from collections.abc import Mapping
54
from typing import Any
@@ -20,7 +19,7 @@
2019

2120

2221
class OptionsFlowHandler(config_entries.OptionsFlow):
23-
"""Config flow options handler for myenergi."""
22+
"""Config flow options handler for daikin_onecta ."""
2423

2524
def __init__(self, config_entry):
2625
"""Initialize HACS options flow."""

custom_components/daikin_onecta/const.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""Constants for Daikin Oncecta."""
2-
32
from homeassistant.components.sensor import CONF_STATE_CLASS
43
from homeassistant.components.sensor import SensorDeviceClass
54
from homeassistant.components.sensor import SensorStateClass

custom_components/daikin_onecta/coordinator.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""Coordinator for Daikin Onecta integration."""
2-
32
from __future__ import annotations
43

54
import logging
@@ -25,7 +24,12 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
2524
"""Initialize."""
2625
self.options = config_entry.options
2726

28-
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=self.determine_update_interval(hass))
27+
super().__init__(
28+
hass,
29+
_LOGGER,
30+
name=DOMAIN,
31+
update_interval=self.determine_update_interval(hass),
32+
)
2933

3034
_LOGGER.info(
3135
"Daikin coordinator initialized with %s interval.",

custom_components/daikin_onecta/daikin_api.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""Platform for the Daikin AC."""
2-
32
import asyncio
43
import functools
54
import logging
@@ -13,7 +12,6 @@
1312

1413
from .const import DAIKIN_DEVICES
1514
from .const import DOMAIN
16-
from .daikin_base import Appliance
1715

1816
_LOGGER = logging.getLogger(__name__)
1917

@@ -40,7 +38,14 @@ def __init__(
4038
self._last_patch_call = datetime.min
4139

4240
# Store the limits as member so that we can add these to the diagnostics
43-
self.rate_limits = {"minute": 0, "day": 0, "remaining_minutes": 0, "remaining_day": 0, "retry_after": 0, "ratelimit_reset": 0}
41+
self.rate_limits = {
42+
"minute": 0,
43+
"day": 0,
44+
"remaining_minutes": 0,
45+
"remaining_day": 0,
46+
"retry_after": 0,
47+
"ratelimit_reset": 0,
48+
}
4449

4550
# The following lock is used to serialize http requests to Daikin cloud
4651
# to prevent receiving old settings while a PATCH is ongoing.

custom_components/daikin_onecta/daikin_base.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""Pydaikin base appliance, represent a Daikin device."""
2-
32
import logging
43

54
from .device import DaikinOnectaDevice

0 commit comments

Comments
 (0)