Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pykumo/py_kumo_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,19 @@ def _request(self, post_data):

return {}

def has_profile(self) -> bool:
"""Return True if the unit profile has been populated from a successful poll.

The profile starts as an empty dict at construction and is populated after
the first successful ``update_status()`` call. Consumers (e.g. hass-kumo)
should call this before relying on capability methods such as
``has_auto_mode()``, ``has_heat_mode()``, or ``get_fan_speeds()``:
those methods silently return defaults (``False`` / a fallback list) when
the profile is empty, which can cause incorrect behaviour if cached at
initialisation time while the adapter is temporarily offline.
"""
return bool(self._profile)

def get_status(self):
"""Last retrieved status dictionary from unit"""
return self._status
Expand Down
42 changes: 42 additions & 0 deletions tests/test_py_kumo_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Tests for PyKumoBase.has_profile()."""

import unittest

from pykumo.py_kumo_base import PyKumoBase


# Minimal cfg_json accepted by PyKumoBase.__init__
_CFG = {
"password": "dGVzdA==", # base64("test")
"crypto_serial": "0123456789ABCDEF01234567",
}


class TestHasProfile(unittest.TestCase):
"""PyKumoBase.has_profile() behaviour."""

def _make_unit(self):
return PyKumoBase("Test Unit", "192.168.1.1", _CFG)

def test_false_before_any_poll(self):
"""has_profile() is False immediately after construction."""
unit = self._make_unit()
self.assertFalse(unit.has_profile())

def test_true_after_profile_populated(self):
"""has_profile() is True once _profile contains real data."""
unit = self._make_unit()
unit._profile = {"hasModeAuto": True, "numberOfFanSpeeds": 5}
self.assertTrue(unit.has_profile())

def test_false_when_profile_reset_to_empty(self):
"""has_profile() returns False again if profile is cleared."""
unit = self._make_unit()
unit._profile = {"hasModeAuto": True}
self.assertTrue(unit.has_profile())
unit._profile = {}
self.assertFalse(unit.has_profile())


if __name__ == "__main__":
unittest.main()