Advertisement
Guest User

Untitled

a guest
Mar 29th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. """
  2. Ryobi platform for the cover component.
  3. For more details about this platform, please refer to the documentation
  4. https://home-assistant.io/components/cover.ryobi_gdo/
  5. """
  6. import logging
  7. import voluptuous as vol
  8.  
  9. import homeassistant.helpers.config_validation as cv
  10.  
  11. from homeassistant.components.cover import (
  12. CoverDevice, PLATFORM_SCHEMA, SUPPORT_OPEN, SUPPORT_CLOSE)
  13. from homeassistant.const import (
  14. CONF_USERNAME, CONF_PASSWORD, STATE_UNKNOWN, STATE_CLOSED)
  15.  
  16. REQUIREMENTS = ['py_ryobi_gdo==0.0.27']
  17.  
  18. _LOGGER = logging.getLogger(__name__)
  19.  
  20. CONF_DEVICE_ID = 'device_id'
  21.  
  22. PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
  23. vol.Required(CONF_DEVICE_ID): vol.All(cv.ensure_list, [cv.string]),
  24. vol.Required(CONF_PASSWORD): cv.string,
  25. vol.Required(CONF_USERNAME): cv.string,
  26. })
  27.  
  28. SUPPORTED_FEATURES = (SUPPORT_OPEN | SUPPORT_CLOSE)
  29.  
  30.  
  31. def setup_platform(hass, config, add_entities, discovery_info=None):
  32. """Set up the Ryobi covers."""
  33. from py_ryobi_gdo import RyobiGDO as ryobi_door
  34. covers = []
  35.  
  36. username = config.get(CONF_USERNAME)
  37. password = config.get(CONF_PASSWORD)
  38. devices = config.get(CONF_DEVICE_ID)
  39.  
  40. for device_id in devices:
  41. my_door = ryobi_door(username, password, device_id)
  42. _LOGGER.debug("Getting the API key")
  43. if my_door.get_api_key() is False:
  44. _LOGGER.error("Wrong credentials, no API key retrieved")
  45. return
  46. _LOGGER.debug("Checking if the device ID is present")
  47. if my_door.check_device_id() is False:
  48. _LOGGER.error("%s not in your device list", device_id)
  49. return
  50. _LOGGER.debug("Adding device %s to covers", device_id)
  51. covers.append(RyobiCover(hass, my_door))
  52. if covers:
  53. _LOGGER.debug("Adding covers")
  54. add_entities(covers, True)
  55.  
  56.  
  57. class RyobiCover(CoverDevice):
  58. """Representation of a ryobi cover."""
  59.  
  60. def __init__(self, hass, ryobi_door):
  61. """Initialize the cover."""
  62. self.ryobi_door = ryobi_door
  63. self._name = 'ryobi_gdo_{}'.format(ryobi_door.get_device_id())
  64. self._door_state = None
  65.  
  66. @property
  67. def name(self):
  68. """Return the name of the cover."""
  69. return self._name
  70.  
  71. @property
  72. def is_closed(self):
  73. """Return if the cover is closed."""
  74. if self._door_state == STATE_UNKNOWN:
  75. return False
  76. return self._door_state == STATE_CLOSED
  77.  
  78. @property
  79. def device_class(self):
  80. """Return the class of this device, from component DEVICE_CLASSES."""
  81. return 'garage'
  82.  
  83. @property
  84. def supported_features(self):
  85. """Flag supported features."""
  86. return SUPPORTED_FEATURES
  87.  
  88. def close_cover(self, **kwargs):
  89. """Close the cover."""
  90. _LOGGER.debug("Closing garage door")
  91. self.ryobi_door.close_device()
  92.  
  93. def open_cover(self, **kwargs):
  94. """Open the cover."""
  95. _LOGGER.debug("Opening garage door")
  96. self.ryobi_door.open_device()
  97.  
  98. def update(self):
  99. """Update status from the door."""
  100. _LOGGER.debug("Updating RyobiGDO status")
  101. self.ryobi_door.update()
  102. self._door_state = self.ryobi_door.get_door_status()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement