Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Support for Free Mobile SMS platform."""
2 
3 from __future__ import annotations
4 
5 from http import HTTPStatus
6 import logging
7 
8 from freesms import FreeClient
9 import voluptuous as vol
10 
12  PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA,
13  BaseNotificationService,
14 )
15 from homeassistant.const import CONF_ACCESS_TOKEN, CONF_USERNAME
16 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
23  {vol.Required(CONF_USERNAME): cv.string, vol.Required(CONF_ACCESS_TOKEN): cv.string}
24 )
25 
26 
28  hass: HomeAssistant,
29  config: ConfigType,
30  discovery_info: DiscoveryInfoType | None = None,
31 ) -> FreeSMSNotificationService:
32  """Get the Free Mobile SMS notification service."""
33  return FreeSMSNotificationService(config[CONF_USERNAME], config[CONF_ACCESS_TOKEN])
34 
35 
36 class FreeSMSNotificationService(BaseNotificationService):
37  """Implement a notification service for the Free Mobile SMS service."""
38 
39  def __init__(self, username, access_token):
40  """Initialize the service."""
41  self.free_clientfree_client = FreeClient(username, access_token)
42 
43  def send_message(self, message="", **kwargs):
44  """Send a message to the Free Mobile user cell."""
45  resp = self.free_clientfree_client.send_sms(message)
46 
47  if resp.status_code == HTTPStatus.BAD_REQUEST:
48  _LOGGER.error("At least one parameter is missing")
49  elif resp.status_code == HTTPStatus.PAYMENT_REQUIRED:
50  _LOGGER.error("Too much SMS send in a few time")
51  elif resp.status_code == HTTPStatus.FORBIDDEN:
52  _LOGGER.error("Wrong Username/Password")
53  elif resp.status_code == HTTPStatus.INTERNAL_SERVER_ERROR:
54  _LOGGER.error("Server error, try later")
FreeSMSNotificationService get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:31