diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index 669f0162a..e9b9c1102 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -4,7 +4,7 @@ name: Latest commit env: - CACHE_VERSION: 16 + CACHE_VERSION: 1 DEFAULT_PYTHON: "3.9" PRE_COMMIT_HOME: ~/.cache/pre-commit @@ -49,7 +49,7 @@ jobs: python -m venv venv . venv/bin/activate pip install -U pip setuptools wheel - pip install -r requirements_test.txt + pip install -r requirements_test.txt -r requirements_commit.txt - name: Restore pre-commit environment from cache id: cache-precommit uses: actions/cache@v2 @@ -195,7 +195,10 @@ jobs: python -m venv venv . venv/bin/activate pip install -U pip setuptools wheel - pip install -r requirements_test.txt + #pip install -r requirements_test.txt + # 20220124 Mimic setup_test.sh + pip install --upgrade -r requirements_test.txt -c https://raw.githubusercontent.com/home-assistant/core/dev/homeassistant/package_constraints.txt -r https://raw.githubusercontent.com/home-assistant/core/dev/requirements_test.txt -r https://raw.githubusercontent.com/home-assistant/core/dev/requirements_test_pre_commit.txt + pip install --upgrade pytest-asyncio pytest: runs-on: ubuntu-latest diff --git a/CHANGELOG.md b/CHANGELOG.md index afdd60b1c..4aaaee262 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +# v0.16.1 - Smile - various updates: + - BREAKING: Change active device detecting, detect both OpenTherm (replace Auxiliary) and OnOff (new) heating and cooling devices. + - Add Adam v3.6.4 (beta) and Anna firmware 4.2 support (representation and switching on/off of a schedule has changed) + - Anna: Fix cooling_active prediction + - Schedules: always show `available_schemas` and `selected_schema`, also with "None" available and/or selected + - Cleanup and optimize code + - Adapt and improve testcode + - Support stretch without domain_data (firmware 2.7) + # v0.16.0 - Smile - Change output format, allowing full use of Core DataUpdateCoordintor in plugwise-beta - Change from list- to dict-format for binary_sensors, sensors and switches - Provide gateway-devices for Legacy Anna and Stretch diff --git a/plugwise/__init__.py b/plugwise/__init__.py index 3b2e0f2cc..82c37f0f8 100644 --- a/plugwise/__init__.py +++ b/plugwise/__init__.py @@ -1,6 +1,6 @@ """Plugwise module.""" -__version__ = "0.16.0" +__version__ = "0.16.1a27" from plugwise.smile import Smile from plugwise.stick import Stick diff --git a/plugwise/constants.py b/plugwise/constants.py index 76e22ca2f..494e47d3a 100644 --- a/plugwise/constants.py +++ b/plugwise/constants.py @@ -356,13 +356,14 @@ ### Smile constants ### APPLIANCES = "/core/appliances" +DIRECT_OBJECTS = "/core/direct_objects" DOMAIN_OBJECTS = "/core/domain_objects" LOCATIONS = "/core/locations" MODULES = "/core/modules" NOTIFICATIONS = "/core/notifications" RULES = "/core/rules" SYSTEM = "/system" -STATUS = "/system/status.xml" +STATUS = "/system/status" ATTR_TYPE = "type" DEFAULT_TIMEOUT = 30 diff --git a/plugwise/helper.py b/plugwise/helper.py index 781e9ca2c..d28e13aaa 100644 --- a/plugwise/helper.py +++ b/plugwise/helper.py @@ -26,7 +26,6 @@ BINARY_SENSORS, COOLING_ICON, DEVICE_MEASUREMENTS, - DOMAIN_OBJECTS, ENERGY_KILO_WATT_HOUR, ENERGY_WATT_HOUR, FAKE_LOC, @@ -138,25 +137,43 @@ def check_model(name, v_name): return name -def schemas_schedule_temp(schedules): +def schemas_schedule_temp(schedules, name): """Helper-function for schemas(). Obtain the schedule temperature of the schema/schedule. """ - for period, temp in schedules.items(): - moment_1, moment_2 = period.split(",") - moment_1 = moment_1.replace("[", "").split(" ") - moment_2 = moment_2.replace(")", "").split(" ") - result_1 = DAYS.get(moment_1[0], "None") - result_2 = DAYS.get(moment_2[0], "None") + if name == "None": + return # pragma: no cover + + schema_list = [] + for period, temp in schedules[name].items(): + tmp_list = [] + moment, dummy = period.split(",") + moment = moment.replace("[", "").split(" ") + day_nr = DAYS.get(moment[0], "None") + start_time = dt.datetime.strptime(moment[1], "%H:%M").time() + tmp_list.extend((day_nr, start_time, temp)) + schema_list.append(tmp_list) + + length = len(schema_list) + schema_list = sorted(schema_list) + + # Schema with less than 2 items + if length == 1: + return schema_list[0][2] + + for i in range(length): + result_1 = schema_list[i][0] + start = schema_list[i][1] + n = (i + 1) % (length - 1) + result_2 = schema_list[n][0] + end = schema_list[n][1] now = dt.datetime.now().time() - start = dt.datetime.strptime(moment_1[1], "%H:%M").time() - end = dt.datetime.strptime(moment_2[1], "%H:%M").time() if ( result_1 == dt.datetime.now().weekday() or result_2 == dt.datetime.now().weekday() ): if in_between(now, start, end): - return temp + return schema_list[i][2] def types_finder(data): @@ -277,17 +294,21 @@ async def _request( url = f"{self._endpoint}{command}" try: + + if method == "delete": + resp = await self._websession.delete(url, auth=self._auth) if method == "get": # Work-around for Stretchv2, should not hurt the other smiles headers = {"Accept-Encoding": "gzip"} - resp = await self._websession.get(url, auth=self._auth, headers=headers) + resp = await self._websession.get(url, headers=headers, auth=self._auth) if method == "put": headers = {"Content-type": "text/xml"} resp = await self._websession.put( - url, data=data, headers=headers, auth=self._auth + url, + headers=headers, + data=data, + auth=self._auth, ) - if method == "delete": - resp = await self._websession.delete(url, auth=self._auth) except ServerTimeoutError: if retry < 1: _LOGGER.error("Timed out sending command to Plugwise: %s", command) @@ -434,7 +455,7 @@ def _energy_device_info_finder(self, appliance, appl): return appl def _appliance_info_finder(self, appliance, appl): - """Collect device info (Smile/Stretch, Thermostats, Auxiliary): firmware, model and vendor name.""" + """Collect device info (Smile/Stretch, Thermostats, OpenTherm/On-Off): firmware, model and vendor name.""" # Find gateway and heater_central devices if appl.pwclass == "gateway": self.gateway_id = appliance.attrib["id"] @@ -467,12 +488,21 @@ def _appliance_info_finder(self, appliance, appl): return appl if appl.pwclass == "heater_central": + # Provide info for On-Off device + if self._on_off_device: + self._heater_id = appliance.attrib["id"] + appl.name = "OnOff" + appl.v_name = None + appl.model = "Unknown" + + return appl + # Remove heater_central when no active device present - if not self._active_device_present: + if not self._ot_device and not self._on_off_device: return None self._heater_id = appliance.attrib["id"] - appl.name = "Auxiliary" + appl.name = "OpenTherm" locator1 = ".//logs/point_log[type='flame_state']/boiler_state" locator2 = ".//services/boiler_state" mod_type = "boiler_state" @@ -483,7 +513,9 @@ def _appliance_info_finder(self, appliance, appl): appl.model = check_model(module_data[1], appl.v_name) if appl.model is None: appl.model = ( - "Generic heater/cooler" if self._cp_state else "Generic heater" + "Generic heater/cooler" + if self._cooling_present + else "Generic heater" ) return appl @@ -526,54 +558,44 @@ def _all_appliances(self): self._all_locations() - # Create a gateway for legacy P1 and Anna - # And inject a home_location as device id for legacy so + # Create a gateway for legacy Anna, P1 and Stretches + # and inject a home_location as device id for legacy so # appl_data can use the location id as device id, where needed. if self._smile_legacy: - if self.smile_type == "power": - self._appl_data[self._home_location] = { - "class": "gateway", - "fw": self.smile_version[0], - "location": self._home_location, - "model": "P1", - "name": "P1", - "vendor": "Plugwise B.V.", - } - self.gateway_id = self._home_location - # legacy p1 has no more devices - return - - elif self.smile_type == "thermostat": - self._appl_data[self._home_location] = { - "class": "gateway", - "fw": self.smile_version[0], - "location": self._home_location, - "model": "Anna", - "name": "Anna", - "vendor": "Plugwise B.V.", - } - self.gateway_id = self._home_location - - # Create a gateway for the Stretches - if self.smile_type == "stretch": - self._appl_data[self.gateway_id] = { + self._appl_data[self._home_location] = { "class": "gateway", "fw": self.smile_version[0], "location": self._home_location, - "model": "Stretch", - "name": "Stretch", "vendor": "Plugwise B.V.", } + self.gateway_id = self._home_location + + if self.smile_type == "power": + self._appl_data[self._home_location].update( + {"model": "P1", "name": "P1"} + ) + # legacy p1 has no more devices + return + + if self.smile_type == "thermostat": + self._appl_data[self._home_location].update( + {"model": "Anna", "name": "Anna"} + ) + + if self.smile_type == "stretch": + self._appl_data[self._home_location].update( + {"model": "Stretch", "name": "Stretch"} + ) # The presence of either indicates a local active device, e.g. heat-pump or gas-fired heater - self._cp_state = self._appliances.find( - ".//logs/point_log[type='compressor_state']" + ch_state = self._appliances.find( + ".//logs/point_log[type='central_heating_state']" ) - fl_state = self._appliances.find(".//logs/point_log[type='flame_state']") - bl_state = self._appliances.find(".//services/boiler_state") - self._active_device_present = ( - self._cp_state is not None or fl_state is not None or bl_state is not None + ot_fault_code = self._appliances.find( + ".//logs/point_log[type='open_therm_oem_fault_code']" ) + self._ot_device = ch_state is not None and ot_fault_code is not None + self._on_off_device = ch_state is not None and ot_fault_code is None for appliance in self._appliances.findall("./appliance"): appl = Munch() @@ -669,7 +691,7 @@ def _presets(self, loc_id): if not (rule_ids := self._rule_ids_by_tag(tag_1, loc_id)): if not (rule_ids := self._rule_ids_by_name(tag_2, loc_id)): - return presets + return presets # pragma: no cover for rule_id in rule_ids: directives = self._domain_objects.find(f'rule[@id="{rule_id}"]/directives') @@ -689,19 +711,21 @@ def _presets(self, loc_id): def _rule_ids_by_name(self, name, loc_id): """Helper-function for _presets(). - Obtain the rule_id from the given name and location_id. + Obtain the rule_id from the given name and and provide the location_id, when present. """ schema_ids = {} locator = f'.//contexts/context/zone/location[@id="{loc_id}"]' for rule in self._domain_objects.findall(f'.//rule[name="{name}"]'): if rule.find(locator) is not None: schema_ids[rule.attrib["id"]] = loc_id + else: + schema_ids[rule.attrib["id"]] = None return schema_ids def _rule_ids_by_tag(self, tag, loc_id): """Helper-function for _presets(), _schemas() and _last_active_schema(). - Obtain the rule_id from the given template_tag and location_id. + Obtain the rule_id from the given template_tag and provide the location_id, when present. """ schema_ids = {} locator1 = f'.//template[@tag="{tag}"]' @@ -710,6 +734,8 @@ def _rule_ids_by_tag(self, tag, loc_id): if rule.find(locator1) is not None: if rule.find(locator2) is not None: schema_ids[rule.attrib["id"]] = loc_id + else: + schema_ids[rule.attrib["id"]] = None return schema_ids @@ -718,9 +744,8 @@ def _appliance_measurements(self, appliance, data, measurements): for measurement, attrs in measurements: p_locator = f'.//logs/point_log[type="{measurement}"]/period/measurement' if appliance.find(p_locator) is not None: - if self._smile_legacy: - if measurement == "domestic_hot_water_state": - continue + if self._smile_legacy and measurement == "domestic_hot_water_state": + continue measure = appliance.find(p_locator).text # Fix for Adam + Anna: there is a pressure-measurement with an unrealistic value, @@ -740,6 +765,10 @@ def _appliance_measurements(self, appliance, data, measurements): measure, attrs[ATTR_UNIT_OF_MEASUREMENT] ) + # Anna: use the local outdoor temperature as reference for turning cooling on/off + if measurement == "outdoor_temperature": + self._outdoor_temp = data[measurement] + i_locator = f'.//logs/interval_log[type="{measurement}"]/period/measurement' if appliance.find(i_locator) is not None: name = f"{measurement}_interval" @@ -760,7 +789,7 @@ def _get_appliance_data(self, d_id): appliance = self._appliances.find(f'.//appliance[@id="{d_id}"]') measurements = DEVICE_MEASUREMENTS.items() - if self._active_device_present: + if self._ot_device or self._on_off_device: measurements = { **DEVICE_MEASUREMENTS, **HEATER_CENTRAL_MEASUREMENTS, @@ -784,18 +813,17 @@ def _get_appliance_data(self, d_id): # Anna: indicate possible active heating/cooling operation-mode # Actual ongoing heating/cooling is shown via heating_state/cooling_state if "cooling_activation_outdoor_temperature" in data: - data["cooling_active"] = False self._cooling_present = True if ( not self.cooling_active - and data["temperature"] > data["cooling_activation_outdoor_temperature"] + and self._outdoor_temp > data["cooling_activation_outdoor_temperature"] ): - data["cooling_active"] = self.cooling_active = True + self.cooling_active = True if ( self.cooling_active - and data["temperature"] < data["cooling_deactivation_threshold"] + and self._outdoor_temp < data["cooling_deactivation_threshold"] ): - data["cooling_active"] = self.cooling_active = False + self.cooling_active = False return data @@ -1042,15 +1070,12 @@ def _preset(self, loc_id): if preset is not None: return preset.text - def _schemas_legacy(self): + def _schemas_legacy(self, avail, sched_temp, sel): """Helper-function for _schemas(). Collect available schemas/schedules for the legacy thermostat. """ - available = [] name = None - schedule_temperature = None schemas = {} - selected = None for schema in self._domain_objects.findall(".//rule"): if rule_name := schema.find("name").text: @@ -1060,73 +1085,90 @@ def _schemas_legacy(self): log_type = "schedule_state" locator = f"appliance[type='thermostat']/logs/point_log[type='{log_type}']/period/measurement" active = False - if self._domain_objects.find(locator) is not None: - active = self._domain_objects.find(locator).text == "on" + if (result := self._domain_objects.find(locator)) is not None: + active = result.text == "on" if name is not None: schemas[name] = active + avail = [name] + if active: + sel = name - available, selected = determine_selected(available, selected, schemas) - return available, selected, schedule_temperature + return avail, sel, sched_temp, None - def _schemas(self, loc_id): + def _schemas(self, location): """Helper-function for smile.py: _device_data_climate(). - Obtain the available schemas/schedules based on the Location ID. + Obtain the available schemas/schedules. Adam: a schedule can be connected to more than one location. + NEW: when a location_id is present then the schedule is active. Valid for both Adam and non-legacy Anna. """ - available = [] + available = ["None"] + last_used = None rule_ids = {} - schemas = {} schedule_temperature = None - selected = None + selected = "None" + + # Legacy Anna schedule, only one schedule allowed + if self._smile_legacy: + return self._schemas_legacy(available, schedule_temperature, selected) - # Legacy schemas - if self._smile_legacy: # Only one schedule allowed - return self._schemas_legacy() + # Adam schedules, one schedule can be linked to various locations + # self._last_active contains the locations and the active schedule name per location, or None + if location not in self._last_active: + self._last_active[location] = None - # Current schemas tag = "zone_preset_based_on_time_and_presence_with_override" - if not (rule_ids := self._rule_ids_by_tag(tag, loc_id)): - return available, selected, schedule_temperature + if not (rule_ids := self._rule_ids_by_tag(tag, location)): + return available, selected, schedule_temperature, None - for rule_id, dummy in rule_ids.items(): + schedules = {} + for rule_id, loc_id in rule_ids.items(): name = self._domain_objects.find(f'rule[@id="{rule_id}"]/name').text - active = ( - self._domain_objects.find(f'rule[@id="{rule_id}"]/active').text - == "true" - ) - schemas[name] = active - schedules = {} + schedule = {} locator = f'rule[@id="{rule_id}"]/directives' directives = self._domain_objects.find(locator) for directive in directives: - schedule = directive.find("then").attrib - keys, dummy = zip(*schedule.items()) + entry = directive.find("then").attrib + keys, dummy = zip(*entry.items()) if str(keys[0]) == "preset": - schedules[directive.attrib["time"]] = float( - self._presets(loc_id)[schedule["preset"]][0] + schedule[directive.attrib["time"]] = float( + self._presets(loc_id)[entry["preset"]][0] ) else: - schedules[directive.attrib["time"]] = float(schedule["setpoint"]) - - schedule_temperature = schemas_schedule_temp(schedules) + schedule[directive.attrib["time"]] = float(entry["setpoint"]) + + if schedule: + available.append(name) + if location == loc_id: + selected = name + self._last_active[location] = selected + schedules[name] = schedule + else: + _LOGGER.error( + "Schedule %s has no preset switching moments, ignoring.", name + ) - available, selected = determine_selected(available, selected, schemas) + if schedules: + available.remove("None") + last_used = self._last_used_schedule(location, rule_ids) + schedule_temperature = schemas_schedule_temp(schedules, last_used) - return available, selected, schedule_temperature + return available, selected, schedule_temperature, last_used - def _last_active_schema(self, loc_id): + def _last_used_schedule(self, loc_id, rule_ids): """Helper-function for smile.py: _device_data_climate(). - Determine the last active schema/schedule based on the Location ID. + Determine the last-used schedule based on the location or the modified date. """ + # First, find last_used == selected + last_used = self._last_active.get(loc_id) + if last_used is not None: + return last_used + + # Alternatively, find last_used by finding the most recent modified_date epoch = dt.datetime(1970, 1, 1, tzinfo=pytz.utc) - rule_ids = {} schemas = {} - last_modified = None - tag = "zone_preset_based_on_time_and_presence_with_override" - - if not (rule_ids := self._rule_ids_by_tag(tag, loc_id)): - return + if not rule_ids: + return # pragma: no cover for rule_id, dummy in rule_ids.items(): schema_name = self._domain_objects.find(f'rule[@id="{rule_id}"]/name').text @@ -1136,10 +1178,10 @@ def _last_active_schema(self, loc_id): schema_time = parse(schema_date) schemas[schema_name] = (schema_time - epoch).total_seconds() - if schemas != {}: - last_modified = sorted(schemas.items(), key=lambda kv: kv[1])[-1][0] + if schemas: + last_used = sorted(schemas.items(), key=lambda kv: kv[1])[-1][0] - return last_modified + return last_used def _object_value(self, obj_id, measurement): """Helper-function for smile.py: _get_device_data() and _device_data_anna(). @@ -1183,7 +1225,7 @@ def _create_dicts_from_data(self, data, bs_dict, s_dict, sw_dict): for item in BINARY_SENSORS: if list(item.keys())[0] == key: data.pop(key) - if self._active_device_present: + if self._ot_device or self._on_off_device: bs_dict[key] = value for item in SENSORS: if list(item.keys())[0] == key: diff --git a/plugwise/smile.py b/plugwise/smile.py index a0b200ae4..d94e82f52 100644 --- a/plugwise/smile.py +++ b/plugwise/smile.py @@ -7,6 +7,8 @@ import aiohttp +from defusedxml import ElementTree as etree + # Dict as class from munch import Munch @@ -19,6 +21,7 @@ DEFAULT_TIMEOUT, DEFAULT_USERNAME, DEVICE_STATE, + DIRECT_OBJECTS, DOMAIN_OBJECTS, LOCATIONS, MODULES, @@ -54,8 +57,6 @@ def _append_special(self, data, d_id, bs_dict, s_dict): if d_id == self.gateway_id: if self._sm_thermostat is not None: bs_dict.update(PW_NOTIFICATION) - if not self._active_device_present and "heating_state" in data: - s_dict.update(DEVICE_STATE) if d_id == self._heater_id and self._sm_thermostat is not None: s_dict.update(DEVICE_STATE) @@ -83,7 +84,7 @@ def _all_device_data(self): self.gw_devices[dev_id] = dev_and_data - self.gw_data["active_device"] = self._active_device_present + self.gw_data["active_device"] = self._ot_device or self._on_off_device self.gw_data["cooling_present"] = self._cooling_present self.gw_data["gateway_id"] = self.gateway_id self.gw_data["heater_id"] = self._heater_id @@ -141,20 +142,12 @@ def _device_data_adam(self, details, device_data): """ if self.smile_name == "Adam": # Indicate heating_state based on valves being open in case of city-provided heating - if details["class"] == "gateway": - if ( - not self._active_device_present - and self._heating_valves() is not None - ): + if details["class"] == "heater_central": + if self._on_off_device and self._heating_valves() is not None: device_data["heating_state"] = True if self._heating_valves() == 0: device_data["heating_state"] = False - # Adam: indicate active heating/cooling operation-mode - # Actual ongoing heating/cooling is shown via heating_state/cooling_state - if details["class"] == "heater_central": - device_data["cooling_active"] = self.cooling_active - return device_data def _device_data_climate(self, details, device_data): @@ -162,19 +155,43 @@ def _device_data_climate(self, details, device_data): Determine climate-control device data. """ loc_id = details["location"] - device_data["active_preset"] = self._preset(loc_id) - device_data["presets"] = self._presets(loc_id) - avail_schemas, sel_schema, sched_setpoint = self._schemas(loc_id) - if not self._smile_legacy: - device_data["schedule_temperature"] = sched_setpoint + # Presets + device_data["preset_modes"] = None + device_data["active_preset"] = None + presets = self._presets(loc_id) + if presets: + device_data["presets"] = presets + device_data["preset_modes"] = list(presets) + device_data["active_preset"] = self._preset(loc_id) + + # Schedule + avail_schemas, sel_schema, sched_setpoint, last_active = self._schemas(loc_id) device_data["available_schedules"] = avail_schemas device_data["selected_schedule"] = sel_schema + if not self._smile_legacy: + device_data["schedule_temperature"] = sched_setpoint if self._smile_legacy: device_data["last_used"] = "".join(map(str, avail_schemas)) else: - device_data["last_used"] = self._last_active_schema(loc_id) - + device_data["last_used"] = last_active + + # Operation mode: auto, heat, cool, off + device_data["mode"] = "auto" + schedule_status = False + if sel_schema != "None": + schedule_status = True + if not schedule_status: + # Mimic HomeKit behavior + if self._preset(loc_id) == "away": + device_data["mode"] = "off" # pragma: no cover + else: + device_data["mode"] = "heat" + if self._heater_id is not None: + if self.cooling_active: + device_data["mode"] = "cool" + + # Control_state if ctrl_state := self._control_state(loc_id): device_data["control_state"] = ctrl_state @@ -204,6 +221,11 @@ def _get_device_data(self, dev_id): if power_data is not None: device_data.update(power_data) + # Adam: indicate active heating/cooling operation-mode + # Actual ongoing heating/cooling is shown via heating_state/cooling_state + if details["class"] == "heater_central": + device_data["cooling_active"] = self.cooling_active + # Switching groups data device_data = self._device_data_switching_group(details, device_data) # Specific, not generic Adam data @@ -258,16 +280,20 @@ def __init__( websession, ) - self._active_device_present = False + self._on_off_device = False + self._ot_device = False self._appliances = None self._appl_data = None self._cooling_present = False self._domain_objects = None self._heater_id = None self._home_location = None + self._last_active = {} self._locations = None self._modules = None + self._nodomain_mc = None self._notifications = {} + self._outdoor_temp = None self._sm_thermostat = None self._smile_legacy = False self._stretch_v2 = False @@ -286,22 +312,36 @@ def __init__( async def connect(self): """Connect to Plugwise device and determine its name, type and version.""" names = [] + global DOMAIN_OBJECTS result = await self._request(DOMAIN_OBJECTS) - dsmrmain = result.find(".//module/protocols/dsmrmain") - vendor_names = result.findall(".//module/vendor_name") + if not vendor_names: + try: + result = await self._request(DIRECT_OBJECTS) + vendor_names = result.findall(".//module/vendor_name") + except InvalidXMLError: + _LOGGER.warning( + "Connected but neither DOMAIN nor DIRECT objects returned vendor names" + ) + raise ConnectionFailedError + for name in vendor_names: names.append(name.text) + dsmrmain = result.find(".//module/protocols/dsmrmain") + if "Plugwise" not in names: - if dsmrmain is None: # pragma: no cover + modules = await self._request(MODULES) + m_c = modules.find(".//module/protocols/master_controller") + if dsmrmain is None and m_c is None: # pragma: no cover _LOGGER.error( "Connected but expected text not returned, \ we got %s", result, ) raise ConnectionFailedError + self._nodomain_mc = m_c # Determine smile specifics await self._smile_detect(result, dsmrmain) @@ -313,7 +353,7 @@ async def connect(self): async def _smile_detect_legacy(self, result, dsmrmain): """Helper-function for _smile_detect().""" - network = result.find(".//module/protocols/network_router/network") + network = result.find(".//module/protocols/master_controller") # Assume legacy self._smile_legacy = True @@ -336,13 +376,18 @@ async def _smile_detect_legacy(self, result, dsmrmain): raise ConnectionFailedError # Stretch: - elif network is not None: + elif network is not None or self._nodomain_mc is not None: try: - system = await self._request(SYSTEM) + # Regular stretch + system = await self._request(STATUS) + try: + version = system.find(".//gateway/firmware").text + except AttributeError: + # Stretch without domains has no STATUS: + system = await self._request(SYSTEM) version = system.find(".//gateway/firmware").text model = system.find(".//gateway/product").text self.smile_hostname = system.find(".//gateway/hostname").text - self.gateway_id = network.attrib["id"] except InvalidXMLError: # pragma: no cover # Corner case check raise ConnectionFailedError @@ -466,9 +511,13 @@ async def async_update(self): data, self.gw_devices, dev_dict, dev_id, "switches", key ) + # Anna: update cooling_active to it's final value after all entities have been updated + if not self._smile_legacy and self.smile_name == "Anna": + self.gw_devices[self._heater_id]["cooling_active"] = self.cooling_active + return [self.gw_data, self.gw_devices] - async def _set_schedule_state_legacy(self, name, state): + async def _set_schedule_state_legacy(self, name, status): """Helper-function for set_schedule_state().""" schema_rule_id = None for rule in self._domain_objects.findall("rule"): @@ -479,7 +528,9 @@ async def _set_schedule_state_legacy(self, name, state): return False template_id = None - state = str(state) + state = "false" + if str(status) == "on": + state = "true" locator = f'.//*[@id="{schema_rule_id}"]/template' for rule in self._domain_objects.findall(locator): template_id = rule.attrib["id"] @@ -501,26 +552,30 @@ async def set_schedule_state(self, loc_id, name, state): if self._smile_legacy: return await self._set_schedule_state_legacy(name, state) - schema_rule_ids = self._rule_ids_by_name(str(name), loc_id) - if schema_rule_ids == {} or schema_rule_ids is None: + schema_rule = self._rule_ids_by_name(str(name), loc_id) + if schema_rule == {} or schema_rule is None: return False - for schema_rule_id, location_id in schema_rule_ids.items(): - template_id = None - if location_id == loc_id: - state = str(state) - locator = f'.//*[@id="{schema_rule_id}"]/template' - for rule in self._domain_objects.findall(locator): - template_id = rule.attrib["id"] - - uri = f"{RULES};id={schema_rule_id}" - data = ( - "{state}' - ) + schema_rule_id = next(iter(schema_rule)) + info = "" + if state == "on": + info = f'' - await self._request(uri, method="put", data=data) + template = ( + '