Fix non-syncthru supporting printers (#21482)
* Fix non syncthru-syncthru supporting printers * Formatting * Update requirements_all * Update syncthru.py * Fix component to be async (as is the used SyncThru implementation) * Add async syntax * Omit loop passing * Don't await async_add_platform * Generate new all requirements * Explain, why exception is caught in setuExplain, why exception is caught in setupp * Handle failing initial setup correctly * Formatting * Formatting * Fix requested changes * Update requirements and add nielstron as codeowner * Run codeowners script * Make notification about missing syncthru support a warning * Revert pure formatting * Fix logging
This commit is contained in:
parent
d218ba98e7
commit
ef5ca63bf0
4 changed files with 79 additions and 50 deletions
|
@ -209,6 +209,7 @@ homeassistant/components/swiss_public_transport/* @fabaff
|
|||
homeassistant/components/switchbot/* @danielhiversen
|
||||
homeassistant/components/switcher_kis/* @tomerfi
|
||||
homeassistant/components/switchmate/* @danielhiversen
|
||||
homeassistant/components/syncthru/* @nielstron
|
||||
homeassistant/components/synology_srm/* @aerialls
|
||||
homeassistant/components/syslog/* @fabaff
|
||||
homeassistant/components/sytadin/* @gautric
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
"name": "Syncthru",
|
||||
"documentation": "https://www.home-assistant.io/components/syncthru",
|
||||
"requirements": [
|
||||
"pysyncthru==0.3.1"
|
||||
"pysyncthru==0.4.2"
|
||||
],
|
||||
"dependencies": [],
|
||||
"codeowners": []
|
||||
"codeowners": ["@nielstron"]
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import voluptuous as vol
|
|||
|
||||
from homeassistant.const import (
|
||||
CONF_RESOURCE, CONF_HOST, CONF_NAME, CONF_MONITORED_CONDITIONS)
|
||||
from homeassistant.helpers import aiohttp_client
|
||||
from homeassistant.helpers.entity import Entity
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
|
@ -12,40 +13,33 @@ from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_NAME = 'Samsung Printer'
|
||||
DEFAULT_MONITORED_CONDITIONS = [
|
||||
'toner_black',
|
||||
'toner_cyan',
|
||||
'toner_magenta',
|
||||
'toner_yellow',
|
||||
'drum_black',
|
||||
'drum_cyan',
|
||||
'drum_magenta',
|
||||
'drum_yellow',
|
||||
'tray_1',
|
||||
'tray_2',
|
||||
'tray_3',
|
||||
'tray_4',
|
||||
'tray_5',
|
||||
'output_tray_0',
|
||||
'output_tray_1',
|
||||
'output_tray_2',
|
||||
'output_tray_3',
|
||||
'output_tray_4',
|
||||
'output_tray_5',
|
||||
]
|
||||
COLORS = [
|
||||
'black',
|
||||
'cyan',
|
||||
'magenta',
|
||||
'yellow'
|
||||
]
|
||||
DRUM_COLORS = COLORS
|
||||
TONER_COLORS = COLORS
|
||||
TRAYS = range(1, 6)
|
||||
OUTPUT_TRAYS = range(0, 6)
|
||||
DEFAULT_MONITORED_CONDITIONS = []
|
||||
DEFAULT_MONITORED_CONDITIONS.extend(
|
||||
['toner_{}'.format(key) for key in TONER_COLORS]
|
||||
)
|
||||
DEFAULT_MONITORED_CONDITIONS.extend(
|
||||
['drum_{}'.format(key) for key in DRUM_COLORS]
|
||||
)
|
||||
DEFAULT_MONITORED_CONDITIONS.extend(
|
||||
['trays_{}'.format(key) for key in TRAYS]
|
||||
)
|
||||
DEFAULT_MONITORED_CONDITIONS.extend(
|
||||
['output_trays_{}'.format(key) for key in OUTPUT_TRAYS]
|
||||
)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_RESOURCE): cv.url,
|
||||
vol.Optional(
|
||||
CONF_NAME,
|
||||
default=DEFAULT_NAME
|
||||
): cv.string,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(
|
||||
CONF_MONITORED_CONDITIONS,
|
||||
default=DEFAULT_MONITORED_CONDITIONS
|
||||
|
@ -53,48 +47,70 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||
})
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
async def async_setup_platform(hass,
|
||||
config,
|
||||
async_add_entities,
|
||||
discovery_info=None):
|
||||
"""Set up the SyncThru component."""
|
||||
from pysyncthru import SyncThru, test_syncthru
|
||||
from pysyncthru import SyncThru
|
||||
|
||||
if discovery_info is not None:
|
||||
_LOGGER.info("Discovered a new Samsung Printer at %s",
|
||||
discovery_info.get(CONF_HOST))
|
||||
host = discovery_info.get(CONF_HOST)
|
||||
name = discovery_info.get(CONF_NAME, DEFAULT_NAME)
|
||||
_LOGGER.debug("Discovered a new Samsung Printer: %s", discovery_info)
|
||||
# Test if the discovered device actually is a syncthru printer
|
||||
if not test_syncthru(host):
|
||||
_LOGGER.error("No SyncThru Printer found at %s", host)
|
||||
return
|
||||
# Main device, always added
|
||||
monitored = DEFAULT_MONITORED_CONDITIONS
|
||||
else:
|
||||
host = config.get(CONF_RESOURCE)
|
||||
name = config.get(CONF_NAME)
|
||||
monitored = config.get(CONF_MONITORED_CONDITIONS)
|
||||
|
||||
# Main device, always added
|
||||
try:
|
||||
printer = SyncThru(host)
|
||||
except TypeError:
|
||||
# if an exception is thrown, printer cannot be set up
|
||||
return
|
||||
session = aiohttp_client.async_get_clientsession(hass)
|
||||
|
||||
printer = SyncThru(host, session)
|
||||
# Test if the discovered device actually is a syncthru printer
|
||||
# and fetch the available toner/drum/etc
|
||||
try:
|
||||
# No error is thrown when the device is off
|
||||
# (only after user added it manually)
|
||||
# therefore additional catches are inside the Sensor below
|
||||
await printer.update()
|
||||
supp_toner = printer.toner_status(filter_supported=True)
|
||||
supp_drum = printer.drum_status(filter_supported=True)
|
||||
supp_tray = printer.input_tray_status(filter_supported=True)
|
||||
supp_output_tray = printer.output_tray_status()
|
||||
except ValueError:
|
||||
# if an exception is thrown, printer does not support syncthru
|
||||
# and should not be set up
|
||||
# If the printer was discovered automatically, no warning or error
|
||||
# should be issued and printer should not be set up
|
||||
if discovery_info is not None:
|
||||
_LOGGER.info("Samsung printer at %s does not support SyncThru",
|
||||
host)
|
||||
return
|
||||
# Otherwise, emulate printer that supports everything
|
||||
supp_toner = TONER_COLORS
|
||||
supp_drum = DRUM_COLORS
|
||||
supp_tray = TRAYS
|
||||
supp_output_tray = OUTPUT_TRAYS
|
||||
|
||||
printer.update()
|
||||
devices = [SyncThruMainSensor(printer, name)]
|
||||
|
||||
for key in printer.toner_status(filter_supported=True):
|
||||
for key in supp_toner:
|
||||
if 'toner_{}'.format(key) in monitored:
|
||||
devices.append(SyncThruTonerSensor(printer, name, key))
|
||||
for key in printer.drum_status(filter_supported=True):
|
||||
for key in supp_drum:
|
||||
if 'drum_{}'.format(key) in monitored:
|
||||
devices.append(SyncThruDrumSensor(printer, name, key))
|
||||
for key in printer.input_tray_status(filter_supported=True):
|
||||
for key in supp_tray:
|
||||
if 'tray_{}'.format(key) in monitored:
|
||||
devices.append(SyncThruInputTraySensor(printer, name, key))
|
||||
for key in printer.output_tray_status():
|
||||
for key in supp_output_tray:
|
||||
if 'output_tray_{}'.format(key) in monitored:
|
||||
devices.append(SyncThruOutputTraySensor(printer, name, key))
|
||||
|
||||
add_entities(devices, True)
|
||||
async_add_entities(devices, True)
|
||||
|
||||
|
||||
class SyncThruSensor(Entity):
|
||||
|
@ -143,16 +159,28 @@ class SyncThruSensor(Entity):
|
|||
|
||||
|
||||
class SyncThruMainSensor(SyncThruSensor):
|
||||
"""Implementation of the main sensor, monitoring the general state."""
|
||||
"""Implementation of the main sensor, conducting the actual polling."""
|
||||
|
||||
def __init__(self, syncthru, name):
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(syncthru, name)
|
||||
self._id_suffix = '_main'
|
||||
self._active = True
|
||||
|
||||
def update(self):
|
||||
async def async_update(self):
|
||||
"""Get the latest data from SyncThru and update the state."""
|
||||
self.syncthru.update()
|
||||
if not self._active:
|
||||
return
|
||||
try:
|
||||
await self.syncthru.update()
|
||||
except ValueError:
|
||||
# if an exception is thrown, printer does not support syncthru
|
||||
_LOGGER.warning(
|
||||
"Configured printer at %s does not support SyncThru. "
|
||||
"Consider changing your configuration",
|
||||
self.syncthru.url
|
||||
)
|
||||
self._active = False
|
||||
self._state = self.syncthru.device_status()
|
||||
|
||||
|
||||
|
|
|
@ -1300,7 +1300,7 @@ pystride==0.1.7
|
|||
pysupla==0.0.3
|
||||
|
||||
# homeassistant.components.syncthru
|
||||
pysyncthru==0.3.1
|
||||
pysyncthru==0.4.2
|
||||
|
||||
# homeassistant.components.tautulli
|
||||
pytautulli==0.5.0
|
||||
|
|
Loading…
Add table
Reference in a new issue