Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """Support to interface with Sonos players."""
- import functools as ft
- import logging
- import socket
- import asyncio
- import voluptuous as vol
- from homeassistant.components.foo import DOMAIN as FOO_DOMAIN
- from homeassistant.helpers.entity import Switch
- from homeassistant.const import (
- ATTR_ENTITY_ID, CONF_HOSTS, STATE_ON, STATE_OFF)
- import homeassistant.helpers.config_validation as cv
- DEPENDENCIES = ('foo',)
- _LOGGER = logging.getLogger(__name__)
- PARALLEL_UPDATES = 0
- DATA_FOO = 'foo_switch'
- class FooData:
- """Storage class for platform global data."""
- def __init__(self, hass):
- """Initialize the data."""
- self.uids = set()
- self.entities = []
- self.topology_condition = asyncio.Condition(loop=hass.loop)
- def setup_platform(hass, config, add_entities, discovery_info=None):
- """Set up the Foo platform.
- Deprecated.
- """
- _LOGGER.warning('Loading Foo via platform config is deprecated.')
- _setup_platform(hass, config, add_entities, discovery_info)
- async def async_setup_entry(hass, config_entry, async_add_entities):
- """Set up Foo from a config entry."""
- def add_entities(entities, update_before_add=False):
- """Sync version of async add entities."""
- hass.add_job(async_add_entities, entities, update_before_add)
- hass.async_add_executor_job(
- _setup_platform, hass, hass.data[FOO_DOMAIN].get('switch', {}),
- add_entities, None)
- def _setup_platform(hass, config, add_entities, discovery_info):
- """Set up the Foo platform."""
- if DATA_FOO not in hass.data:
- hass.data[DATA_FOO] = FooData(hass)
- devices = []
- if discovery_info:
- uid = discovery_info.get('uid')
- # If host already exists by config
- if uid in hass.data[DATA_FOO].uids:
- return
- devices.append(discovery_info)
- else:
- devices = [{'name': 'super_switch2', 'host': '1.1.0.0', 'uid': '1729'}]
- if not devices:
- _LOGGER.warning("No Foo switch found")
- return
- hass.data[DATA_FOO].uids.update(d.uid for d in devices)
- add_entities(FooEntity(d) for d in devices)
- _LOGGER.debug("Added %s Foo switch", len(devices))
- def _service_to_entities(service):
- """Extract and return entities from service call."""
- entity_ids = service.data.get('entity_id')
- entities = hass.data[DATA_FOO].entities
- if entity_ids:
- entities = [e for e in entities if e.entity_id in entity_ids]
- return entities
- class FooEntity(Switch):
- """Representation of a Foo entity."""
- def __init__(self, device):
- """Initialize the Foo entity."""
- self._unique_id = device.uid
- self._device = device
- self._name = device.name
- self._state = STATE_OFF
- async def async_added_to_hass(self):
- """Subscribe Foo events."""
- self.hass.data[DATA_FOO].entities.append(self)
- @property
- def unique_id(self):
- """Return a unique ID."""
- return self._unique_id
- def __hash__(self):
- """Return a hash of self."""
- return hash(self.unique_id)
- @property
- def name(self):
- """Return the name of the entity."""
- return self._name
- @property
- def device_info(self):
- """Return information about the device."""
- return {
- 'identifiers': {
- (FOO_DOMAIN, self._unique_id)
- },
- 'name': self._name,
- 'model': "ULTRON !!!",
- 'manufacturer': 'Foo',
- }
- @property
- def state(self):
- """Return the state of the entity."""
- return self._state
- @property
- def available(self) -> bool:
- """Return True if entity is available."""
- return True
- def update(self):
- """Retrieve latest state."""
- def turn_on(self):
- """Turn the media player on."""
- self._state = STATE_ON
- def turn_off(self):
- """Turn off media player."""
- self._state = STATE_OFF
Add Comment
Please, Sign In to add comment