grea09

foo/switch.py

Mar 18th, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.11 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. """Support to interface with Sonos players."""
  4. import functools as ft
  5. import logging
  6. import socket
  7. import asyncio
  8.  
  9. import voluptuous as vol
  10.  
  11. from homeassistant.components.foo import DOMAIN as FOO_DOMAIN
  12. from homeassistant.helpers.entity import Switch
  13. from homeassistant.const import (
  14.     ATTR_ENTITY_ID, CONF_HOSTS, STATE_ON, STATE_OFF)
  15. import homeassistant.helpers.config_validation as cv
  16.  
  17. DEPENDENCIES = ('foo',)
  18.  
  19. _LOGGER = logging.getLogger(__name__)
  20.  
  21. PARALLEL_UPDATES = 0
  22.  
  23. DATA_FOO = 'foo_switch'
  24.  
  25. class FooData:
  26.     """Storage class for platform global data."""
  27.  
  28.     def __init__(self, hass):
  29.         """Initialize the data."""
  30.         self.uids = set()
  31.         self.entities = []
  32.         self.topology_condition = asyncio.Condition(loop=hass.loop)
  33.  
  34.  
  35. def setup_platform(hass, config, add_entities, discovery_info=None):
  36.     """Set up the Foo platform.
  37.  
  38.    Deprecated.
  39.    """
  40.     _LOGGER.warning('Loading Foo via platform config is deprecated.')
  41.     _setup_platform(hass, config, add_entities, discovery_info)
  42.  
  43.  
  44. async def async_setup_entry(hass, config_entry, async_add_entities):
  45.     """Set up Foo from a config entry."""
  46.     def add_entities(entities, update_before_add=False):
  47.         """Sync version of async add entities."""
  48.         hass.add_job(async_add_entities, entities, update_before_add)
  49.  
  50.     hass.async_add_executor_job(
  51.         _setup_platform, hass, hass.data[FOO_DOMAIN].get('switch', {}),
  52.         add_entities, None)
  53.  
  54.  
  55. def _setup_platform(hass, config, add_entities, discovery_info):
  56.     """Set up the Foo platform."""
  57.     if DATA_FOO not in hass.data:
  58.         hass.data[DATA_FOO] = FooData(hass)
  59.  
  60.     devices = []
  61.     if discovery_info:
  62.         uid = discovery_info.get('uid')
  63.  
  64.         # If host already exists by config
  65.         if uid in hass.data[DATA_FOO].uids:
  66.             return
  67.  
  68.         devices.append(discovery_info)
  69.     else:
  70.         devices = [{'name': 'super_switch2', 'host': '1.1.0.0', 'uid': '1729'}]
  71.  
  72.         if not devices:
  73.             _LOGGER.warning("No Foo switch found")
  74.             return
  75.  
  76.     hass.data[DATA_FOO].uids.update(d.uid for d in devices)
  77.     add_entities(FooEntity(d) for d in devices)
  78.     _LOGGER.debug("Added %s Foo switch", len(devices))
  79.  
  80.     def _service_to_entities(service):
  81.         """Extract and return entities from service call."""
  82.         entity_ids = service.data.get('entity_id')
  83.  
  84.         entities = hass.data[DATA_FOO].entities
  85.         if entity_ids:
  86.             entities = [e for e in entities if e.entity_id in entity_ids]
  87.         return entities
  88.  
  89.  
  90. class FooEntity(Switch):
  91.     """Representation of a Foo entity."""
  92.  
  93.     def __init__(self, device):
  94.         """Initialize the Foo entity."""
  95.         self._unique_id = device.uid
  96.         self._device = device
  97.         self._name = device.name
  98.         self._state = STATE_OFF
  99.  
  100.     async def async_added_to_hass(self):
  101.         """Subscribe Foo events."""
  102.         self.hass.data[DATA_FOO].entities.append(self)
  103.  
  104.     @property
  105.     def unique_id(self):
  106.         """Return a unique ID."""
  107.         return self._unique_id
  108.  
  109.     def __hash__(self):
  110.         """Return a hash of self."""
  111.         return hash(self.unique_id)
  112.  
  113.     @property
  114.     def name(self):
  115.         """Return the name of the entity."""
  116.         return self._name
  117.  
  118.     @property
  119.     def device_info(self):
  120.         """Return information about the device."""
  121.         return {
  122.             'identifiers': {
  123.                 (FOO_DOMAIN, self._unique_id)
  124.             },
  125.             'name': self._name,
  126.             'model': "ULTRON !!!",
  127.             'manufacturer': 'Foo',
  128.         }
  129.  
  130.     @property
  131.     def state(self):
  132.         """Return the state of the entity."""
  133.         return self._state
  134.  
  135.     @property
  136.     def available(self) -> bool:
  137.         """Return True if entity is available."""
  138.         return True
  139.  
  140.  
  141.     def update(self):
  142.         """Retrieve latest state."""
  143.  
  144.  
  145.  
  146.     def turn_on(self):
  147.         """Turn the media player on."""
  148.         self._state = STATE_ON
  149.  
  150.  
  151.     def turn_off(self):
  152.         """Turn off media player."""
  153.         self._state = STATE_OFF
Add Comment
Please, Sign In to add comment