8000 Improve bulb API, force turn on for all light changes as offline changes are not supported by rytilahti · Pull Request #76 · python-kasa/python-kasa · GitHub
[go: up one dir, main page]

Skip to content

Improve bulb API, force turn on for all light changes as offline changes are not supported #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 6, 2020
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
33 changes: 32 additions & 1 deletion kasa/smartbulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,25 @@ def light_state(self) -> Dict[str, str]:

return light_state

async def get_light_details(self) -> Dict[str, int]:
"""Return light details.

Example:
{'lamp_beam_angle': 290, 'min_voltage': 220, 'max_voltage': 240,
'wattage': 5, 'incandescent_equivalent': 40, 'max_lumens': 450,
'color_rendering_index': 80}
"""
return await self._query_helper(self.LIGHT_SERVICE, "get_light_details")

async def get_turn_on_behavior(self) -> Dict:
"""Return the behavior for turning the bulb on.

Example:
{'soft_on': {'mode': 'last_status'},
'hard_on': {'mode': 'last_status'}}
"""
return await self._query_helper(self.LIGHT_SERVICE, "get_default_behavior")

async def get_light_state(self) -> Dict[str, Dict]:
"""Query the light state."""
# TODO: add warning and refer to use light.state?
Expand All @@ -163,6 +182,13 @@ async def set_light_state(self, state: Dict, *, transition: int = None) -> Dict:
if transition is not None:
state["transition_period"] = transition

# if no on/off is defined, turn on the light
if "on_off" not in state:
state["on_off"] = 1

# This is necessary to allow turning on into a specific state
state["ignore_default"] = 1

light_state = await self._query_helper(
self.LIGHT_SERVICE, "transition_light_state", state
)
Expand Down Expand Up @@ -239,7 +265,9 @@ def color_temp(self) -> int:
return int(light_state["color_temp"])

@requires_update
async def set_color_temp(self, temp: int, *, transition: int = None) -> Dict:
async def set_color_temp(
self, temp: int, *, brightness=None, transition: int = None
) -> Dict:
"""Set the color temperature of the device in kelvin.

:param int temp: The new color temperature, in Kelvin
Expand All @@ -256,6 +284,9 @@ async def set_color_temp(self, temp: int, *, transition: int = None) -> Dict:
)

light_state = {"color_temp": temp}
if brightness is not None:
light_state["brightness"] = brightness

return await self.set_light_state(light_state, transition=transition)

@property # type: ignore
Expand Down
40 changes: 21 additions & 19 deletions kasa/tests/newfakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,31 +323,33 @@ def set_hs220_dimmer_transition(self, x, *args):
self.proto["system"]["get_sysinfo"]["relay_state"] = 1
self.proto["system"]["get_sysinfo"]["brightness"] = x["brightness"]

def transition_light_state(self, x, *args):
_LOGGER.debug("Setting light state to %s", x)
def transition_light_state(self, state_changes, *args):
_LOGGER.debug("Setting light state to %s", state_changes)
light_state = self.proto["system"]["get_sysinfo"]["light_state"]
# The required change depends on the light state,
# exception being turning the bulb on and off

if "on_off" in x:
if x["on_off"] and not light_state["on_off"]: # turning on
new_state = light_state["dft_on_state"]
new_state["on_off"] = 1
self.proto["system"]["get_sysinfo"]["light_state"] = new_state
elif not x["on_off"] and light_state["on_off"]:
new_state = {"dft_on_state": light_state, "on_off": 0}
_LOGGER.debug("Current light state: %s", light_state)
new_state = light_state

self.proto["system"]["get_sysinfo"]["light_state"] = new_state
if state_changes["on_off"] == 1: # turn on requested
if not light_state[
"on_off"
]: # if we were off, use the dft_on_state as a base
_LOGGER.debug("Bulb was off, using dft_on_state")
new_state = light_state["dft_on_state"]

return
# override the existing settings
new_state.update(state_changes)

if not light_state["on_off"] and "on_off" not in x:
light_state = light_state["dft_on_state"]
if (
not state_changes["on_off"] and "dft_on_state" not in light_state
): # if not already off, pack the data inside dft_on_state
_LOGGER.debug(
"Bulb was on and turn_off was requested, saving to dft_on_state"
)
new_state = {"dft_on_state": light_state, "on_off": 0}

_LOGGER.debug("Old state: %s", light_state)
for key in x:
light_state[key] = x[key]
_LOGGER.debug("New state: %s", light_state)
_LOGGER.debug("New light state: %s", new_state)
self.proto["system"]["get_sysinfo"]["light_state"] = new_state

def light_state(self, x, *args):
light_state = self.proto["system"]["get_sysinfo"]["light_state"]
Expand Down
0