1 """Support to interact with Remember The Milk."""
8 import voluptuous
as vol
17 from .entity
import RememberTheMilkEntity
21 _LOGGER = logging.getLogger(__name__)
23 DOMAIN =
"remember_the_milk"
26 CONF_SHARED_SECRET =
"shared_secret"
27 CONF_ID_MAP =
"id_map"
28 CONF_LIST_ID =
"list_id"
29 CONF_TIMESERIES_ID =
"timeseries_id"
30 CONF_TASK_ID =
"task_id"
32 RTM_SCHEMA = vol.Schema(
34 vol.Required(CONF_NAME): cv.string,
35 vol.Required(CONF_API_KEY): cv.string,
36 vol.Required(CONF_SHARED_SECRET): cv.string,
40 CONFIG_SCHEMA = vol.Schema(
41 {DOMAIN: vol.All(cv.ensure_list, [RTM_SCHEMA])}, extra=vol.ALLOW_EXTRA
44 CONFIG_FILE_NAME =
".remember_the_milk.conf"
45 SERVICE_CREATE_TASK =
"create_task"
46 SERVICE_COMPLETE_TASK =
"complete_task"
48 SERVICE_SCHEMA_CREATE_TASK = vol.Schema(
49 {vol.Required(CONF_NAME): cv.string, vol.Optional(CONF_ID): cv.string}
52 SERVICE_SCHEMA_COMPLETE_TASK = vol.Schema({vol.Required(CONF_ID): cv.string})
55 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
56 """Set up the Remember the milk component."""
57 component = EntityComponent[RememberTheMilkEntity](_LOGGER, DOMAIN, hass)
60 for rtm_config
in config[DOMAIN]:
61 account_name = rtm_config[CONF_NAME]
62 _LOGGER.debug(
"Adding Remember the milk account %s", account_name)
63 api_key = rtm_config[CONF_API_KEY]
64 shared_secret = rtm_config[CONF_SHARED_SECRET]
65 token = stored_rtm_config.get_token(account_name)
67 _LOGGER.debug(
"found token for account %s", account_name)
79 hass, account_name, api_key, shared_secret, stored_rtm_config, component
82 _LOGGER.debug(
"Finished adding all Remember the milk accounts")
87 hass, account_name, api_key, shared_secret, token, stored_rtm_config, component
90 account_name, api_key, shared_secret, token, stored_rtm_config
92 component.add_entities([entity])
93 hass.services.register(
95 f
"{account_name}_create_task",
97 schema=SERVICE_SCHEMA_CREATE_TASK,
99 hass.services.register(
101 f
"{account_name}_complete_task",
102 entity.complete_task,
103 schema=SERVICE_SCHEMA_COMPLETE_TASK,
108 hass, account_name, api_key, shared_secret, stored_rtm_config, component
111 api = Rtm(api_key, shared_secret,
"write",
None)
112 url, frob = api.authenticate_desktop()
113 _LOGGER.debug(
"Sent authentication request to server")
115 def register_account_callback(fields: list[dict[str, str]]) ->
None:
116 """Call for register the configurator."""
117 api.retrieve_token(frob)
119 if api.token
is None:
120 _LOGGER.error(
"Failed to register, please try again")
121 configurator.notify_errors(
122 hass, request_id,
"Failed to register, please try again."
126 stored_rtm_config.set_token(account_name, token)
127 _LOGGER.debug(
"Retrieved new token from server")
139 configurator.request_done(hass, request_id)
141 request_id = configurator.request_config(
143 f
"{DOMAIN} - {account_name}",
144 callback=register_account_callback,
146 "You need to log in to Remember The Milk to"
147 "connect your account. \n\n"
148 "Step 1: Click on the link 'Remember The Milk login'\n\n"
149 "Step 2: Click on 'login completed'"
151 link_name=
"Remember The Milk login",
153 submit_caption=
"login completed",
158 """Internal configuration data for RememberTheMilk class.
160 This class stores the authentication token it get from the backend.
164 """Create new instance of configuration."""
170 _LOGGER.debug(
"Loading configuration from file: %s", self.
_config_file_path_config_file_path)
172 self.
_config_config = json.load(config_file)
175 "Failed to load configuration file, creating a new one: %s",
181 """Write the configuration to a file."""
183 json.dump(self.
_config_config, config_file)
186 """Get the server token for a profile."""
187 if profile_name
in self.
_config_config:
188 return self.
_config_config[profile_name][CONF_TOKEN]
192 """Store a new server token for a profile."""
194 self.
_config_config[profile_name][CONF_TOKEN] = token
198 """Delete a token for a profile.
200 Usually called when the token has expired.
202 self.
_config_config.pop(profile_name,
None)
206 """Initialize the data structures for a profile."""
207 if profile_name
not in self.
_config_config:
208 self.
_config_config[profile_name] = {}
209 if CONF_ID_MAP
not in self.
_config_config[profile_name]:
210 self.
_config_config[profile_name][CONF_ID_MAP] = {}
213 """Get the RTM ids for a Home Assistant task ID.
215 The id of a RTM tasks consists of the tuple:
216 list id, timeseries id and the task id.
219 ids = self.
_config_config[profile_name][CONF_ID_MAP].
get(hass_id)
222 return ids[CONF_LIST_ID], ids[CONF_TIMESERIES_ID], ids[CONF_TASK_ID]
224 def set_rtm_id(self, profile_name, hass_id, list_id, time_series_id, rtm_task_id):
225 """Add/Update the RTM task ID for a Home Assistant task IS."""
228 CONF_LIST_ID: list_id,
229 CONF_TIMESERIES_ID: time_series_id,
230 CONF_TASK_ID: rtm_task_id,
232 self.
_config_config[profile_name][CONF_ID_MAP][hass_id] = id_tuple
236 """Delete a key mapping."""
238 if hass_id
in self.
_config_config[profile_name][CONF_ID_MAP]:
239 del self.
_config_config[profile_name][CONF_ID_MAP][hass_id]
def set_token(self, profile_name, token)
def set_rtm_id(self, profile_name, hass_id, list_id, time_series_id, rtm_task_id)
def delete_token(self, profile_name)
def _initialize_profile(self, profile_name)
def get_token(self, profile_name)
def delete_rtm_id(self, profile_name, hass_id)
def get_rtm_id(self, profile_name, hass_id)
web.Response get(self, web.Request request, str config_key)
None open(self, **Any kwargs)
bool setup(HomeAssistant hass, ConfigType config)
def _create_instance(hass, account_name, api_key, shared_secret, token, stored_rtm_config, component)
def _register_new_account(hass, account_name, api_key, shared_secret, stored_rtm_config, component)