1 """Support for Worx Landroid mower."""
3 from __future__
import annotations
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
22 _LOGGER = logging.getLogger(__name__)
24 CONF_ALLOW_UNREACHABLE =
"allow_unreachable"
28 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
30 vol.Required(CONF_HOST): cv.string,
31 vol.Required(CONF_PIN): vol.All(vol.Coerce(str), vol.Match(
r"\d{4}")),
32 vol.Optional(CONF_ALLOW_UNREACHABLE, default=
True): cv.boolean,
33 vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
39 "repositioning-error",
47 "collision-sensor-blocked",
57 async_add_entities: AddEntitiesCallback,
58 discovery_info: DiscoveryInfoType |
None =
None,
60 """Set up the Worx Landroid sensors."""
61 for typ
in (
"battery",
"state"):
66 """Implementation of a Worx Landroid sensor."""
69 """Initialize a Worx Landroid sensor."""
72 self.
hosthost = config.get(CONF_HOST)
73 self.
pinpin = config.get(CONF_PIN)
74 self.
timeouttimeout = config.get(CONF_TIMEOUT)
76 self.
urlurl = f
"http://{self.host}/jsondata.cgi"
80 """Return the name of the sensor."""
81 return f
"worxlandroid-{self.sensor}"
85 """Return the state of the sensor."""
90 """Return the unit of measurement of the sensor."""
91 if self.
sensorsensor ==
"battery":
96 """Update the sensor data from the mower."""
97 connection_error =
False
101 async
with asyncio.timeout(self.
timeouttimeout):
102 auth = aiohttp.helpers.BasicAuth(
"admin", self.
pinpin)
103 mower_response = await session.get(self.
urlurl, auth=auth)
104 except (TimeoutError, aiohttp.ClientError):
106 _LOGGER.error(
"Error connecting to mower at %s", self.
urlurl)
108 connection_error =
True
111 if connection_error
is True and self.
allow_unreachableallow_unreachable
is False:
112 if self.
sensorsensor ==
"error":
114 elif self.
sensorsensor ==
"state":
115 self.
_state_state =
"connection-error"
118 elif connection_error
is False:
121 data = await mower_response.json(content_type=
"text/html")
124 if self.
sensorsensor ==
"battery":
125 self.
_state_state = data[
"perc_batt"]
128 elif self.
sensorsensor ==
"error":
129 self.
_state_state =
"no" if self.
get_errorget_error(data)
is None else "yes"
132 elif self.
sensorsensor ==
"state":
135 elif self.
sensorsensor ==
"error":
140 """Get the mower error."""
141 for i, err
in enumerate(obj[
"allarmi"]):
142 if i != 2
and err == 1:
143 return ERROR_STATE[i]
148 """Get the state of the mower."""
149 if (state := self.
get_errorget_error(obj))
is None:
150 if obj[
"batteryChargerState"] ==
"charging":
151 return obj[
"batteryChargerState"]
def native_unit_of_measurement(self)
def __init__(self, sensor, config)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)