Skip to content

Commit

Permalink
Fix mood handling (#260)
Browse files Browse the repository at this point in the history
* fix mood handling (moods are always associated with groups)

* fix getting active mood for group

* make available moods part of a group

* remove block comment

* add tests for moods

* fix missing whitespace and blank lines

* another missing blank line in test_group

* fix another missing blank line and whitespace

* re-add moods to main

Co-authored-by: rgci <32896521+rgci@users.noreply.github.com>
  • Loading branch information
2 people authored and ggravlingen committed Jan 4, 2020
1 parent 7425056 commit c0763e8
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 31 deletions.
14 changes: 7 additions & 7 deletions pytradfri/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@
else:
print("No lights found!")
light = None
groups = api(gateway.get_groups())
groups_commands = api(gateway.get_groups())
groups = api(groups_commands)
moods = []
if groups:
group = groups[0]
for group in groups:
moods_commands = api(group.moods())
group_moods = api(moods_commands)
moods.extend(group_moods)
else:
print("No groups found!")
group = None
moods = api(gateway.get_moods())
if moods:
mood = moods[0]
else:
print("No moods found!")
mood = None
tasks = api(gateway.get_smart_tasks())
homekit_id = api(gateway.get_gateway_info()).homekit_id

Expand Down
22 changes: 4 additions & 18 deletions pytradfri/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,18 @@ def process_result(result):
[ROOT_GATEWAY, ATTR_GATEWAY_INFO],
process_result=process_result)

def get_moods(self):
def get_moods(self, group_id):
"""
Return moods defined on the gateway.
Return moods available in given group.
Returns a Command.
"""
mood_parent = self._get_mood_parent()

def process_result(result):
return [self.get_mood(mood, mood_parent=mood_parent) for mood in
return [self.get_mood(mood, mood_parent=group_id) for mood in
result]

return Command('get', [ROOT_MOODS, mood_parent],
return Command('get', [ROOT_MOODS, group_id],
process_result=process_result)

def get_mood(self, mood_id, *, mood_parent=None):
Expand All @@ -124,26 +123,13 @@ def get_mood(self, mood_id, *, mood_parent=None):
Returns a Command.
"""
if mood_parent is None:
mood_parent = self._get_mood_parent()

def process_result(result):
return Mood(result, mood_parent)

return Command('get', [ROOT_MOODS, mood_parent, mood_id],
mood_parent, process_result=process_result)

def _get_mood_parent(self):
"""
Get the parent of all moods.
Returns a Command.
"""
def process_result(result):
return result[0]

return Command('get', [ROOT_MOODS], process_result=process_result)

def get_smart_tasks(self):
"""
Return the transitions linked to the gateway.
Expand Down
9 changes: 7 additions & 2 deletions pytradfri/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,19 @@ def members(self):
"""Return device objects of members of this group."""
return [self._gateway.get_device(dev) for dev in self.member_ids]

def moods(self):
"""Return mood objects of moods in this group."""
return self._gateway.get_moods(self.id)

def mood(self):
""""Active mood."""
return self._gateway.get_mood(self.mood_id)
return self._gateway.get_mood(self.mood_id, mood_parent=self.id)

def activate_mood(self, mood_id):
"""Activate a mood."""
return self.set_values({
ATTR_MOOD: mood_id
ATTR_MOOD: mood_id,
ATTR_DEVICE_STATE: int(self.state)
})

def set_state(self, state):
Expand Down
2 changes: 1 addition & 1 deletion pytradfri/mood.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ def path(self):
return [ROOT_MOODS, self._parent, self.id]

def __repr__(self):
return '<Mood {}>'.format(self.name)
return '<Mood {} {}>'.format(self._parent, self.name)
9 changes: 9 additions & 0 deletions tests/moods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Retrieved from Gateway running on 1.9.27
MOOD = {
'9001': 'FOCUS',
'9002': 1577189497,
'9003': 196625,
'9057': 2,
'9068': 1,
'15013': [{'5850': 1, '5851': 254, '9003': 65547}]
}
18 changes: 15 additions & 3 deletions tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
ATTR_LIGHT_MIREDS,
ATTR_LIGHT_COLOR_HUE,
ATTR_LIGHT_COLOR_SATURATION,
ATTR_LIGHT_DIMMER
ATTR_LIGHT_DIMMER,
ROOT_MOODS
)
from pytradfri.group import Group
from pytradfri.gateway import Gateway


@pytest.fixture
def group():
return Group(GROUP)
def gateway():
return Gateway()


@pytest.fixture
def group(gateway):
return Group(gateway, GROUP)


def test_setters():
Expand Down Expand Up @@ -48,3 +55,8 @@ def test_setters():
ATTR_LIGHT_COLOR_SATURATION: 200,
ATTR_LIGHT_DIMMER: 100,
}


def test_moods(group):
cmd = group.moods()
assert cmd.path == [ROOT_MOODS, group.id]
14 changes: 14 additions & 0 deletions tests/test_mood.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest
from moods import MOOD

from pytradfri.const import ROOT_MOODS
from pytradfri.mood import Mood


@pytest.fixture
def mood():
return Mood(MOOD, 131080)


def test_mood_properties(mood):
assert mood.path == [ROOT_MOODS, 131080, 196625]

0 comments on commit c0763e8

Please sign in to comment.