Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support to interact with Remember The Milk."""
2 
3 import logging
4 
5 from rtmapi import Rtm, RtmRequestFailedException
6 
7 from homeassistant.const import CONF_ID, CONF_NAME, STATE_OK
8 from homeassistant.core import ServiceCall
9 from homeassistant.helpers.entity import Entity
10 
11 _LOGGER = logging.getLogger(__name__)
12 
13 
15  """Representation of an interface to Remember The Milk."""
16 
17  def __init__(self, name, api_key, shared_secret, token, rtm_config):
18  """Create new instance of Remember The Milk component."""
19  self._name_name = name
20  self._api_key_api_key = api_key
21  self._shared_secret_shared_secret = shared_secret
22  self._token_token = token
23  self._rtm_config_rtm_config = rtm_config
24  self._rtm_api_rtm_api = Rtm(api_key, shared_secret, "delete", token)
25  self._token_valid_token_valid = None
26  self._check_token_check_token()
27  _LOGGER.debug("Instance created for account %s", self._name_name)
28 
29  def _check_token(self):
30  """Check if the API token is still valid.
31 
32  If it is not valid any more, delete it from the configuration. This
33  will trigger a new authentication process.
34  """
35  valid = self._rtm_api_rtm_api.token_valid()
36  if not valid:
37  _LOGGER.error(
38  "Token for account %s is invalid. You need to register again!",
39  self.namenamename,
40  )
41  self._rtm_config_rtm_config.delete_token(self._name_name)
42  self._token_valid_token_valid = False
43  else:
44  self._token_valid_token_valid = True
45  return self._token_valid_token_valid
46 
47  def create_task(self, call: ServiceCall) -> None:
48  """Create a new task on Remember The Milk.
49 
50  You can use the smart syntax to define the attributes of a new task,
51  e.g. "my task #some_tag ^today" will add tag "some_tag" and set the
52  due date to today.
53  """
54  try:
55  task_name = call.data[CONF_NAME]
56  hass_id = call.data.get(CONF_ID)
57  rtm_id = None
58  if hass_id is not None:
59  rtm_id = self._rtm_config_rtm_config.get_rtm_id(self._name_name, hass_id)
60  result = self._rtm_api_rtm_api.rtm.timelines.create()
61  timeline = result.timeline.value
62 
63  if hass_id is None or rtm_id is None:
64  result = self._rtm_api_rtm_api.rtm.tasks.add(
65  timeline=timeline, name=task_name, parse="1"
66  )
67  _LOGGER.debug(
68  "Created new task '%s' in account %s", task_name, self.namenamename
69  )
70  self._rtm_config_rtm_config.set_rtm_id(
71  self._name_name,
72  hass_id,
73  result.list.id,
74  result.list.taskseries.id,
75  result.list.taskseries.task.id,
76  )
77  else:
78  self._rtm_api_rtm_api.rtm.tasks.setName(
79  name=task_name,
80  list_id=rtm_id[0],
81  taskseries_id=rtm_id[1],
82  task_id=rtm_id[2],
83  timeline=timeline,
84  )
85  _LOGGER.debug(
86  "Updated task with id '%s' in account %s to name %s",
87  hass_id,
88  self.namenamename,
89  task_name,
90  )
91  except RtmRequestFailedException as rtm_exception:
92  _LOGGER.error(
93  "Error creating new Remember The Milk task for account %s: %s",
94  self._name_name,
95  rtm_exception,
96  )
97 
98  def complete_task(self, call: ServiceCall) -> None:
99  """Complete a task that was previously created by this component."""
100  hass_id = call.data[CONF_ID]
101  rtm_id = self._rtm_config_rtm_config.get_rtm_id(self._name_name, hass_id)
102  if rtm_id is None:
103  _LOGGER.error(
104  (
105  "Could not find task with ID %s in account %s. "
106  "So task could not be closed"
107  ),
108  hass_id,
109  self._name_name,
110  )
111  return
112  try:
113  result = self._rtm_api_rtm_api.rtm.timelines.create()
114  timeline = result.timeline.value
115  self._rtm_api_rtm_api.rtm.tasks.complete(
116  list_id=rtm_id[0],
117  taskseries_id=rtm_id[1],
118  task_id=rtm_id[2],
119  timeline=timeline,
120  )
121  self._rtm_config_rtm_config.delete_rtm_id(self._name_name, hass_id)
122  _LOGGER.debug(
123  "Completed task with id %s in account %s", hass_id, self._name_name
124  )
125  except RtmRequestFailedException as rtm_exception:
126  _LOGGER.error(
127  "Error creating new Remember The Milk task for account %s: %s",
128  self._name_name,
129  rtm_exception,
130  )
131 
132  @property
133  def name(self):
134  """Return the name of the device."""
135  return self._name_name
136 
137  @property
138  def state(self):
139  """Return the state of the device."""
140  if not self._token_valid_token_valid:
141  return "API token invalid"
142  return STATE_OK
def __init__(self, name, api_key, shared_secret, token, rtm_config)
Definition: entity.py:17
str|UndefinedType|None name(self)
Definition: entity.py:738